我正在根据这个来源制作一个 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 语法仍然生疏。