我创建了一个SqLite数据库程序,但每次显示以下错误
找不到这样的表格
请告知为何显示错误。
public static final String TOWN_ID="_id";
public static final String TOWN_NAME="name";
public static final String DISTT_ID="disttidd";
private static final String TAG="DbaAdapter";
private static final String DATABASE_NAME="mydb";
private static final String DATABASE_TABLE="tableetown";
private static final int DATABASE_VERSION=2;
private static final String DATABASE_CREATE="create table "+DATABASE_TABLE+"("+TOWN_ID+" int primary key ,"+TOWN_NAME+" text not null,"+DISTT_ID+" text not null)";
private final Context context;
private DatabaseHelper DBhelper;
private static SQLiteDatabase db;
public tableTown(Context ctx){
this.context=ctx;
DBhelper=new DatabaseHelper(context);
}
/* db=DBhelper.getWritableDatabase();
return this;
*
*/
private static class DatabaseHelper extends SQLiteOpenHelper{
DatabaseHelper(Context context){
super(context,DATABASE_NAME,null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.w(TAG,"Upgrading database from version " + oldVersion +" to "+ newVersion+", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS tableTown");
onCreate(db);
}
}
public tableTown open() throws SQLException {
db=DBhelper.getWritableDatabase();
return this;
// TODO Auto-generated method stub
}
public Cursor fetchAllDistt() {
// TODO Auto-generated method stub
Cursor mCursor = db.query(DATABASE_TABLE, new String[] {TOWN_ID,TOWN_NAME,DISTT_ID}, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public void close() {
// TODO Auto-generated method stub
DBhelper.close();
}
}
答案 0 :(得分:0)
在你的创建中写下这个:
String DATABASE_CREATE = "CREATE TABLE " + DATABASE_TABLE + "("
+ TOWN_ID + " INTEGER PRIMARY KEY," + TOWN_NAME + " TEXT NOT NULL,"
+ DISTT_ID + " TEXT NOT NULL" + " )";
db.execSQL(DATABASE_CREATE);
答案 1 :(得分:0)
答案 2 :(得分:0)
如果你的应用程序在onUpgrade崩溃,那么Volodymyr Yatsykiv回答了你的问题。尝试将其更改为
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS " +DATABASE_TABLE);
onCreate(db);
}
并确保使用DATABASE_TABLE而不是硬编码表名称。
如果仍然崩溃,请提供完整的日志。
祝福。
答案 3 :(得分:-1)
试试这个:
String DATABASE_CREATE="create table "+DATABASE_TABLE+"("+TOWN_ID+" integer primary key ,"+TOWN_NAME+" text not null,"+DISTT_ID+" text not null);";
把“;”到DATABASE_CREATE字符串的末尾。
寻找这个:
private static final String DATABASE_TABLE="tableetown";
和此:
db.execSQL("DROP TABLE IF EXISTS tableTown");
表名不同!