如果你在DBHelper类中查看我的onCreate,我有两个注释掉了.这使得它无论哪一个都不公开都可以工作.
必须有一种方法来创建多表数据库,因为数据库中的单个表几乎违背了拥有数据库的目的.
10-23 02:11:35.383: ERROR/AndroIDRuntime(300): Caused by: androID.database.sqlite.sqliteException: Can't upgrade read-only database from version 0 to 1: /data/data/com.parkingticket/databases/Tickets.db
提前致谢.
这是我的代码
package com.parkingticket; import java.sql.sqlException; import androID.content.ContentValues; import androID.content.Context; import androID.database.Cursor; import androID.database.sqlite.sqliteDatabase; import androID.database.sqlite.sqliteDatabase.CursorFactory; import androID.database.sqlite.sqliteException; import androID.database.sqlite.sqliteOpenHelper; import androID.util.Log; public class TicketDBAdapter {private static final String DATABASE_name="Tickets.db";private static final int DATABASE_VERSION = 1;private static final String PARKEDCARS_table = "ParkedCars";private static final String PARKINGMETERS_table = "ParkingMeters";private static final String PARKINGTICKETS_table = "ParkingTickets";private static final String POliCEOFFICERS_table = "PoliceOfficers";// The name and column index for each column in PARKEDCARSpublic static final String KEY_CARID = "carID"; public static final int CARID_ColUMN = 0;public static final String KEY_CARMAKE = "Make"; public static final int CARMAKE_ColUMN = 1;public static final String KEY_CARMODEL = "Model"; public static final int CARMODEL_ColUMN = 2;public static final String KEY_CARcolor = "color"; public static final int CARcolor_ColUMN = 3;public static final String KEY_CARliCENSENUMBER = "licenseNumber"; public static final int CARliCENSENUMBER_ColUMN = 4;public static final String KEY_CARMINUTESPARKED = "MinutesParked"; public static final int CARMINUTESPARKED_ColUMN = 5;// The name and column index for each column in PARKINGMETERSpublic static final String KEY_METERID = "meterID"; public static final int METERID_ColUMN = 0;public static final String KEY_MINUTESPURCHASED = "MinutesPurchased"; public static final int MINUTESPURCHASED_ColUMN = 1;// The name and column index for each column in PARKINGTICKETS //Todo create the columns and indexs for parking tickets// The name and column index for each column in POliCEOFFICERSpublic static final String KEY_OFFICERID = "officerID"; public static final int OFFICERID_ColUMN = 0;public static final String KEY_OFFICERname = "name"; public static final int OFFICERname_ColUMN = 1;public static final String KEY_OFFICERBADGE = "BadgeNumber"; public static final int OFFICERBADE_ColUMN = 2;//Variable to hold the database instanceprivate sqliteDatabase ticketDB;//Context of the application using the database.private final Context context;//Database open/upgrade helperprivate TicketDBHelper ticketDBHelper;public TicketDBAdapter(Context _context){ context = _context; ticketDBHelper = new TicketDBHelper(context,DATABASE_name,null,DATABASE_VERSION);}public voID open() throws sqliteException{ try { ticketDB = ticketDBHelper.getWritableDatabase(); } catch(sqliteException ex) { ticketDB = ticketDBHelper.getReadableDatabase(); }}public voID close(){ ticketDB.close();} //Insert a new ParkedCarpublic long insertParkedCar(ParkedCar _car){ //Create a new row of values to insert ContentValues newParkedCarValues = new ContentValues(); //Assign values for each row newParkedCarValues.put(KEY_CARMAKE,_car.getMake()); newParkedCarValues.put(KEY_CARMODEL,_car.getModel()); newParkedCarValues.put(KEY_CARcolor,_car.getcolor()); newParkedCarValues.put(KEY_CARliCENSENUMBER,_car.getlicenseNumber()); newParkedCarValues.put(KEY_CARMINUTESPARKED,_car.getMinutesParked()); //Insert the row return ticketDB.insert(PARKEDCARS_table,newParkedCarValues);}//Remove a ParkedCar based on its indexpublic boolean removeParkedCar(long _rowIndex){ return ticketDB.delete(PARKEDCARS_table,KEY_CARID + "=" + _rowIndex,null)>0;}//Update a ParkedCar's MinutesParked//Todo Create an update for ParkedCar's minutesParked.public Cursor getAllParkedCarsCursor(){ return ticketDB.query(PARKEDCARS_table,new String[] {KEY_CARID,KEY_CARMAKE,KEY_CARMODEL,KEY_CARcolor,KEY_CARliCENSENUMBER,KEY_CARMINUTESPARKED},null);}public Cursor setCursorParkedCar(long _rowIndex) throws sqlException{ Cursor result = ticketDB.query(true,PARKEDCARS_table,new String []{KEY_CARID},null); if ((result.getCount() == 0) || !result.movetoFirst()) { throw new sqlException("No ParkedCar found for row: " + _rowIndex); } return result;}public static class TicketDBHelper extends sqliteOpenHelper{ public TicketDBHelper(Context context,String name,CursorFactory factory,int version) { super(context,name,factory,version); } //sql Statement to create PARKEDCARS table private static final String PARKEDCARS_CREATE = "create table " + PARKEDCARS_table + " (" + KEY_CARID + " integer primary key autoincrement," + KEY_CARMAKE + " text not null," + KEY_CARMODEL + " text not null," + KEY_CARcolor + " text not null," + KEY_CARliCENSENUMBER + " text not null," + KEY_CARMINUTESPARKED + "int not null);"; //sql Statement to create ParkingMeters table private static final String PARKINGMETERS_CREATE = "create table" + PARKINGMETERS_table + " (" + KEY_METERID + " integer primary key autoincrement," + KEY_MINUTESPURCHASED + " int not null);"; //sql Statement to create ParkingTickets table //Todo create the statement for parkingTickets //sql Statement to create PoliceOfficers table private static final String POliCEOFFICERS_CREATE = "create table" + POliCEOFFICERS_table + " (" + KEY_OFFICERID + " integer primary key autoincrement," + KEY_OFFICERname + " text not null," + KEY_OFFICERBADGE + "text not null);"; //Called when no database exists in disk and the helper class needs to create a new one. @OverrIDe public voID onCreate(sqliteDatabase _db) { //_db.execsql(PARKEDCARS_CREATE); _db.execsql(PARKINGMETERS_CREATE); //_db.execsql(POliCEOFFICERS_CREATE); } //Called when there is a database verion mismatch meaning that the version of the database on disk needs to be upgraded to the current version @OverrIDe public voID onUpgrade(sqliteDatabase _db,int _oldVersion,int _newVersion) { //Log the version upgrade. Log.w("TaskDBAdapter","Upgrading from version " + _oldVersion + " to " + _newVersion + ",which will destroy all old data"); //Upgrade the existing database to conform to the new version //Multiple prevIoUs versions can be handled by comparing _oldVersoin and _newVersion values //The simplest case is to drop teh old table and create a new one. _db.execsql("DROP table IF EXISTS " + PARKEDCARS_table); _db.execsql("DROP table IF EXISTS " + PARKINGMETERS_table); _db.execsql("DROP table IF EXISTS " + POliCEOFFICERS_table); onCreate(_db); }} }解决方法 我知道我迟到了,但我认为这可能有助于其他人阅读这篇文章.
我有同样的问题,它是关于空格的,当你在编写关于空间的SQL查询时要非常小心.例如在上面的代码中,你可以注意到最后没有空格:
KEY_OFFICERBADGE“text not null”
所以基本上,你已经声明了一个没有类型的列.希望它有所帮助.
总结以上是内存溢出为你收集整理的在android中创建一个多表SQL数据库全部内容,希望文章能够帮你解决在android中创建一个多表SQL数据库所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)