Android SQLiteException parameter - android

I'm working very well with the SQLite database, but now I have a problem while using a query.
For example, if use a query like this:
SQLiteDatabase db = mHelper.getReadableDatabase();
String parameter = mypar;
String sql = SELECT color, name FROM table WHERE name = '" + parameter + "';
Cursor c = db.rawQuery(sql, null);
while (c.moveToNext()) {
String color = c.getString(0);
String name = c.getString(1);
}
c.close();
db.close();
Everything works fine
But if the parameter has an apex
String parameter = mypar';
I get an exception
Caused by: android.database.sqlite.SQLiteException: near "r": syntax error (code 1):
How can I solve this problem? Thank you

this is my solution, works fine!! try it
String parameter = mypar';
String sql = SELECT color, name FROM table WHERE name = ?;
Cursor c = db.rawQuery(sql, new String[] { parameter });

Related

Error executing select query in sqlite by android

I have write this code for select filtered data from sqlite db through android. But it says there are some error. Can you help me to correct my code please.
category = data[0];
district = data[1];
SQLiteDatabase db = openOrCreateDatabase("dbumbers", 1, null);
Cursor c = db.rawQuery("SELECT * FROM numbers where [category = '"+category+"'] and [district = '"+district+"']", null);
You should use this:
Cursor c = db.rawQuery("SELECT * FROM numbers where category = '"+category+"' and district = '"+district+"'", null);
You must eliminate [] from your query

How to insert a text with single quots to sqlite db in android?

I am facing an issue in inserting an issue in inserting a text with single quotes in sqlite db in android. it is returning the following error.
06-21 15:21:55.644: E/AndroidRuntime(16328): android.database.sqlite.SQLiteException: near
"re": syntax error (code 1): , while compiling: select * from tbl_chatHistory where
chat_date = '1.403344315631E12' and chat_time = '1.403344315635E12' and chat_text =
'you're'
Please help.
**************** EDITED *************************
public boolean checkChatExists(String date, String time, String chat) {
boolean result = false;
String selectQuery = "select * from tbl_chatHistory where chat_date = '"+date+"'
and chat_time = '"+time+"' and chat_text = '"+chat+"'";
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
result = true;
} else {
result = false;
}
cursor.close();
return result;
}
In SQL string literals you can escape ' as ''.
It however better to use parametrized queries. Replace the string literals with ? and provide the values in the bindArgs array. For example:
String selectQuery = "select * from tbl_chatHistory where chat_date = ? and chat_time = ? and chat_text = ?";
Cursor cursor = database.rawQuery(selectQuery, new String[] { date, time, chat });
Use ` instead of '. The problem is that the second ' is recognised as the end of the text.

rawQuery in android

I have a database in my application. I want to select specific rows.
here is my query string :
SQLiteDatabase db = this.getReadableDatabase();
String C_PRODUCT_CATEGORY_ID = category_id;
String TABLE_PRODUCT = tblProduct;
String categoryID = 1;
String select = String.format("SELECT %s FROM %s WHERE %s=%s",
C_PRODUCT_CATEGORY_ID, TABLE_PRODUCT, C_PRODUCT_CATEGORY_ID,
categoryID);
Cursor c = db.rawQuery(select,new String[]{});
the cursor c is empty! what when i use the .db file on device with sqlite editor and run
SELECT category_id FROM tblProduct WHERE category_id=1
it returns 2 rows.
whats wrong? please help me!
Use database's query method as,
Cursor c = db.query(TABLE_PRODUCT, null, C_PRODUCT_CATEGORY_ID+"=?", new String[]{categoryID}, null, null, null);
Hope this works well for you !!
SQLiteDatabase db = this.getReadableDatabase();
String categoryID = 1;
String select = "SELECT category_id FROM tblProduct WHERE category_id="+categoryID;
Cursor c = db.rawQuery(select,new String[]{});
Just use this
It could be that you need to put the ' and ' after "="; I mean:
String select = String.format("SELECT %s FROM %s WHERE %s='%s'",
C_PRODUCT_CATEGORY_ID, TABLE_PRODUCT, C_PRODUCT_CATEGORY_ID,
categoryID);
Cursor c = db.rawQuery(select,null);
try this
Cursor c = dB. rawQuery (select, null);

How to use th SUM function to return the values from sqlite DB in android?

In my application, store the amount details of company in my sq lite DB.For example om my first position of DB in name column-company 1,total column - 400,second position of DB in name column-company 2,total column - 800,third position of DB in name column-company 1,total column - 500.how Sum the company 1 details only to return the total amount.
My main coding is,
String str = db.company_amount("Company 1");
Log.v("Total", ""+str);
My DB coding is,
String company_amount(String name){
SQLiteDatabase db = this.getReadableDatabase();
String s = "";
Cursor cursor = db.rawQuery("SELECT SUM(KEY_AMOUNT) FROM TABLE_BILL_DETAILS WHERE = ?", new String[] {String.valueOf(name)});
if (cursor != null) {
if (cursor.moveToNext()) {
s = cursor.getString(1);
return cursor.getString(1);
}
cursor.close();
}
return s;
}
It shows some error,I don't know how to return the values.Can any one know please help me to solve this problem.
My Logcat Error
04-25 14:54:06.701: E/AndroidRuntime(2776): FATAL EXCEPTION: main
04-25 14:54:06.701: E/AndroidRuntime(2776): java.lang.RuntimeException: Unable to start activity
ComponentInfo{invoicebill.details/invoicebill.details.Total_company_details}: android.database.sqlite.SQLiteException: near "=": syntax error: , while compiling: SELECT SUM(KEY_AMOUNT) FROM TABLE_BILL_DETAILS WHERE = ?
SQLiteDatabase db = getReadableDatabase();
String sql = "SELECT SUM(KEY_AMOUNT) FROM TABLE_BILL_DETAILS WHERE name = ?";
long sum = android.database.DatabaseUtils.longForQuery(db, sql, new String[]{name});
WHERE = ?
This is incorrect SQL syntax. You forgot to specify which field you're comparing with the argument. I guess it's name or something similar, so correct syntax would be something like:
WHERE name = ?
you can use to get details using id like this.
public int getTotalOfAmount(int id) {
odb = dbh.getReadableDatabase();
Cursor c = odb.rawQuery("SELECT SUM(" + KEY_AMOUNT + ") FROM " + DATABASE_TABLE + " WHERE " + KEY_ID + " = " + id, null);
c.moveToFirst();
int i = c.getInt(0);
c.close();
return i;
}
.

No result on sqlite query

Hi all
I create a table like that
private static final String CREATE_TABLE_PRODUCT =
"create table prod (id integer primary key,titre text not null, desc text, is_free integer);";
i update it this way
SQLiteDatabase db = getConnection();
ContentValues updateEvent = new ContentValues();
updateEvent.put("is_free", 1);
int ok = db.update(prod, updateEvent, "id=?",
new String[] { Long.toString(evenement.getId()) });
db.close();
I do see the change in eclipse with DDMS questoid
but when i try to retrieve the value I get nothing ....
Cursor c = db.query(prod, new String[] { "id",
"titre", "desc", "is_free", },
"is_free=?", new String[] {"1"}, null, null,
null);
I've tried some variant like
String query = "SELECT * FROM "+EVENEMENT_TABLE+" WHERE is_free=1;";
Cursor c = sdb.rawQuery(query, null);
with no success
is this problem come from the type (integer) of my column ?
Any help
Do not use reserved keywords like desc in your queries. This probably causes query to fail.
In case you don't know update will not add values to the table. Use insert instead.

Categories

Resources