getting sqLiteException when useing getContentResolver().query() - 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.

Related

ContentProvider (contacts) - no such column: metadata_dirty

I'm trying to get all contacts of a specific type from the phone like following:
Cursor cursor = context.getContentResolver().query(
ContactsContract.RawContacts.CONTENT_URI,
null,
ContactsContract.RawContacts.ACCOUNT_TYPE + "='com.whatsapp'",
null,
ContactsContract.RawContacts.CONTACT_ID + " ASC");
But this line already throws an exception (custom rom, nougat => maybe it's related to this?). I only got this error from one user yet and I'm stuck here, does anyone know how to solve that? Is there an alternative way to query all contacts?
My exception looks like following:
Exception: android.database.sqlite.SQLiteException: no such column: metadata_dirty (code 1): , while compiling:
SELECT sort_key, send_to_voicemail, pinned, display_name, metadata_dirty,
phonebook_label_alt, version, phonebook_bucket, _id, custom_ringtone,
times_contacted, account_type_and_data_set, sync4, dirty, sync2,
contact_id, raw_contact_is_user_profile, aggregation_mode, data_set,
phonebook_label, account_type, sync3, display_name_alt, phonetic_name,
last_time_contacted, display_name_source, backup_id, sort_key_alt,
phonebook_bucket_alt, deleted, starred, account_name, sync1, sourceid,
phonetic_name_style
FROM view_raw_contacts_restricted AS view_raw_contacts
WHERE (1)
AND ((account_type='com.whatsapp'))
ORDER BY contact_id ASC
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java)
at android.content.ContentProviderProxy.query(ContentProviderNative.java)
at android.content.ContentResolver.query(ContentResolver.java)
at android.content.ContentResolver.query(ContentResolver.java)
...
I don't see anything wrong with your query, it seems like this is an internal bug in the user's custom ROM.
METADATA_DIRTY is a new column in Android N:
https://developer.android.com/reference/android/provider/ContactsContract.RawContactsColumns.html#METADATA_DIRTY
So it seems like the system is Nougat, but the Contacts DB is based on an older version of Android.

sqlite exception error SELECT command

Caused by: android.database.sqlite.SQLiteException: near "FROM": syntax error (code 1): , while compiling: SELECT _id, FROM TRACKS WERE _id=9
String test = "SELECT _id, FROM TRACKS WERE _id="+"9";
Cursor cursor = database.rawQuery(test, null);
Did not see the Point :( any help
Maybe there is a error in my Statement but it didn't work
No comma after id and WERE should be WHERE.
Also consider using [rawQuery()](http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#rawQuery(java.lang.String, java.lang.String[], android.os.CancellationSignal)] with selectionArgs to avoid having to concatenate query and values as you did in +id=+9.
SELECT _id, FROM TRACKS WERE _id="+"9"
'WERE' should be WHERE

Error: No such column name

I have been trying to execute a query:-
String selectQuery="SELECT "+ROLE+" FROM "+TABLE_EMPLOYEE+ " WHERE "+USER_ID+ "='"+userId+"' AND "+PASSWORD+"='"+password+"';";
cursorObj = dbObj.rawQuery(selectQuery, null);
This will result to:-
SELECT Role FROM employee WHERE User_Id='HondaSE' AND Password='456';
The logcat says:-
01-08 12:05:10.070: W/System.err(9318): android.database.sqlite.SQLiteException: no such column:
HondaQE: , while compiling: SELECT Role FROM employee WHERE User_Id=HondaQE AND Password=123;
I have tried to run the query with double quotes as well, for userId and password. resulting in:-
SELECT Role FROM employee WHERE User_Id="HondaSE" AND Password="456";
However both the queryies work perfectly fine when executed in SQLITE Data browser.
Both respond with same error.
Your logcat shows as below.
while compiling: SELECT Role FROM employee WHERE User_Id=HondaQE AND Password=123;
there is no single quotes around strings in User_Id=HondaQE AND Password=123.
You weren't compiling your raw query correctly. I would advise you to use "query" instead and use selection args to avoid these mistakes and possible sql injections.
Rewrite like this
String selectQuery="SELECT "+ROLE+" FROM TABLE_EMPLOYEE " WHERE USER_ID ='" + userId + "' AND PASSWORD = '" +password+ "'";
I had the closed the db before accessing the cursor. This was the error.
After accessing the cursor, the db is to be closed. Error solved.

SQL open a table confusion

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.

Android SQlite query problem

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.

Categories

Resources