Android SQLiteDatabase语法错误,代码1

时间:2013-02-01 11:04:16

标签: android syntax-error sqlite

我正在制作一个基于this source的SQLiteDatabase,但我遇到了一个我无法弄清楚的语法错误。我已经在StackOverflow上看到了关于此的其他帖子,但它们似乎没有帮助。在这些情况下,它们在某处包含一个额外的空格,或者是一个保留字,但我尝试了不同的间距组合,并确保没有任何单词被保留。

我唯一能想到的是我在一个数据库中制作两个表,这是不可接受的吗?以下是导致问题的代码的要点:

//table name
private static final String TABLE_INGREDIENTS = "table_ingredients";
private static final String TABLE_UNITS = "table_units";

//column names
private static final String ING_KEY_ID = "ing_id";
private static final String ING_KEY_NAME = "ing_name";
private static final String ING_KEY_DENSITY = "ing_density";    
private static final String UNIT_KEY_ID = "unit_id";
private static final String UNIT_KEY_NAME = "unit_name";
private static final String UNIT_KEY_BASE_VALUE = "unit_base_value";
private static final String UNIT_KEY_FINAL_VALUE = "unit_final_value";
private static final String UNIT_KEY_IS_WEIGHT = "unit_is_weight";

// Create Tables
@Override
public void onCreate(SQLiteDatabase db) 
{
    String CREATE_ING_TABLE = "CREATE INGREDIENTS TABLE IF NOT EXISTS " + TABLE_INGREDIENTS + "("
            + ING_KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + ING_KEY_NAME + " TEXT,"
            + ING_KEY_DENSITY + " REAL" + ")";
    db.execSQL(CREATE_ING_TABLE);

    String CREATE_UNIT_TABLE = "CREATE UNITS TABLE IF NOT EXISTS " + TABLE_UNITS + "("
            + UNIT_KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + UNIT_KEY_NAME + " TEXT,"
            + UNIT_KEY_BASE_VALUE + " REAL," + UNIT_KEY_FINAL_VALUE + " REAL,"
            + UNIT_KEY_IS_WEIGHT + " INTEGER" + ")";        
    db.execSQL(CREATE_UNIT_TABLE);
}

这是我的日志输出,以防我的解释错误:

02-01 12:48:43.855: D/QBCPro:::(7186): LoadAllTask doInBackground called...
02-01 12:48:43.855: D/QBCPro:::(7186): Entering onResume()...
02-01 12:48:43.875: E/SQLiteLog(7186): (1) near "INGREDIENTS": syntax error
02-01 12:48:43.875: W/dalvikvm(7186): threadid=11: thread exiting with uncaught exception (group=0x41eb8300)
02-01 12:48:43.895: E/AndroidRuntime(7186): FATAL EXCEPTION: AsyncTask #1
02-01 12:48:43.895: E/AndroidRuntime(7186): java.lang.RuntimeException: An error occured while executing doInBackground()
02-01 12:48:43.895: E/AndroidRuntime(7186):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
02-01 12:48:43.895: E/AndroidRuntime(7186):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
02-01 12:48:43.895: E/AndroidRuntime(7186):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
02-01 12:48:43.895: E/AndroidRuntime(7186):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
02-01 12:48:43.895: E/AndroidRuntime(7186):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-01 12:48:43.895: E/AndroidRuntime(7186):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-01 12:48:43.895: E/AndroidRuntime(7186):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
02-01 12:48:43.895: E/AndroidRuntime(7186):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
02-01 12:48:43.895: E/AndroidRuntime(7186):     at java.lang.Thread.run(Thread.java:856)
02-01 12:48:43.895: E/AndroidRuntime(7186): Caused by: android.database.sqlite.SQLiteException: near "INGREDIENTS": syntax error (code 1): , while compiling: CREATE INGREDIENTS TABLE IF NOT EXISTS table_ingredients(ing_id INTEGER PRIMARY KEY AUTOINCREMENT,ing_name TEXT,ing_density REAL)
02-01 12:48:43.895: E/AndroidRuntime(7186):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
02-01 12:48:43.895: E/AndroidRuntime(7186):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
02-01 12:48:43.895: E/AndroidRuntime(7186):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
02-01 12:48:43.895: E/AndroidRuntime(7186):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
02-01 12:48:43.895: E/AndroidRuntime(7186):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
02-01 12:48:43.895: E/AndroidRuntime(7186):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
02-01 12:48:43.895: E/AndroidRuntime(7186):     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1663)
02-01 12:48:43.895: E/AndroidRuntime(7186):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594)
02-01 12:48:43.895: E/AndroidRuntime(7186):     at com.sfware.quickbakeconverterpro.DatabaseHelper.onCreate(DatabaseHelper.java:49)
02-01 12:48:43.895: E/AndroidRuntime(7186):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
02-01 12:48:43.895: E/AndroidRuntime(7186):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
02-01 12:48:43.895: E/AndroidRuntime(7186):     at com.sfware.quickbakeconverterpro.DatabaseHelper.open(DatabaseHelper.java:72)
02-01 12:48:43.895: E/AndroidRuntime(7186):     at com.sfware.quickbakeconverterpro.QuickBakeConverterPro$LoadAllTask.doInBackground(QuickBakeConverterPro.java:690)
02-01 12:48:43.895: E/AndroidRuntime(7186):     at com.sfware.quickbakeconverterpro.QuickBakeConverterPro$LoadAllTask.doInBackground(QuickBakeConverterPro.java:1)
02-01 12:48:43.895: E/AndroidRuntime(7186):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-01 12:48:43.895: E/AndroidRuntime(7186):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
02-01 12:48:43.895: E/AndroidRuntime(7186):     ... 5 more
02-01 12:48:44.045: D/libEGL(7186): loaded /system/lib/egl/libEGL_mali.so
02-01 12:48:44.135: D/libEGL(7186): loaded /system/lib/egl/libGLESv1_CM_mali.so
02-01 12:48:44.145: D/libEGL(7186): loaded /system/lib/egl/libGLESv2_mali.so
02-01 12:48:44.215: D/OpenGLRenderer(7186): Enabling debug mode 0
02-01 12:48:44.400: D/QBCPro:::(7186): Entering onPause()...
02-01 12:48:44.405: D/QBCPro:::(7186): Leaving onPause()...
02-01 12:48:44.545: D/QBCPro:::(7186): Entering onStop()...
02-01 12:48:44.545: D/QBCPro:::(7186): About to save data in new SaveAllTask
02-01 12:48:44.545: D/QBCPro:::(7186): Leaving onStop()...
02-01 12:48:44.545: D/QBCPro:::(7186): SaveAllTask doInBackground called...
02-01 12:48:44.560: E/SQLiteLog(7186): (1) near "INGREDIENTS": syntax error

有人能为我发现吗?不幸的是,我仍然厌恶SQLiteDatabase语法。

1 个答案:

答案 0 :(得分:6)

快速修复:

更改为

 String CREATE_ING_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_INGREDIENTS + "("
            + ING_KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + ING_KEY_NAME + " TEXT,"
            + ING_KEY_DENSITY + " REAL" + ")";

说明:

execSQL()方法需要一个SQL字符串,必须按照指南以特定方式构造。原始问题有CREATE 成分 TABLE IF NOT EXISTSexecSQL()方法不支持此问题。当它被CREATE TABLE IF NOT EXISTS替换时,它将正常工作。

有关详细信息,请查看developer.android.com上的SQLiteDatabase page,特别是两个execSQL()方法。