SQLite SELECT gives exception How to fix it?
android, eclipse, exception, select, sqlite
Solution
db.execSQL("SELECT * FROM actuator");
You can't use `execSQL` method for statements which return data. Exactly from docs
Execute a single SQL statement that is NOT a SELECT or any other SQL statement that returns data.
You have to use rawQuery() or query() method.
So for your case, you can use following snippet of code for getting data from database:
String query = "SELECT * FROM actuator";
Cursor c = db.rawQuery(query, null);
if (c != null && c.moveToFirst()) {
// do your stuff
}
Actually, your code do nothing. If you want to retrieve data from db, you need to assign them to Cursor via methods mentioned above.
Problem
I am developing an android application (using Eclipse and 5554 emulator). When I execute the following code it seems CREATE TABLE and INSERT is working correctly but SELECT has some problem. Do you think the problem is coming from CREATE TABLE and INSERT? or Is syntax of SELECT not right? ``` public void onCreateA(SQLiteDatabase db) { System.out.println("On create works"); try { db.execSQL("DROP TABLE IF EXISTS actuator;"); db.execSQL("CREATE TABLE IF NOT EXISTS actuator ("+ "ACTUATOR_ID INT(11) NOT NULL,"+ "ACTUATOR_HW_ID int(11) DEFAULT NULL,"+ "DEVICE_NAME TEXT(45) DEFAULT NULL,"+ "DEVICE_TYPE TEXT(45) DEFAULT NULL,"+ "DEVICE_SUB_TYPE TEXT(45) DEFAULT NULL,"+ "LOCATION_ID INT(11) DEFAULT NULL,"+ "STATUES_VALUE REAL DEFAULT NULL,"+ "MAX_STATUES_VALUE REAL DEFAULT NULL,"+ "MIN_STATUES_VALUE REAL DEFAULT NULL,"+ "PRIMARY KEY (`ACTUATOR_ID`))"); System.out.println("table created now values will be added"); System.out.print("DONE!"); } catch (SQLException e) { System.out.println("table is not created"); } try { db.execSQL("INSERT INTO actuator (ACTUATOR_ID, ACTUATOR_HW_ID, DEVICE_NAME, DEVICE_TYPE, DEVICE_SUB_TYPE, LOCATION_ID, STATUES_VALUE, MAX_STATUES_VALUE, MIN_STATUES_VALUE) " + "VALUES (1, 2, 'Heat Sensor FX615','Heat Sensor', 'Optic Heat Sensor', 4, 6, 7, 8) " ); System.out.println("values are added"); } catch (Exception e) { e.printStackTrace(); System.out.println("values are not added"); } try { db.execSQL("SELECT * FROM actuator"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("not selected"); } } ``` but the output is : ``` 04-11 08:49:24.513: I/System.out(1499): **On create works** 04-11 08:49:24.571: I/System.out(1499): **table created now values will be added** 04-11 08:49:24.627: I/System.out(1499): **DONE!values are added** 04-11 08:49:24.513: I/System.out(1499): **On create works** 04-11 08:49:24.631: W/System.err(1499): android.database.sqlite.SQLiteException: unknown error (code 0): Queries can be performed using SQLiteDatabase query or rawQuery methods only. 04-11 08:49:24.631: W/System.err(1499): at android.database.sqlite.SQLiteConnection.nativeExecuteForChangedRowCount(Native Method) 04-11 08:49:24.631: W/System.err(1499): at android.database.sqlite.SQLiteConnection.executeForChangedRowCount(SQLiteConnection.java:727) 04-11 08:49:24.641: W/System.err(1499): at android.database.sqlite.SQLiteSession.executeForChangedRowCount(SQLiteSession.java:754) 04-11 08:49:24.641: W/System.err(1499): at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:64) 04-11 08:49:24.641: W/System.err(1499): at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1665) 04-11 08:49:24.641: W/System.err(1499): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594) 04-11 08:49:24.641: W/System.err(1499): at com.example.databasetryout.DataBaseTryOut.onCreateA(DataBaseTryOut.java:196) 04-11 08:49:24.641: W/System.err(1499): at com.example.databasetryout.DataBaseTryOut$3.onClick(DataBaseTryOut.java:75) 04-11 08:49:24.641: W/System.err(1499): at android.view.View.performClick(View.java:4204) 04-11 08:49:24.641: W/System.err(1499): at android.view.View$PerformClick.run(View.java:17355) 04-11 08:49:24.641: W/System.err(1499): at android.os.Handler.handleCallback(Handler.java:725) 04-11 08:49:24.641: W/System.err(1499): at android.os.Handler.dispatchMessage(Handler.java:92) 04-11 08:49:24.651: W/System.err(1499): at android.os.Looper.loop(Looper.java:137) 04-11 08:49:24.651: W/System.err(1499): at android.app.ActivityThread.main(ActivityThread.java:5041) 04-11 08:49:24.651: W/System.err(1499): at java.lang.reflect.Method.invokeNative(Native Method) 04-11 08:49:24.651: W/System.err(1499): at java.lang.reflect.Method.invoke(Method.java:511) 04-11 08:49:24.651: W/System.err(1499): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 04-11 08:49:24.651: W/System.err(1499): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 04-11 08:49:24.651: W/System.err(1499): at dalvik.system.NativeStart.main(Native Method) 04-11 08:49:24.651: I/System.out(1499): **not selected** 04-11 08:49:24.651: I/System.out(1499): **finish** ```