没有这样的表在SqLite数据库程序中发现错误

时间:2013-04-12 07:59:52

标签: android sqlite

我创建了一个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();
}

}

4 个答案:

答案 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)

我在这种情况下使用SQLiteManager

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

希望这有帮助,Yaron

答案 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");

表名不同!