Shreyash Mahajan
Shreyash Mahajan

Reputation: 23596

Android: Issue with updating the table field

I am wonder about how to update the table.

I have use below two method to update the Table.

Which one is right one and Whats wrong in it ? As i got the Exception while use both.

Code:

//First One For Update
public boolean setFavouriteBook(String id){
    String query = "update Articles set type = 0 Where address = '"+id+"'";
    myDataBase.rawQuery(query, null);
    return true;
}

// Second One for Update
//---updates a title---
public boolean setFavourite(String address) 
{
    ContentValues args = new ContentValues();
    args.put("type", 0);

    return myDataBase.update("Articles", args, "address = '"+address+"'", null) > 0;
}

And the Log is:

    02-08 10:29:56.010: INFO/Database(10350): sqlite returned: error code = 8, msg = prepared statement aborts at 28: [UPDATE Articles SET type=? WHERE address = '3']
02-08 10:29:56.010: ERROR/Database(10350): Error updating type=0 using UPDATE Articles SET type=? WHERE address = '3'
02-08 10:29:56.021: DEBUG/AndroidRuntime(10350): Shutting down VM
02-08 10:29:56.021: WARN/dalvikvm(10350): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
02-08 10:29:56.041: ERROR/AndroidRuntime(10350): FATAL EXCEPTION: main
02-08 10:29:56.041: ERROR/AndroidRuntime(10350): android.database.sqlite.SQLiteException: error code 8: attempt to write a readonly database
02-08 10:29:56.041: ERROR/AndroidRuntime(10350):     at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
02-08 10:29:56.041: ERROR/AndroidRuntime(10350):     at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:55)
02-08 10:29:56.041: ERROR/AndroidRuntime(10350):     at android.database.sqlite.SQLiteDatabase.updateWithOnConflict(SQLiteDatabase.java:1692)
02-08 10:29:56.041: ERROR/AndroidRuntime(10350):     at android.database.sqlite.SQLiteDatabase.update(SQLiteDatabase.java:1622)
02-08 10:29:56.041: ERROR/AndroidRuntime(10350):     at com.project.SkadebogenApp.DataBaseHelper.setFavourite(DataBaseHelper.java:417)
02-08 10:29:56.041: ERROR/AndroidRuntime(10350):     at com.project.SkadebogenApp.CommonWebViewActivity.onClick(CommonWebViewActivity.java:204)
02-08 10:29:56.041: ERROR/AndroidRuntime(10350):     at android.view.View.performClick(View.java:2408)
02-08 10:29:56.041: ERROR/AndroidRuntime(10350):     at android.view.View$PerformClick.run(View.java:8816)
02-08 10:29:56.041: ERROR/AndroidRuntime(10350):     at android.os.Handler.handleCallback(Handler.java:587)
02-08 10:29:56.041: ERROR/AndroidRuntime(10350):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-08 10:29:56.041: ERROR/AndroidRuntime(10350):     at android.os.Looper.loop(Looper.java:123)
02-08 10:29:56.041: ERROR/AndroidRuntime(10350):     at android.app.ActivityThread.main(ActivityThread.java:4627)
02-08 10:29:56.041: ERROR/AndroidRuntime(10350):     at java.lang.reflect.Method.invokeNative(Native Method)
02-08 10:29:56.041: ERROR/AndroidRuntime(10350):     at java.lang.reflect.Method.invoke(Method.java:521)
02-08 10:29:56.041: ERROR/AndroidRuntime(10350):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-08 10:29:56.041: ERROR/AndroidRuntime(10350):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-08 10:29:56.041: ERROR/AndroidRuntime(10350):     at dalvik.system.NativeStart.main(Native Method)
02-08 10:29:56.071: WARN/ActivityManager(59):   Force finishing activity com.project.SkadebogenApp/.CommonWebViewActivity
02-08 10:29:56.600: WARN/ActivityManager(59): Activity pause timeout for HistoryRecord{450b78f8 com.project.SkadebogenApp/.CommonWebViewActivity}

Upvotes: 0

Views: 1169

Answers (3)

Lalit Poptani
Lalit Poptani

Reputation: 67286

If your database is in SDCard.

If you have your database in SDCard, so you have to give WRITE_EXTERNAL_STORAGE permission in your Manifest file. You can check this answer for the same problem.

Else you can use

SQLiteDatabase.OPEN_READWRITE instead of SQLiteDatabase.OPEN_READONLY while you are opening your database.

 checkDB = SQLiteDatabase.openDatabase(myPath, null,
                                                SQLiteDatabase.OPEN_READWRITE);

Upvotes: 3

deepak Sharma
deepak Sharma

Reputation: 1641

Hey the error show in your log file that you open your DB in Readable mode and here you are writing something in database so just getWritableDatabase(); use this . may be it will helpfull to you
or can you take WRITE_EXTERNAL_STORAGE permission.

Thanks

Upvotes: 2

Lucifer
Lucifer

Reputation: 29672

I am not 100% sure, but, you can not use address as column name, it reflect with some keyword in database. just try renaming that field with address1 and then execute your query.

Upvotes: 0

Related Questions