I'm getting this error when running my code in Android Studio.
08-06 04:10:00.209 24514-24514/com.example.mk.anotherapp E/SQLiteLog﹕ (1) near "16": syntax error
08-06 04:10:00.269 24514-24514/com.example.mk.anotherapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.mk.anotherapp, PID: 24514
android.database.sqlite.SQLiteException: near "16": syntax error (code 1): , while compiling: SELECT 10 FROM 16 WHERE _id=?
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1238)
at com.example.mk.anotherapp.Irradfrag$1.onClick(Irradfrag.java:108)
at android.view.View.performClick(View.java:4445)
at android.view.View$PerformClick.run(View.java:18446)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5146)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
Here is the relevant code:
//assign actual values
Integer pitch = pitchsel;
Integer orientation = orientaationsel * 5;
Double shadefactor = shadesel / 100.0;
String postregion = "1";
RegionDatabase mDb = new RegionDatabase(getActivity());
SQLiteDatabase db = mDb.getReadableDatabase();
String[] columns = {"Region"};
String Selection = "_id=?";
String[] SelectionArgs ={""+postcodesel};
Cursor cursor = db.query("Region_Key",columns,Selection,SelectionArgs,null,null,null,null);
while (cursor.moveToNext()) {
postregion = cursor.getString(cursor.getColumnIndexOrThrow("Region"));
Toast.makeText(getActivity(),"Region:"+ postregion, Toast.LENGTH_SHORT).show();
}
String[] columnsx = {""+orientation};
String[] SelectionArgsx ={""+pitch};
String ratio ="1";
// This is the query that the error refers to
cursor = db.query(postregion,columnsx,Selection,SelectionArgsx,null,null,null,null);
while (cursor.moveToNext()) {
ratio = cursor.getString(cursor.getColumnIndexOrThrow(""+orientation));
Toast.makeText(getActivity(),"Ratio:"+ ratio, Toast.LENGTH_SHORT).show();
}
What really has me stumped is that the first query works perfectly and the second doesn't. This is a syntax error and the syntax seems to be exactly the same. The queries are done in two different tables in the same database. I've double checked the column names and data values to make sure everything exists as typed.
The other times this error has been posted, it is usually a case of wrong quotes in a rawquery, but that's not what I'm doing.
EDIT
My database is premade and consists of 25 tables with 37 columns each. I generated it from a spreadsheet whose data I have to be able to access. If the name of each column has to be non-numeric, any idea how to easily change the column names?
EDIT 2
Thanks everyone. It was due to numeric table names. I submitted an answer with 2 solutions below.