I have to insert sequential number from 1 to N in an empty column in a database. Android Studio is giving me the following error when I run my code:
Caused by: android.database.sqlite.SQLiteException: unrecognized token: ":" (code 1): , while compiling: SELECT #i:=0; UPDATE table_name SET column_one = #i:=#i+1;
My line of code:
db.execSQL("SELECT #i:=0; UPDATE table_name SET column_one = #i:=#i+1;");
(Please note when I change MySQL statement between parenthesis the code runs and gets executed)
Source of SQL code:
Insert sequential number in MySQL
I'm new in Java, Android Studio and MySQL.
What am I missing here?
Help me to understand why MySQL #i:=#i+1 expression is not running properly in Android Studio.
Thanks in advance.
Because android database is using sqlite,but sqlite dose not supported “#”,if you want batch processing ,I suggest you use Java
Try this .
database.execSQL("SELECT " + #i + " :=0 ; UPDATE table_name SET column_one = " + #i + " := " + #i "+1;");
Related
I'm trying to create an SQLite database in an Android app, but my app keeps crashing and LogCat gives the following error:
FATAL EXCEPTION: main
Process: com.zebra.leadcapture, PID: 15427
android.database.sqlite.SQLiteException: table lead_capture_database has no column named EntryNumber (code 1): , while compiling: INSERT INTO lead_capture_database(EntryNumber,BarcodeData,Notes) VALUES('0','','');
#################################################################
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
(table lead_capture_database has no column named EntryNumber (code 1): , while compiling: INSERT INTO lead_capture_database(EntryNumber,BarcodeData,Notes) VALUES('0','','');)
Here's the code I'm using:
public void addNewEntry(String barcodeDataString, String notesString) {
leadDatabase = openOrCreateDatabase(database_name,MODE_PRIVATE,null);
String create_string = "CREATE TABLE IF NOT EXISTS " + database_name
+ "(EntryNumber TEXT,BarcodeData TEXT,Notes TEXT);";
String insert_string = "INSERT INTO " + database_name + "(EntryNumber,BarcodeData,Notes) VALUES('" + entryCount + "','" + barcodeDataString + "','" + notesString + "');";
leadDatabase.execSQL(create_string);
leadDatabase.execSQL(insert_string);
entryCount++;
}
Why is it not recognizing EntryNumber as a column?
This was answered in the comments above, but for future reference: I deleted the app from my device and reinstalled it through Android Studio which fixed the problem. I had previously been writing the app with a SQLite database without that column but with the same name. I imagine changing the string I was using for the name would have had the same effect.
You added that column after table was created. In this case, that column is not added into database. If you drop the table and recreate it, it will fix your problem without uninstalling the app. Also you can create a method for migration.
i've got a big problem with the android Api.
I'm trying to inset some Values in a SQL-Database and if the key already exist i want to update.
normaly with SQL it's no problem but android throws alwas an error.
my code:
"IF EXISTS (SELECT * FROM "+table+" WHERE Name='"+key+"')" +
" UPDATE "+table+" SET (Value='"+Value+"') WHERE Name='"+key+"'" +
" ELSE" +
" INSERT INTO Table1 (Name,Value) VALUES ('"+key+"','"+Value+"')";
the error i alwas get:
09-19 19:56:46.311: E/AndroidRuntime(32454): Caused by:
android.database.sqlite.SQLiteException: near "CASE": syntax error (code 1): , while compiling: CASE WHEN EXISTS (SELECT * FROM Settings WHERE Name='AppVersion')THEN UPDATE Settings SET (Value='0.5 beta') WHERE Name='AppVersion'ELSE INSERT INTO Table1 (Name,Value) VALUES ('AppVersion','0.5 beta')`
I've also tried to use "CASE" but it doesnt work neither.
"CASE WHEN EXISTS (SELECT * FROM "+table+" WHERE Name='"+key+"')" +
"THEN UPDATE "+table+" SET (Value='"+Value+"') WHERE Name='"+key+"'" +
"ELSE INSERT INTO Table1 (Name,Value) VALUES ('"+key+"','"+Value+"')";
Please can anybody tell me how it works.
PS.: sorry for bad english.
SQLite (obviously) does not support the syntax that you are trying to use.
There are basically two options:
Use SQLite's "ON CONFLICT REPLACE" syntax with an INSERT statement
this requires a unique constraint (in this case, on the 'Name' column).
be aware that this will first delete the old row, and then insert a new one.
If delete/insert is unacceptable to you, then you'll probably have to resort to attempting to update first, get the update count, and if it's zero, do the insert.
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.
I am getting the value from webservice. I am storing in sqlite. While storing in sqlite, I am getting an error. even I replaced single quote with "\'". Which characters are not supported in sqlite?
My error is:
03-26 13:22:22.478: WARN/System.err(311): android.database.sqlite.SQLiteException: near "s": syntax error:
My insert statement is:
myDB.execSQL("INSERT INTO "+TableName
+"("+fieldname+")"
+" VALUES ('"+(homevalue)+"');");
Can anybody tell what to do or give an example?
there is a name which is like "name's" i.e this single quote is problem
Maybe one of your string TableName, fieldname or homevalue contains some quotation. print out the query first and see.
I am just a beginner,
but i think you should write it like this:
"INSERT INTO " + tableName + "(" + feildName + ") VALUES ('" + homeValue + "');"
you put () around the homeValue
I know there is probably a simple thing I'm missing, but I've been beating my head against the wall for the past hour or two. I have a database for the Android application I'm currently working on (Android v1.6) and I just want to insert a single record into a database table. My code looks like the following:
//Save information to my table
sql = "INSERT INTO table1 (field1, field2, field3) " +
"VALUES (" + field_one + ", " + field_two + ")";
Log.v("Test Saving", sql);
myDataBase.rawQuery(sql, null);
the myDataBase variable is a SQLiteDatabase object that can select data fine from another table in the schema. The saving appears to work fine (no errors in LogCat) but when I copy the database from the device and open it in sqlite browser the new record isn't there. I also tried manually running the query in sqlite browser and that works fine. The table schema for table1 is _id, field1, field2, field3.
Any help would be greatly appreciated. Thanks!
Your query is invalid because you are providing 2 values for 3 columns. Your raw query should look like:
sql = "INSERT INTO table1 (field1, field2) " +
"VALUES (" + field_one + ", " + field_two + ")";
although your schema contains three fields. By the way, you can see the log to see the actual error reporting from sqlite.
The right answer is given by the CommonsWare in comments. You should be using execSql() instead of rawQuery(). And it works like a charm.
I thought it would be useful for others, not to have to dig through the comments to find the right answer.
Since it is a string values you forgot "'" to add...this query will surely work, i tested
sql = "INSERT INTO table1 (field1, field2) " +
"VALUES ('" + field_one + "', '" + field_two + "')";
I changed my code to use myDataBase.insert() instead of rawQuery() and it's working. I'm not sure why the actual sql query didn't work though, so if anyone can shed some light on that I'd still appreciate it.