S Fitz S Fitz - 3 months ago 11
Android Question

Android SQLiteDatabase syntax error, code 1

I'm making an SQLiteDatabase based from this source, but I've run into a syntax error that I can't figure out. I've seen the other posts on StackOverflow about this, but they don't seem to help. In those cases, they contain an extra space somewhere, or a reserved word, but I've tried different combinations of spacing and made sure none of the words are reserved.

The only thing I can think of is that I'm making two tables in one database, is that unacceptable? Here's the gist of the code that's causing the problem:

//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);
}


And here's my log output just in case my interpretation is wrong:

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


Can anyone spot it for me? I'm still rusty with SQLiteDatabase syntax unfortunately.

Answer

Quick fix:

change to

 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" + ")";

Explanation:

The execSQL() method requires a SQL string, which has to be constructed in a specific way, following the guidelines. The original question had CREATEINGREDIENTSTABLE IF NOT EXISTS, which is not supported by the execSQL() method. When it's replaced by CREATE TABLE IF NOT EXISTS, it will work fine.

For more info, have a look at the SQLiteDatabase page over on developer.android.com, and specifically the two execSQL() methods.