Reputation: 8145
when trying to update my database through queries that I get from an internet page, I get an exception.. Here is the code:
url = new URL(Database.Munawwat_UpdateDB_URL);
URLConnection connection = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line = null;
File dbfile = new File(Database.Munawwat_Database_PATH);
if (!dbfile.exists()) {
Toast.makeText(Munawwat_List.this, "error in db location", Toast.LENGTH_SHORT).show();
return;
}
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
String query = "";
while ((line = reader.readLine()) != null)
if (!line.contains("WHERE"))
query += line + "\n";
else {
query += line.trim();
Cursor c = db.rawQuery(query, null);
c.moveToFirst();
c.close();
query = "";
}
db.close();
reader.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Logcat:
14:52:46.136: E/SQLiteQuery(17802): exception: not an error; query: UPDATE Adab SET Body="AAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBCBCBCB" WHERE _id=4 10-14
14:52:47.484: W/System.err(17802): android.database.sqlite.SQLiteException: not an error 10-14
14:52:47.496: W/System.err(17802): at android.database.sqlite.SQLiteQuery.nativeFillWindow(Native Method) 10-14
14:52:47.503: W/System.err(17802): at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:86) 10-14
14:52:47.511: W/System.err(17802): at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:164) 10-14
14:52:47.523: W/System.err(17802): at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:156) 10-14
14:52:47.527: W/System.err(17802): at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:161) 10-14
14:52:47.527: W/System.err(17802): at android.database.AbstractCursor.moveToFirst(AbstractCursor.java:201) 10-14
14:52:47.531: W/System.err(17802): at omar.Munawwat_List$2.onClick(Munawwat_List.java:141) 10-14
14:52:47.531: W/System.err(17802): at android.view.View.performClick(View.java:3511) 10-14
14:52:47.535: W/System.err(17802): at android.view.View$PerformClick.run(View.java:14105) 10-14
14:52:47.535: W/System.err(17802): at android.os.Handler.handleCallback(Handler.java:605) 10-14
14:52:47.535: W/System.err(17802): at android.os.Handler.dispatchMessage(Handler.java:92) 10-14
14:52:47.539: W/System.err(17802): at android.os.Looper.loop(Looper.java:137) 10-14
14:52:47.539: W/System.err(17802): at android.app.ActivityThread.main(ActivityThread.java:4575) 10-14
14:52:47.539: W/System.err(17802): at java.lang.reflect.Method.invokeNative(Native Method) 10-14
14:52:47.539: W/System.err(17802): at java.lang.reflect.Method.invoke(Method.java:511) 10-14
14:52:47.542: W/System.err(17802): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 10-14
14:52:47.542: W/System.err(17802): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 10-14
14:52:47.546: W/System.err(17802): at dalvik.system.NativeStart.main(Native Method)
So the database doesnt get updated.. Why is this?
Upvotes: 0
Views: 4023
Reputation: 14786
It looks like your query contains a string constant enclosed in double quotes "
. It needs to be enclosed in single quotes '
.
Edit: Barak's answer points out the main problem. You should use execSQL instead of rawQuery, because you are not expecting a cursor back.
Upvotes: 1
Reputation: 16393
You are trying to use the rawQuery
to do an update, you should not do that (or you will get exceptions). rawQuery
expects the DB to return a cursor to it, and an update does not do that, so you get an exception.
You should to use the update
convenience method or the execSQL
method to do updates as they don't require a cursor to be returned.
Upvotes: 0