0

我创建了一个 SqlLite 数据库程序,但每次它显示以下错误

没有找到这样的表

请告知为什么显示错误。

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();
}

}
4

4 回答 4

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 而不是硬编码表名。

如果它仍然崩溃,请提供完整的日志。

最好的祝愿。

于 2013-06-20T22:08:05.827 回答
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);
于 2013-04-12T08:30:32.183 回答
0

我在这种情况下使用SQLiteManager

在 onCreate 中放置一个断点,确保该行被执行。然后,切换到 DDMS 视图并使用插件找出问题所在。

希望这会有所帮助,亚龙

于 2013-04-12T09:01:40.283 回答
-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");

表名不同!

于 2013-04-12T08:14:48.597 回答