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.
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.
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.
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.
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)
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With