I'm trying to run an update statement. The method located in database class is below.
I'm passing the variables to the method correctly and the code that runs without exception. However when I LogCat, the column value does not change. I don't know what or where the problem is...can someone please help me?
public Cursor updateStatus(int ID,String status) {
return database.rawQuery("Update events set status= '"+status+"' where _id=" + ID, null);
}
Use execSQL() instead of rawQuery() to execute SQL like this.
rawQuery() alone just compiles the SQL but does not run it.
Related
I wanna insert some row to a table in Android, I'm using this:
INSERT INTO MyTable (Column_1, Column_1) VALUES ('X',100);
The query runs and no exception is thrown, but when I retrieve all rows from MyTable, no row
is returned.
I do not want to use the insert method, because the queries are read from a file and I want to insert them to the database.
What's wrong with my code?
Update : The rawQuery() method doesn't run the query, but execSQL() does.
The title of your question suggests you're using rawQuery(). It just compiles the SQL but does not run it. Calling one of the moveTo...() methods on the returned Cursor would also execute the SQL.
For an insert query, use execSQL() instead of rawQuery(), even if the documentation incorrectly states it should not be used with INSERT.
How is your Table organized?
You seem to use twice the same column Column_1 is this a typo?
Alternative, you could use insert-command of SQListeDatabase insteat:
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#insert%28java.lang.String,%20java.lang.String,%20android.content.ContentValues%29
We pass so many parameters to query() method of android. Android has simplified our work by giving query method. Is there any way so that i can print the sql query which android will form from this query method arguments which android creates and sends to sqlite.
According to a previous post, I have tried and I got the following solution in order to print the query string in the log.
Use buildQueryString method of SQLiteQueryBuilder. It takes almost same parameters as query() method takes .........
String sqlQry = SQLiteQueryBuilder.buildQueryString(false,TABLE_NAME, null, COLUMN_NAME_PATIENTID +"="+ patientID, null, null, COLUMN_NAME_PATIENTFIRSTNAME, null);
Log.i(TAG, sqlQry);
cursor = db.query(TABLE_NAME, null, COLUMN_NAME_PATIENTID +"="+ patientID, null, null, null, COLUMN_NAME_PATIENTFIRSTNAME);
For what it's worth, if you run under the debugger you can view the private member variable mQuery, which shows you the exact SQL query executed on that cursor - it's handy and can be used on demand without mucking with any code.
Since the query methods are of cursor type, I am not sure whether It will be printed or not.
If you want to debug any query, you can use EXPLAIN QUERY PLAN keyword along with the query or use SQLiteQueryBuilder() or simply run the SQL query by using rawQuery() method.
You can take references from:
http://developer.android.com/reference/android/database/sqlite/SQLiteQueryBuilder.html
http://www.sqlite.org/eqp.html
I verified using SQLite Database Browser that the table contains no rows. I stepped through the query builder code to get the generated query and ran the query in SQLite Database Broswer. The query returned zero rows. But still, the cursor returned by Android's SQLiteQueryBuilder.query method returns true on cursor.moveToFirst() call and returns null values.
Anyone seen something like this before?
OK: I figured this is because I'm using a MAX aggregation function in the query. This could be a bug, may be? I now use a sort with a limit clause instead of MAX and worked around.
I think you should do something like:
if (c.getCount()>0) {
c.moveToFirst();
moveToFirst is actually implemented as moveToPosition(0). moveToPosition is a final method defined in AbstractCursor. Looking at the code, you can see that the result is partially depentandt on getCount. and it seems that in your case getCount returns a non-zero value.
See what's the value of the '_id' column, and try to delete it. Alternatively, try to call that code after re-creating the database (calling 'drop table' and then 'create table').
I have the following code to open a database on the SDCARD and then update a value. If I run the code as is, it will generally run through without throwing an error but nothing gets updated.
If I single step through each line, it works perfectly. I have also tried adding SystemClock.Sleep(2000) in between each statement. Still, it does not run properly unless I single step through this section.
Any ideas?
SQLiteDatabase db = openOrCreateDatabase( sdDIR + "/DBNAME.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
db.execSQL("UPDATE tableName SET value='" + newValue + "' WHERE name='field_name';");
db.close();
From the Android SQLiteDatabase documentation:
public void execSQL (String sql, Object[] bindArgs)
Execute a single SQL statement that is NOT a SELECT/INSERT/UPDATE/DELETE.
You need to use update() or rawQuery().
i created a database with 6 columns and i have a create and update method in my class that takes 6 parameters/arguments which represent these columns. my problem is that, anytime i try to update or create the database without using all 6 arguments (setting some to null), i get an error "constraint failed". this is most particular with the update method.
any ideas how i can get around this? because sometimes i don't want to fill all columns. I have removed the "text not null" constraint when creating the database. Thank you.
You're going to want to use the ContentValues to achieve this. Heres a quick demo.
My function
public boolean updateStuff(int id,ContentValues args) {
return mDb.update(TableName, args, _id_col + "=" + id, null) > 0;
}
And to call it. Note you can put as many ContentValues as you need
ContentValues initValues = new ContentValues();
initValues.put(col_key,col_value);
Edit:
mDB is a SQLiteDatabase