Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increase the value of a record in android/sqlite database

Tags:

android

sqlite

I try to increase the value of an integer key in a row in my table. However, nothing really seems to happen.

db.rawQuery("UPDATE table SET key = key + 1 WHERE name=?", new String[] {name});

However, this code works fine (just sets the key to a hard-coded value):

    ContentValues values = new ContentValues();
    values.put("key", 2);
    db.update("table", values, "name=?", new String[] {name});

Also tried '?' instead of just ?, but it resulted just in a run-time error.

like image 691
nip3o Avatar asked Aug 06 '10 20:08

nip3o


People also ask

How do I edit data in SQLite database?

Introduction to SQLite UPDATE statement First, specify the table where you want to update after the UPDATE clause. Second, set new value for each column of the table in the SET clause. Third, specify rows to update using a condition in the WHERE clause. The WHERE clause is optional.

Which command is used in SQLite to modify the values in the database table?

The ALTER TABLE command in SQLite allows these alterations of an existing table: it can be renamed; a column can be renamed; a column can be added to it; or a column can be dropped from it.

How much data we can store in SQLite?

SQLite database files have a maximum size of about 140 TB. On a phone, the size of the storage (a few GB) will limit your database file size, while the memory size will limit how much data you can retrieve from a query. Furthermore, Android cursors have a limit of 1 MB for the results.

What is content value SQLite?

ContentValues is a maplike class that matches a value to a String key. It contains multiple overloaded put methods that enforce type safety. Here is a list of the put methods supported by ContentValues: void put(String key, Byte value) void put(String key, Integer value)


1 Answers

From http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html:

public Cursor rawQuery (String sql, String[] selectionArgs)

Runs the provided SQL and returns a Cursor over the result set. Returns a Cursor object, which is positioned before the first entry

compared to:

public void execSQL (String sql, Object[] bindArgs)

Execute a single SQL statement that is not a query. For example, CREATE TABLE, DELETE, INSERT, etc.

In other words, SQL queries which return a table are to be run with rawQuery, and those that do not return tables are to be run with execSQL.

like image 157
MPelletier Avatar answered Nov 08 '22 16:11

MPelletier