Android - DatabaseUtils.sqlEscapeString() does not work - android

I want to insert "women's cloth" in the database, so I use
String name = DatabaseUtils.sqlEscapeString("women's cloth");
but it gives out put like
"'Women''s Clothing'"
and while inserting app crashes and gives
E/SQLiteLog: (1) near ",": syntax error
--------- beginning of crash -----------------
07-12 12:42:19.628 22387-22521/com.i4ustores E/AndroidRuntime: FATAL EXCEPTION: Thread-8634
android.database.sqlite.SQLiteException: near ",": syntax error (code 1):

From your simple code, I guess you create SQL statement manually.
Try using String.format:
String query = String.format("SELECT * FROM %s WHERE %s = ?",
TABLE_NAME, SEARCH_KEY);
Read more at: Local Databases with SQLiteOpenHelper
Or try using PreparedStatement like below:
PreparedStatement pstmt = con.prepareStatement("UPDATE EMPLOYEES
SET SALARY = ? WHERE ID = ?");
pstmt.setBigDecimal(1, 153833.00)
pstmt.setInt(2, 110592)
Please be aware that the parameterIndex (i.e 1 and 2 in above code) start from 1.

Related

SQLiteException syntax error on line breaks trying to read dump file from assets

I'm developing an Android app which in I have a SQLite dump file in assets and I'm trying to create a database in user's phone using that dump.
The problem is it's a long dump file containing sql command including line breaks like this:
CREATE TABLE [tblType] (
[IdType] INTEGER NOT NULL ON CONFLICT ROLLBACK PRIMARY KEY ON CONFLICT ROLLBACK AUTOINCREMENT,
[Type] NVARCHAR);
So when I try to create the database using SQLiteOpenHelper:
SQLiteOpenHelper openHelper0 = new DbHelper(this);
Cursor cu = null;
try {
cu = openHelper0.getReadableDatabase().rawQuery("select IdWord,Word from tblWord", null);
} catch (Exception e) {
e.printStackTrace();
}
I get syntax error on every line including a line break:
android.database.sqlite.SQLiteException: near "(": syntax error (code
1): , while compiling: CREATE TABLE [tblType](
And if I remove the first line break I get:
android.database.sqlite.SQLiteException: near ",": syntax error (code
1): , while compiling: CREATE TABLE [tblType]([IdType] INTEGER NOT
NULL ON CONFLICT ROLLBACK PRIMARY KEY ON CONFLICT ROLLBACK
AUTOINCREMENT,
As I said, it's a large dump and I can't manually remove all linebreaks; So the question is is there any way like configurations or extra code to make Android ignores the line breaks?
This is the main code from DBHelper:
#Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
try {
InputStream is = activity.getAssets().open("am.sql");
Scanner scanner = new Scanner(is).useDelimiter("\n");
while (scanner.hasNext()) {
sqLiteDatabase.execSQL(scanner.next());
}
is.close();
} catch (IOException ex){
}
}
You are reading the file line by line. This does not work for SQL statements having multiple lines.
You can use a semicolon at the end of a line to detect the actual end of statements, if you do not have any SQL statements that contain embedded semicolons (triggers, or text values with a semicolon before a newline).

getting error : Caused by: android.database.sqlite.SQLiteException: near "TABLENAME" syntax error (code 1) compiling a SELECT query

I'm getting the following error while compiling a SELECT query:
Caused by: android.database.sqlite.SQLiteException: near "group": syntax error (code 1): , while compiling: SELECT group_id, logo FROM group WHERE group_name = 'Empty Group'
The query is created as:
c = database.query(TABLE_GROUP, new String[]{KEY_GROUPID, KEY_LOGO}, KEY_GROUPNAME + " = '" + description + "'", null, null, null, null);
with:
TABLE_GROUP = "group";
KEY_LOGO = "logo";
KEY_GROUPID = "group_id";
and the creation script for the table:
create table group
(group_id integer primary key autoincrement,
group_name text not null,
logo string);
Anyone knows what's wrong?
group is a SQLITE keyword and, as every reserved words, it can't use it as table/column name. To fix chose another name for your column. You can find a list of the SQLITE reserved keywords here

how to retrive image from database in android

I tried using this code to retrieve the image from database which is stored in blob format
but this method does not work and my application crashes whenever this method is called
so how can I retrieve my image in byte or bitmap format from the database.
because I want to use that imageswitcher
My logcat shows
07-26 18:12:51.253: E/string1(15682): --Hatheesingh jain temple
07-26 18:12:51.844: E/SQLiteLog(15682): (1) near "jain": syntax error
07-26 18:12:51.884: E/AndroidRuntime(15682): FATAL EXCEPTION: main
07-26 18:12:51.884: E/AndroidRuntime(15682): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.starting.starting/com.starting.starting.detailpage}: android.database.sqlite.SQLiteException: near "jain": syntax error (code 1): , while compiling: SELECT * FROM database WHERE place_name =Hatheesingh jain temple
07-26 18:12:51.884: E/AndroidRuntime(15682): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
07-26 18:12:51.884: E/AndroidRuntime(15682): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
public byte[] get_image(String str){
//openDataBase();
Cursor c1 = checkDB.rawQuery("SELECT * FROM database WHERE place_name =" +str, null);
byte[] img = c1.getBlob(10);
return img;
//CloseDataBase();
}
After looking at your LogCat, it's SQL syntax error in your Query. You haven't wrapped the the string in single quotes so it is SQL Compile error, as clearly mentioned in LogCat
Change
Cursor c1 = checkDB.rawQuery("SELECT * FROM database WHERE place_name =" +str, null);
To
Cursor c1 = checkDB.rawQuery("SELECT * FROM database WHERE place_name ='" + str + "'", null);
Besides this, also call c1.moveToFirst() to avoid CursorIndexOutOfBoundsException
database is a reserved keyword in SQLite. You can not use it as a table name
Look at this SQLite reserved keywords
Also you need to call
c1.moveToFirst();
before
byte[] img = c1.getBlob(10);
or else you will get an CursorIndexOutOfBoundsException.

syntax error near "," while compiling select

Anyone con find the error in these query?
String[] column = {"titolo","regista","cast","descrizione"};
String selection = "_id_film = ? ";
String[] selection_args = {Integer.toString(selectedfilm+1)};
Cursor cursor = db.query("T_FILM", column, selection,selection_args, null, null, null);
logcat says:
android.database.sqlite.SQLiteException: syntax error near"," (code 1) :, while compling SELECT titolo,regista,cast,descrizione FROM T_FILM WHERE _id_film=?
"Cast" is a reserved operation in SQLLite. So it's trying to interpret the column name "cast" as an operation. Your easiest answer is to change the column name to something else.

error in rawquery :near ","

while trying to insert data i am getting this error
07-29 18:15:19.909: E/sqlite(863): android.database.sqlite.SQLiteException: near ",": syntax error: , while compiling: INSERT INTO transactions (details , amount) VALUES( , );
i think the error is in this line unable to sort it help anyone
String insertQuery = "INSERT INTO " +stdb.tname+" (" + stdb.details+" , "+stdb.amt+") VALUES("+setdet+" , "+setamt+");";
Seems like setdet and setamt are empty. which results in the invalid statement:
INSERT INTO transactions (details , amount) VALUES( , );

Categories

Resources