2012-04-09 65 views
3

我有一个数据库已经包含一个表“LocalLogin”,并且已经正确设置并且可以在没有问题的情况下被查询,正在设置与我的新表“PersonList”一样。然而,当我尝试执行上PersonList查询选择一些价值观,我得到的错误在数据库上执行查询时Android“android.database.sqlite.SQLiteException:没有这样的表”错误

android.database.sqlite.SQLiteException: no such table: PersonList: , while compiling: SELECT ... 

它给我的表从来没有创建的想法,虽然我的onCreate方法执行创建查询我的SQLiteOpenHelper类。与LocalLogin具有相同的数据库名称可能存在的问题?

下面是相关代码:

数据库适配器类

public class GoingOutPersonListDbAdapter { 
private static final String DATABASE_NAME = "GoingOutData"; 
private static final String DATABASE_TABLE_PERSONLIST = "PersonList"; 
private static final int DATABASE_VERSION = 1; 

public static final String PERSONLIST_ID = "PersonList_id"; 
public static final String PERSONLIST_LOGIN = "Login"; 
public static final String PERSONLIST_PASSWORD = "Password"; 

private static final String TAG = "Debugstring"; 

private PersonListDatabaseHelper mPersonListDbHelper; 
private SQLiteDatabase mDb; 

private static final String DATABASE_CREATE = 
    "CREATE Table " + DATABASE_TABLE_PERSONLIST+ " (" 
    + PERSONLIST_ID + " integer PRIMARY KEY Autoincrement, " 
    + PERSONLIST_LOGIN + " text NOT NULL," 
    + PERSONLIST_PASSWORD + " text NOT NULL);"; 

private final Context mCtx; 

private static class PersonListDatabaseHelper extends SQLiteOpenHelper { 
    PersonListDatabaseHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 
    @Override 
    public void onCreate(SQLiteDatabase db) { 
     Log.d(TAG,DATABASE_CREATE); 
     db.execSQL(DATABASE_CREATE); 
    } 
} 

public GoingOutPersonListDbAdapter(Context ctx) { 
    this.mCtx = ctx; 
} 

public GoingOutPersonListDbAdapter open() throws SQLException { 
    mPersonListDbHelper = new PersonListDatabaseHelper(mCtx); 
    mDb = mPersonListDbHelper.getWritableDatabase(); 
    return this; 
} 

public Cursor searchPerson(String searchString) { 
    return mDb.query(DATABASE_TABLE_PERSONLIST, new String[] {PERSONLIST_ID, PERSONLIST_LOGIN, PERSONLIST_PASSWORD}, searchString, null, null, null, null); 
} 

}

在我的活动类:

private GoingOutPersonListDbAdapter mPersonListDbHelper; 

... 

mPersonListDbHelper = new GoingOutPersonListDbAdapter(this); 
mPersonListDbHelper.open(); 

... 

//loginEditText is properly set 
Cursor personList = mPersonListDbHelper.searchPerson(GoingOutPersonListDbAdapter.PERSONLIST_LOGIN + " = '" + loginEditText + "'"); 
startManagingCursor(personList); 

回答

5

如果你要添加一个新表,你需要增加你的数据库版本,这样onUpdate会触发,你的数据库将被新表重新创建。

+0

我添加 “\t \t @Override \t \t公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,INT NEWVERSION){ \t \t \t db.execSQL(DATABASE_CREATE); \t \t}”,改变了数据库的版本为2,但我仍然有相同的错误 – 2012-04-09 20:31:36

+0

工程就像一个魅力,我在两个数据库适配器中增加数据库版本,我显然不应该这样做 – 2012-04-09 20:58:30

+0

适合我 – 2012-07-12 10:44:10

0

你申报的私人静态最后弦乐DATABASE_CREATE“两次。我很惊讶这个编译,实际上?也就是说,你应该添加一个日志语句到db.execSQL(DATABASE_CREATE);打电话,看看它是否真的成功。

+0

我编辑了我的代码。我留下了大量不相关的部分,不要在这里发布太多,并且出现了错误的副本 – 2012-04-09 20:19:47

+0

不够公平;但是您仍然需要确定表格是否正在创建。你的语法对我来说很合适,但由于其他原因它可能会悄然失败,这意味着桌子不在那里。 – 2012-04-09 20:25:44

相关问题