0

I referred the following pages but still my problem is not solved,

Android sqlite update table always return 0 , SQLite in Android How to update a specific row , Android SQLite update row not working , Update Function in android SQLite is not working and SQLiteDatabase update not working?

I have a sqlite database as QUESTIONS(ID,QUES,ANS),ID is the primary key. When i try to update a value using following code no change happens in the database.

SQLiteDatabase db = null;
ContentValues values = new ContentValues();
values.put("QUES","New title");
values.put("ANS","Answer" );
long k= db.update("QUESTIONS",values,"ID=2",null);
Log.d("Success",""+k);

The value of k is 1, And there is no change in database. I also tried with

long k= db.update("QUESTIONS",values,null,null);

But no use .

Community
  • 1
  • 1
Sigma Equties
  • 67
  • 2
  • 10

2 Answers2

5

Your first line sets db to be null.

You have no database configured for the UPDATE command to work on.

Kuffs
  • 35,581
  • 10
  • 79
  • 92
  • maybe you have configured a database in the code for that. The reason the above code doesn't work is given in my answer. It is impossible to update a database if the database reference is null. – Kuffs Apr 14 '16 at 14:13
  • i tried but it show message you can accept in 4 minutes – Sigma Equties Apr 14 '16 at 14:18
  • the mistake i did is i copied the insertion code from another class and attached to the new class but missed the code db = openOrCreateDatabase("datas", Context.MODE_PRIVATE, null); – Sigma Equties Apr 14 '16 at 14:20
0

My problem solved

db = openOrCreateDatabase("data", Context.MODE_PRIVATE, null);
Sigma Equties
  • 67
  • 2
  • 10