I am trying to update cached_name column of callog.calls table in android,but getting sql lite exception .I have been trying this for long but could not able to come up with anything.I have searched a lot but could not find sufficient information about update statement.kindly help.
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(CallLog.Calls.CONTENT_URI,
null, null, null, null);
ContentValues value=new ContentValues();
value.put("CallLog.Calls.CACHED_NAME","frank");
cr.update(CallLog.Calls.CONTENT_URI,value,CallLog.Calls.CACHED_NAME, null);
Error:
11-25 20:11:17.755: ERROR/Database(106): Error updating CallLog.Calls.CACHED_NAME=frank using UPDATE calls SET CallLog.Calls.CACHED_NAME=? WHERE name
11-25 20:11:17.776: ERROR/DatabaseUtils(106): Writing exception to parcel
11-25 20:11:17.776: ERROR/DatabaseUtils(106): android.database.sqlite.SQLiteException: near ".": syntax error: , while compiling: UPDATE calls SET CallLog.Calls.CACHED_NAME=? WHERE name
The syntax of your call to update is wrong. It needs to look like this:
cr.update(CallLog.Calls.CONTENT_URI,value,null, null);
that will update all the values in the table. To update a specific row do:
cr.update(CallLog.Calls.CONTENT_URI,value,CallLog.Calls.CACHED_NAME +" = ?", new String[]{oldName});
where oldName is the value to use in your WHERE clause.
I will suggest you to go through these links, for better understanding all the concepts of using Sqlite in Android.
http://www.vogella.de/articles/AndroidSQLite/article.html
http://www.devx.com/wireless/Article/40842/1954
Related
I need help how to implement rawquery in sqlite. I need to insert a record in to specific row in table. I searched and found that I can achieve this using raw query. I am new to sqlite and don't know how to implement raw query. I got syntax error.
Here is my code
public void insert (String potision, String total, String curent)
{
SQLiteDatabase db = this.getWritableDatabase();
db.rawQuery("INSERT INTO "+Table_Name2+" VALUES(?,?) WHERE ID = ? ", new String[] {total,curent,potision});
}
Here is my syntax error:
SQLiteException: near "WHERE": syntax error (code 1): , while compiling: INSERT INTO item_counts_2 VALUES(?,?) WHERE ID = ?
Help will be appreciated
You are having a syntax error because you added a where clause to an insert.
When you execute an insert, you are adding a row to the database. There is no sense in specifying a row.
If you were updating a row, then, the where clause would be fine, because you have to tell him what row to update.
Also when deleting rows, the where clause is widely used, so that you don't erase the whole table.
Insert: official docs
Update: official docs
Delete: official docs
Note that the docs refer to SQLite, since you are using Android.
You can use update query with ContentValues to update specific row data in your DB like this
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_1, value1);
values.put(COLUMN_2, value2);
return db.update(TABLE_NAME, values, ID + " = ?",
new String[] { String.valueOf(idValue) });
Hope it will help you out.
Don't use where clause with insert query.
If you are inserting a new row to database then where clause is redundant, remove it and it will work fine.
OR,
If you want to update value already exist in database, use update query instead of insert,
for more information visit here.
I want to write a Where clause for Sqlite db & my query is as follows,
Cursor cursor = database.query(table_name,new String[]{COLUMN_1,COLUMN_2,COLUMN_3}, COLUMN_1='1', null, null, null, null);
//COLUMN_1='1' is my WHERE Clause & its datatype is text
I am not able to execute this query & its giving Nullpointer exception immediately after this statement.
I dont know the reason I think there is some problem with text datatype.
I have spent almost half a day searching for the solution but disappointed.
PS: I've also tried using ,
Cursor cursor = database.query(table_name,new String[]{COLUMN_1,COLUMN_2,COLUMN_3}, COLUMN_1=?, new String[] {'1'}, null, null, null);
LogCat:
But Same problem.
Do you initialize the database? Do you have anything like this:
database = new DBAdapter(this);
Also you need to open the database before the query.
database.open();
I have found the problem, the database was not opened & hence giving NPE. Thank you guys for your patience.
I needed to store some data related to class periods in an Android project. After looking at my options, I thought a SQL database would be a good choice. Unfortunately, I can't seem to get my statement to actually open a database. My open database statement string goes like this:
"create table "+ DATABASE_PERIOD+" (_id integer primary key autoincrement,"
+KEY_CLASSTITLE+" text not null, "+KEY_PERIOD+" text not null,
"+KEY_XPERIODS+" text not null, "+KEY_DOUBLEPERIODS+" text not null);
I based it off of the Notepad project on the Android website. I just added a few more fields, but it is unable to open the database. If you guys want the error message or some other kind of info, I'll try to get it for you (This is my first time with SQL so I don't really know what is needed to fix this up). Thanks in advance!
The error message I'm getting goes like this:
android.database.sqlite.SQLiteException: no such table: periodData: , while
compiling: SELECT DISTINCT _id, title, period, xPeriods, doublePeriods FROM
periodData WHERE _id = -1
And as it says there, I'm using SQLite.
My code to put some data into the table in my Database Helper class:
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_CLASSTITLE, title);
initialValues.put(KEY_PERIOD, period);
initialValues.put(KEY_XPERIODS, xPeriods);
initialValues.put(KEY_DOUBLEPERIODS, doublePeriods);
return mDb.insert(DATABASE_PERIOD, null, initialValues);
I started using private static strings to ensure that my calls aren't incorrect (Thanks Barak for the catch!)
Your issue is the table name (as the error message indicates). From the code you show you created a table called "period". Yet you try to query it using the table name "periodData".
Two ways to fix it:
1) Change the table name in your database to periodData (more difficult as it involves re-creating the db).
2) Change the table name in your query from "periodData" to "period".
Check out the dev topic on http://developer.android.com/guide/topics/data/data-storage.html#db
You'll create a DbHelper class to help you open the database and your code will look something like:
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder);
Hope this was helpful.
I'm having some strange behaviour.
If the database does not exists, and i execute the following code in my Activity:
listOpenHelper = new ListOpenHelper(ManageListActivity.this);
db = listOpenHelper.getReadableDatabase();
Cursor cursor = db.query(ListOpenHelper.TABLE_NAME, null, null, null, null, null, BaseColumns._ID + " DESC");
The database is create and the table LIST is created, no problem here.
The problem is when i try to execute a similiar block in other Activity:
productListOpenHelper = new ProductListOpenHelper(ProductListActivity.this);
db = productListOpenHelper.getReadableDatabase();
Cursor cursor = db.query(ProductListOpenHelper.TABLE_NAME, null, null, null, null, null, ProductListOpenHelper.NAME + " ASC");
In this case, i get the Exception "android.database.sqlite.SQLiteException: no such table: list: , while compiling: SELECT * FROM list ORDER BY _id DESC"
If i erase the database and execute first the above block, and after the first block, the error will be in the productlist table.
I need to create all my tables in the first execution?
I like to create the tables when the user enter in each of the Activities, there is some good way to do this?
Thanks!
You have two different databases, correct? If not, you probably shouldn't have two different helper classes.
Also, creating helpers as you are may not be ideal. See blog post:
http://www.touchlab.co/blog/single-sqlite-connection/
Please post the code for your helper classes and why you have two different classes.
why you are using two different helpers? i don't think its a good way... You can create the tables in the initially and you can obtain the db whenever you want, with a single helper.
As the title says I am having a little trouble with a query on an sqlite database.Basically, what I am trying to do is retrieve a bunch of quotes from the database, depending on the author chosen from user in a list.
But there is a problem with my SELECT statement(it seems..), and I can't seem to spot it, so if any of you good folk could lend a pair of eyes I would really appreciate it.
My query:
public Cursor getQuotes(int position){
String who = authorName[position];
return qmDB.query(QUOTES_TABLE, new String[]{
KEY_QUOTE
},
KEY_AUTHNAME + "=" + who,
null,
null,
null,
null);
}
Error:
04-28 20:05:10.685: ERROR/AndroidRuntime(25017): Caused by: android.database.sqlite.SQLiteException: near "Anton": syntax error: , while compiling: SELECT quote FROM Quotes WHERE auth_name=Robert Anton Wilson
You need single quotes around your "who" string.