我正在尝试向我的sqlite数据库添加一个新列。我以前添加了一个额外的列(电子邮件),并没有任何问题。下面是我创建的DbHelper类。
public class DbHelper extends SQLiteOpenHelper {
public static String DATABASE_NAME="userdata";
public static final String TABLE_NAME="user";
public static final String KEY_FNAME="fname";
public static final String KEY_LNAME="lname";
public static final String KEY_EMAIL="email";
public static final String KEY_CHECK="check";
public static final String KEY_ID="id";
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE="CREATE TABLE " +TABLE_NAME+" ("+KEY_ID+" INTEGER PRIMARY KEY, "+KEY_FNAME+" TEXT, "+KEY_LNAME+" TEXT, "+KEY_CHECK+" TEXT)";
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
}
创建数据库时,它将从我的主要活动中显示:
private void displayData() {
dataBase = mHelper.getWritableDatabase();
Cursor mCursor = dataBase.rawQuery("SELECT * FROM "
+ DbHelper.TABLE_NAME, null);
userId.clear();
user_fName.clear();
user_lName.clear();
user_eMail.clear();
user_cHeck.clear();
if (mCursor.moveToFirst()) {
do {
userId.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_ID)));
user_fName.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_FNAME)));
user_lName.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_LNAME)));
user_eMail.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_EMAIL)));
user_cHeck.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_CHECK)));
} while (mCursor.moveToNext());
}
DisplayAdapter disadpt = new DisplayAdapter(getActivity(),userId, user_fName, user_lName, user_eMail, user_cHeck);
userList.setAdapter(disadpt);
mCursor.close();
}
{
}
在创建时,我收到以下错误:
04-09 15:33:16.740: E/SQLiteLog(19806): (1) near "TEXT": syntax error
04-09 15:33:16.755: W/dalvikvm(19806): threadid=1: thread exiting with uncaught exception (group=0x40df4a08)
04-09 15:33:16.765: E/AndroidRuntime(19806): FATAL EXCEPTION: main
04-09 15:33:16.765: E/AndroidRuntime(19806): android.database.sqlite.SQLiteException: near "TEXT": syntax error (code 1): , while compiling: CREATE TABLE user (id INTEGER PRIMARY KEY, fname TEXT, lname TEXT, check TEXT)
04-09 15:33:16.765: E/AndroidRuntime(19806): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
04-09 15:33:16.765: E/AndroidRuntime(19806): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:909)
04-09 15:33:16.765: E/AndroidRuntime(19806): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:520)
04-09 15:33:16.765: E/AndroidRuntime(19806): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
04-09 15:33:16.765: E/AndroidRuntime(19806): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
04-09 15:33:16.765: E/AndroidRuntime(19806): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
04-09 15:33:16.765: E/AndroidRuntime(19806): at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1719)
04-09 15:33:16.765: E/AndroidRuntime(19806): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1650)
04-09 15:33:16.765: E/AndroidRuntime(19806): at app.norman.tennis.fours.DbHelper.onCreate(DbHelper.java:23)
04-09 15:33:16.765: E/AndroidRuntime(19806): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
04-09 15:33:16.765: E/AndroidRuntime(19806): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
04-09 15:33:16.765: E/AndroidRuntime(19806): at app.norman.tennis.FoursFragment.displayData(FoursFragment.java:244)
04-09 15:33:16.765: E/AndroidRuntime(19806): at app.norman.tennis.FoursFragment.onResume(FoursFragment.java:235)
04-09 15:33:16.765: E/AndroidRuntime(19806): at android.app.Fragment.performResume(Fragment.java:1828)
04-09 15:33:16.765: E/AndroidRuntime(19806): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:921)
04-09 15:33:16.765: E/AndroidRuntime(19806): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1059)
04-09 15:33:16.765: E/AndroidRuntime(19806): at android.app.BackStackRecord.run(BackStackRecord.java:682)
04-09 15:33:16.765: E/AndroidRuntime(19806): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1437)
04-09 15:33:16.765: E/AndroidRuntime(19806): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
04-09 15:33:16.765: E/AndroidRuntime(19806): at android.os.Handler.handleCallback(Handler.java:725)
04-09 15:33:16.765: E/AndroidRuntime(19806): at android.os.Handler.dispatchMessage(Handler.java:92)
04-09 15:33:16.765: E/AndroidRuntime(19806): at android.os.Looper.loop(Looper.java:158)
04-09 15:33:16.765: E/AndroidRuntime(19806): at android.app.ActivityThread.main(ActivityThread.java:5751)
04-09 15:33:16.765: E/AndroidRuntime(19806): at java.lang.reflect.Method.invokeNative(Native Method)
04-09 15:33:16.765: E/AndroidRuntime(19806): at java.lang.reflect.Method.invoke(Method.java:511)
04-09 15:33:16.765: E/AndroidRuntime(19806): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
04-09 15:33:16.765: E/AndroidRuntime(19806): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
04-09 15:33:16.765: E/AndroidRuntime(19806): at dalvik.system.NativeStart.main(Native Method)
任何人都可以帮助我理解为什么我遇到这个问题。提前谢谢。
答案 0 :(得分:1)
check
是SQL中的保留字。重命名列或在` `
反引号中引用它。
答案 1 :(得分:1)
CHECK
是SQLite中的保留字。请为此列选择其他名称。在创建其他语句时,它会为您节省很多麻烦。