5

I want to increment a column in a sqlite android database. Im doing like this:

public void atualiza(String word){
   this.db.rawQuery("UPDATE words SET count = count + 1 WHERE word= ? ", new String[] {word});     
}

And I call this method on another class like this:

this.dh.atualiza(word); 

But when i pull the database from the emulator and check with the SQLite Database Browser, the field is not incremented. Why?

Czechnology
  • 14,832
  • 10
  • 62
  • 88
rogcg
  • 10,451
  • 20
  • 91
  • 133
  • how about if you do a select from within your code (select count from words where word=?), does it return the correct value? (are you definitely pulling the right database?) – Mathias Conradt Aug 22 '10 at 05:45
  • 1
    I think `rawQuery` returns an error code. Can you check what it is? Also, updates should be made with execSQL. – MPelletier Aug 22 '10 at 17:21

2 Answers2

4

You should really use execSQL for a query that does not return a table: Increase the value of a record in android/sqlite database

Community
  • 1
  • 1
MPelletier
  • 16,256
  • 15
  • 86
  • 137
0

You need to start a transaction on the database Data not inserting in Sqlite database in android

Community
  • 1
  • 1
Konstantin Burov
  • 68,980
  • 16
  • 115
  • 93
  • I should point out that when transactions are not specified, SQLite makes its own. – MPelletier Aug 22 '10 at 17:20
  • Hmm.. do you know good android docs about that feature? I'd highly appreciate a link. – Konstantin Burov Aug 22 '10 at 17:22
  • It's a feature of SQLite, not so much as Android, but here is the official doc: http://sqlite.org/lang_transaction.html : "No changes can be made to the database except within a transaction. Any command that changes the database (basically, any SQL command other than SELECT) will automatically start a transaction if one is not already in effect. Automatically started transactions are committed when the last query finishes." – MPelletier Aug 22 '10 at 20:51
  • Its ok. The problem was change from rawQuery to execSQL. Thanks a lot! – rogcg Aug 22 '10 at 21:30
  • But how do you get the number of affected rows then? – Marcus Wolschon Sep 05 '11 at 10:52