我已经为2个表实现了代码并且工作得很好。但是当我编写升级代码时,会出现此错误。我检查了大多数相关的帖子,甚至修复了OnUpgrade中的一些代码,其中db被递归调用。但我仍然无法弄明白。
数据库助手代码:
private static final int DATABASE_VERSION = 2;
public static final String DATABASE_NAME = "VotingUsers.db";
public static final String TABLE_IN = "in_Users";
public static final String TABLE_OUT = "out_Users";
public static final String COLUMN1 = "ID";
public static final String COLUMN2 = "NUMBER";
public static final String COLUMN3 = "FULL_NAME";
public static final String COLUMN4 = "DISPLAY_PICTURE";
public DataBaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE "+TABLE_IN+" ( " + COLUMN1 + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN2 + " INTEGER UNIQUE, " + COLUMN3 + " TEXT, " +COLUMN4+ " TEXT );" );
db.execSQL("CREATE TABLE "+TABLE_OUT+" ( " + COLUMN1 + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN2 + " INTEGER UNIQUE, " + COLUMN3 + " TEXT, " +COLUMN4+ " TEXT );" );
}
@Override
public void onUpgrade(SQLiteDatabase db,int oldVersion, int newVersion) {
Cursor cursor = db.rawQuery("select * from " + TABLE_IN,null);
if(cursor.getCount() != 0) {
db.execSQL("ALTER TABLE " + TABLE_IN + " ADD COLUMN FULL_NAME TEXT");
while(cursor.moveToNext()) {
db.execSQL("INSERT INTO " + TABLE_IN + " ("+COLUMN3+" ) Values ('"+cursor.getString(2) + " " + cursor.getString(3) +"')");
}
cursor.close();
}
db.execSQL("CREATE TABLE TEMP_TABLE ( " + COLUMN1 + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN2 + " INTEGER UNIQUE, " + COLUMN3 + " TEXT, " +COLUMN4+ " TEXT );" );
db.execSQL("INSERT INTO TEMP_TABLE (" +COLUMN2+", "+COLUMN3+", "+COLUMN4+" ) SELECT NUMBER, FULL_NAME, DISPLAY_PICTURE FROM "+TABLE_IN+");");
db.execSQL("DROP TABLE" + TABLE_IN);
db.execSQL("CREATE TABLE "+TABLE_IN+" ( " + COLUMN1 + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN2 + " INTEGER UNIQUE, " + COLUMN3 + " TEXT, " +COLUMN4+ " TEXT );" );
db.execSQL("INSERT INTO "+TABLE_IN+ " (" +COLUMN2+", "+COLUMN3+", "+COLUMN4+" ) SELECT " +COLUMN2+", "+COLUMN3+", "+COLUMN4+" FROM TEMP_TABLE );");
db.execSQL("DROP TABLE TEMP_TABLE");
cursor = db.rawQuery("select * from " + TABLE_OUT,null);
if(cursor.getCount() != 0) {
db.execSQL("ALTER TABLE " + TABLE_OUT + " ADD COLUMN FULL_NAME TEXT");
while(cursor.moveToNext()) {
db.execSQL("INSERT INTO " + TABLE_OUT + " ("+COLUMN3+" ) Values ('"+cursor.getString(2) + " " + cursor.getString(3) +"')");
}
cursor.close();
db.execSQL("CREATE TABLE TEMP_TABLE ( " + COLUMN1 + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN2 + " INTEGER UNIQUE, " + COLUMN3 + " TEXT, " +COLUMN4+ " TEXT );" );
db.execSQL("INSERT INTO TEMP_TABLE (" +COLUMN2+", "+COLUMN3+", "+COLUMN4+" ) SELECT NUMBER, FULL_NAME, DISPLAY_PICTURE FROM "+TABLE_OUT+");");
db.execSQL("DROP TABLE" + TABLE_OUT);
db.execSQL("CREATE TABLE "+TABLE_OUT+" ( " + COLUMN1 + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN2 + " INTEGER UNIQUE, " + COLUMN3 + " TEXT, " +COLUMN4+ " TEXT );" );
db.execSQL("INSERT INTO "+TABLE_OUT+ " (" +COLUMN2+", "+COLUMN3+", "+COLUMN4+" ) SELECT " +COLUMN2+", "+COLUMN3+", "+COLUMN4+" FROM TEMP_TABLE );");
db.execSQL("DROP TABLE TEMP_TABLE");
}
}
public void insertDataInUsers(Integer number, String name, String dp) {
String ROW1 = "INSERT INTO " + TABLE_IN + " ("
+COLUMN2+", "+COLUMN3+", "+COLUMN4+" ) Values ('"+number+"', '"+name+"', '"+dp+"')";
getDb().execSQL(ROW1);
}
public void insertDataOutUsers(Integer number, String name, String dp) {
String ROW2 = "INSERT INTO " + TABLE_OUT + " ("
+COLUMN2+", "+COLUMN3+", "+COLUMN4+" ) Values ('"+number+"', '"+name+"', '"+dp+"')";
getDb().execSQL(ROW2);
}
public SQLiteDatabase getDb() {
SQLiteDatabase db = this.getWritableDatabase();
return db;
}
public Cursor ShowInUserList() {
Cursor cursor = getDb().rawQuery("select * from " + TABLE_IN,null);
return cursor;
}
public Cursor ShowOutUserList() {
Cursor cursor = getDb().rawQuery("select * from " + TABLE_OUT,null);
return cursor;
}
这可能是一个小错误,但我不知道。
ERROR:
Caused by: java.lang.IllegalStateException: getDatabase called recursively
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:203)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at com.example.android.voteinoutexample.DataBaseHelper.createDb(DataBaseHelper.java:108)
at com.example.android.voteinoutexample.DataBaseHelper.ShowInUserList(DataBaseHelper.java:114)
at com.example.android.voteinoutexample.DataBaseHelper.onUpgrade(DataBaseHelper.java:42)
LINE 108
public Cursor ShowInUserList() {
Cursor cursor = getDb().rawQuery("select * from " + TABLE_IN,null);
return cursor;
}
在我的MainFragment OnCreate中,我有条件检查表是否存在,然后做一些事情..
if(!InTableExists() && !OutTableExists()) {
getUsers();
}else {
Cursor cursor = dbHelper.ShowInUserList();
if(cursor.getCount() != 0) {
data.add(new Vote(Vote.HEADER_TYPE, "IN"));
while(cursor.moveToNext()) {
data.add(new Vote(Vote.ITEM_TYPE, cursor.getString(2) + " " + cursor.getString(3),cursor.getString(4)));
}
cursor.close();
}
cursor = dbHelper.ShowOutUserList();
if(cursor.getCount() != 0) {
data.add(new Vote(Vote.HEADER_TYPE, "OUT"));
while(cursor.moveToNext()) {
data.add(new Vote(Vote.ITEM_TYPE, cursor.getString(2) + " " + cursor.getString(3),cursor.getString(4)));
}
cursor.close();
Toast.makeText(getActivity(),"DATA FROM DATABASE",Toast.LENGTH_LONG).show();
}
}
因此if条件中InTableExists()的求值表达式也显示结果illegalStateException
CAUSE = android.database.sqlite.SQLiteException:near“)”:语法错误(代码1):,同时编译:INSERT INTO TEMP_TABLE(NUMBER,FULL_NAME,DISPLAY_PICTURE)SELECT NUMBER,FULL_NAME,DISPLAY_PICTURE FROM in_Users);