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.
Related
I would like to modify contact's name in an Android device (GT N5110) with Android 4.4. I have tried to do that by this approach:
ContentValues contentValues = new ContentValues();
String selection = ContactsContract.Contacts._ID + " = ? " ;
String[] selectionArgs = new String[] { Integer.toString(id) };
contentValues.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, lastName);
contentValues.put(ContactsContract.Contacts.STARRED, 2); // *
contentValues.put(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, lastName);
contentValues.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, firstName);
return this.context.getContentResolver().update(ContactsContract.Contacts.CONTENT_URI,
contentValues, selection, selectionArgs);
The line marked with * was the only one which modified the contact database. I also tried by applyBatch and added the MIMETYPE selection (ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE). The MIMETYPE approach throws some Exception with these message:
android.database.sqlite.SQLiteException: no such column: mimetype (code 1): , while compiling: SELECT _id FROM view_contacts_restricted WHERE _id = ? AND mimetype=?
I researched and tried suggestions present in update contacts display_name and Modifying contact information.
Can someone help me?
The following website explains it to you. It is a full tutorial by android itself. I highly recommend you use this website to first check if there is an answer to your query first before coming here.
http://developer.android.com/reference/android/provider/ContactsContract.Intents.html#EXTRA_FORCE_CREATE
What this website explains to you is
Send data to the contact you require to edit and it automatically opens the contact edit screen with the details which you passed to it.
If you want to know what all extras you can send to it. Visit the following link.
https://developer.android.com/reference/android/provider/ContactsContract.Intents.Insert.html
I am not sure if this is correct but it may be because of some security policy or so that you can't skip ui confirmation. But again I am not familiar with this so I may be wrong. I have not tested if this works or anything but I think it might help. Especially the second one:
http://developer.android.com/reference/android/provider/ContactsContract.Intents.html#EXTRA_FORCE_CREATE
And
http://www.techjini.com/blog/2011/10/31/insert-and-modify-contact-in-android/
I'm trying to get data from sqlite database.Tried using thiscontext.getContentResolver().query() method and I got following SqlException
near ":23": syntax error (code 1): , while compiling: SELECT wol_wait, mac_addr, _id, address, name, wol_port, user, timeout, pass FROM hosts WHERE (mac_addr=00:23:15:97:ce:a0) ORDER BY name ASC
this is the code that I’m using
Cursor c=context.getContentResolver().query(HostProvider.Hosts.CONTENT_URI,null, Hosts.MAC_ADDR +"=" +String.valueOf(mhost.mac_addr),null,HostProvider.Hosts.NAME + " ASC");
how can I fix this issue?
String literals such as MAC addresses need to be in 'single quotes', or better yet, use ? placeholders and variable binding. Replace
Hosts.MAC_ADDR +"=" +String.valueOf(mhost.mac_addr),null
with
Hosts.MAC_ADDR +"=?", new String[] { String.valueOf(mhost.mac_addr) }
The String.valueOf is possibly not needed.
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 have a listview populated from an SQLite database. I have several items that I successfully populate into the listview, however I'm having trouble with one last thing.
I'm trying to queue the sum total of the column KEY_CONTENT6 which is a string type, however it only contains numbers. I'd like to keep it as a string, so to add it up I'm using Double.valueOf(). The problem is this code force closes on queue and I cant figure out whats wrong:
public Cursor queueAll(){
String[] columns =
new String[]{KEY_ID, "sum("+ Double.valueOf(KEY_CONTENT6) +")",
KEY_CONTENT9, KEY_CONTENT10 };
Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns,
null , null, KEY_CONTENT10, null, KEY_CONTENT9+ " DESC");
return cursor;
}
simply use SUM, no need to use anything else..
String[] columns =
new String[]{KEY_ID, "sum(KEY_CONTENT6)",
KEY_CONTENT9, KEY_CONTENT10 };
It is valid for SQLite. Because, no matter what you set data type in SQLite, it stores values as string. So, type conversion is somewhat built-in in SQLite.
You can't use java in a SQL statement, either stick to strait sql or iterate over the cursor and use java to do your calculation.
You can find everything there is to know about sqlite here http://www.sqlite.org/docs.html
SQLite is basically typeless, so you might be able to use SUM on your column even though it is a string. However, if it's meant to be a numeric column, why not give it a number type??
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