I cant a able to insert apostrophe character in sqlite . I tried this way what many says in forum is replace " ' " with " ' ' "
String replace=replaceString.replace("'"," ''") but after using this method too gives insert error.
I like to use
String replace = replaceString.replace("'","\'");
This will insert the apostrophe into the table.
Are you looking to use ' in a string?
If so you can do this
"insert into my_table set myvalue = 'My dog\'s bone' where id = 5"
Related
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;");
I am using this query to fetch data from sqlite but only one condition is working but not two at same time.
I am also getting some notification that no such column but it exist.
String lquery = "SELECT SUM(totalcalorie) AS LTotal FROM fooditem
WHERE name = ('" + fname + "') AND foodtype = 'Lunch'";
you might be having null values on those places which you are checking while calling..
go through your code again queries seems write.
change totalcalorie to INTEGER type in the database schema
comment says that you are using TEXT
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 trying to learn the SQL Database stuff for SQLite using the android. I have seen a couple examples of the Queries....
I have a two part question about sqlite queries in android.
Part 1
Say I want to delete something. and I use the following Query.
db.delete(MY_DB_TABLE, "CustomerName = ?", new String[] { customerName });
what would happen if the Customer name had a bad character in it.
For example. If I use the following Query
db.execSQL("delete from " + MY_DB_TABLE +
" where customername = '" + customerName + "';");
and say for this example the name of my customer was "Arby's".
That query would blow up because the ' is a special character and the query would not be formatted correctly.
Part 2
does this format allow me to specify as many paramaters as I want.
Example:
db.delete(MYTABLE, "val1 = ? and val2 != ?", new String[] { "test", "test2" } );
Please refer to my post here:
Storing Lists to A Database, and Retrieving Them All Together : Android
and short answer to your question, yes.
Each '?' means that an argument will be expected, so for each '?' you WILL have an exact number of arguments to pass in unless you want an exception :) !
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