SQLite delete syntax error - android

This line, mDatabase.execSQL(sql);, gives me the following error:
{ sqlite returned: error code = 1, msg = near "*": syntax error }
for Delete * from table_name Query
My SQL query is : DELETE * FROM table_name
How can I solve this?

DELETE * FROM table_name is a wrong sql command. Use DELETE from table_name

Syntax error means that basically your statement is spelled wrong and it can not be parsed. In this case error message states where exactly this error occurred - on "*" character. In such case you should go to database documentation and check proper syntax of the command you are trying to use. In case of SQLite it's here. You can find documentation about DELETE statement there, here is the link. It shows you the syntax in graphical way (called syntax diagrams or railroad diagrams) which should be quite easy to follow.
In this case, as mentioned earlier, you just can't specify "*" between DELETE and FROM. This is because you are always deleting whole rows and you can't delete individual selected columns.

If you need to delete the entire table can you use DROP TABLE IF EXISTS then recreate the table

Related

Error with query in SQLite

My SQLLite query is not working properly. See below:
UUID i=UUID.randomUUID();
String Beregero ="INSERT INTO
contacts(id,uuid,name,phone,email,street,city,state,zip) " +
" VALUES(3,"+"'"+i.toString()+"'"+",'Patrice
Beregeron','978-555-1212','pBeregero#BostonBruins.com'," +
"'1 causeway street','Boston','Mass','01236');";
db.execSQL(Beregero);
I am receiving the following error in my log:
(table contacts has no column named uuid (code 1): , while
compiling: INSERT INTO contacts(id,uuid,name,phone,email,
street,city,state,zip) VALUES(3,'12ee5bbf-dabb-4d95-bfe7-6e6f14702add',
'Patrice Beregeron','978-555-1212','pBeregero#BostonBruins.com',
'1 causeway street','Boston','Mass','01236');)
#################################################################
The exception says it all, your table has no column name uuid created. So uninstall the app, then run it again. This error occurs only if the column is not generated.
The error message in you log clearly states the problem. The contacts table does not have a column uuid. You now could do the following:
Check if you have got right lower case vs. upper case. The column in question might be UUID instead of uuid (I don't know SQLite, but case matters in many database systems).
If the column really is not in the table yet, then add it (either by code or by hand). Read SQLite's documentation to learn how to that (I can't help you here because I don't know SQLite).
Uninstalling and re-installing the app would help only if the app would generate the database upon installing or during the first run. Since we don't know anything about the app, this might or might not be the case.
You have not added the column "uuid" in the create table "contacts" query like this :
String createTableContacts = "create table contacts ( ' uuid text ')";

Why do I get syntax error when creating a view with column names?

Developing an Android app, but this is a SQLite question.... My SQL is much ropier than my Java.
This is the SQL (copied from a log file, in code it's constructed from various constants):
CREATE VIEW albums (_id, name , type ) AS SELECT rowno, name, subtype FROM metadata WHERE subtype = 'album'
but it throws:
android.database.sqlite.SQLiteException: near "(": syntax error (code 1): , while compiling:
Specifying column names in the view seems to me to be clearly permitted, if I understand the flow chart in the sqlite documentation here, and at this time of night I really can't see what's wrong. I have tried changing the name of the _id column in case it was something to do with the initial underscore, but to no avail and in any case I will need the _id column later for the CursorAdapter that will end up using this View.
At the bottom of the documentation page you linked it states:
Note that the column-name list syntax is only supported in SQLite
versions 3.9.0 and later.
Version 3.9.0 is not supported by Android. Lollipop has 3.7.11. Marshmallow has 3.8.10.2.

Why the excecSQL not accept * in the statement?

I'm working on an android project and I have to delete all records in my table:
public void deleteAll(){
SQLiteDatabase db=this.getWritableDatabase();
String req="delete * from "+ TABLE_ENIGME;
db.execSQL(req);
}
But when I call my function deleteAll this exception is reveled :
android.database.sqlite.SQLiteException: near "*": syntax error: delete * from enigme2
So why " * " is innapropriate?
Please have a look at http://www.sqlite.org/lang_delete.html.
Delete does not accept wildcard.
Use
Delete From <table name>
instead
mostly because thats not valid sql syntax. The just omit the star and do
delete from enigme2;
It's inappropriate because it isn't valid SQL syntax. The * is used to denote "all columns" (when SELECTing). Since the DELETE statement is removing entire rows stating "all columns" is meaningless, as it removes the values from all columns for that row by default.

sqlite match operator

On Android i would like to use a query similar to this:
SELECT docid FROM docs WHERE docs MATCH '("sqlite database" OR "sqlite library") AND linux'
http://www.sqlite.org/fts3.html#section_3
The problem is that I got a "malformed match expression error". When I do more basic match query it works, and it seems the issue might be because of the parentheses.
When I use exactly the same statement (except the table the column name) the error is the same.
The complete error message:
sqlite returned: error code = 1, msg = statement aborts at 7: [SELECT * FROM namedays WHERE names MATCH '("sqlite database" OR "sqlite library") AND linux';] malformed MATCH expression: [("sqlite database" OR "sqlite library") AND linux], db=/data/data/org.xyz/databases/data
Any idea?
I believe you do not have SQLite compiled with the SQLITE_ENABLE_FTS3_PARENTHESIS option. As such, you can only use the Standard Query Syntax, not the Enhanced Query Syntax
You can easily verify this by executing the compile_options pragma. For example, we see that my sqlite3 doesn't have the enhanced syntax:
sqlite> PRAGMA compile_options;
DISABLE_DIRSYNC
ENABLE_COLUMN_METADATA
ENABLE_FTS3
ENABLE_RTREE
ENABLE_UNLOCK_NOTIFY
SECURE_DELETE
TEMP_STORE=1
THREADSAFE=1

sqlite query runtime error

I was working with sqlite but I end up with this error.I'm not getting what is error here.
Below is my logcat.
android.database.sqlite.SQLiteException: near "/": syntax error: , while compiling:
SELECT start,end FROM crop_list WHERE name =/sdcard/Shri Ramachandra kripalu bhajamana.mp3
I think your problem is you forget to add the quotes marks (' ') like:
SELECT start,end FROM crop_list WHERE name ='/sdcard/Shri Ramachandra kripalu bhajamana.mp3'.
why do you not use class helper and Helper.dabase.query(.....) <- it is faster ;) around 2 segons faster that helper.dabase.rawquery ;)
SQLiteOpenHelper | Android Developers <-- best practice

Categories

Resources