Android database tables syntax errors - android

I am trying to create a database with the name of a string stored in a local variable with the following syntax:
mDb.execSQL("CREATE TABLE " + FLASH_TABLE + " (" + KEY_CARD_ROWID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_QUESTION
+ "TEXT NOT NULL," + KEY_ANSWER + "TEXT NOT NULL);");
which produces the following error in log cat:
05-14 04:40:05.892: ERROR/Database(372): Failure 1 (near "15": syntax error) on 0x272140 when preparing 'CREATE TABLE 15 (_id INTEGER PRIMARY KEY AUTOINCREMENT, questionTEXT NOT NULL,answerTEXT NOT NULL);'.
with FLASH_TABLE being the local variable with the value "15"
KEY_QUESTION and KEY_ANSWER are also local variables
I have looked at every example online that I could find and I can not find the reason for the syntax error. I appreciate any advice you can offer.

You cannot create a table name (or a field name, for that matter) with a string that begins with a number unless you double-quote it. But even though it's technically possible, it's not considered a best practice. If you do, you will also have to double-quote the table name every time you use it in a query so you will quickly find that it's more trouble than it's worth.

Related

Android Studio 3.0 canary 1 : SQL syntax error

I have a problem after upgraded to Android Studio 3.0 canary 1 from Android Studio 2.3
db.execSQL("CREATE TABLE IF NOT EXISTS " + Contract.COUNTRY_PATH + " (" +
Contract.Country._ID + " INTEGER AUTO INCREMENT , " +
Contract.Country.COLUMN_CITY_ID + " INTEGER PRIMARY KEY," +
Contract.Country.COLUMN_COUNTRY_NAME + " TEXT," +
Contract.Country.COLUMN_SUNRISE + " INTEGER," +
Contract.Country.COLUMN_SUNSET + " INTEGER) ");
It shows an error.
'(',')',<column constraint> or comma expected ,got 'AUTO'
It was fine with Android Studio 2.3.
Any suggestions.
Thanks.
Do not put any spaces in you column name.
I'm using Android Studio 3.0 beta7. Same problem still exists.
The SQL syntax checker in Android Studio 3 is stricter than sqlite itself.
INTEGER AUTO INCREMENT in sqlite itself basically just results in a column with integer affinity. The mistyped AUTO INCREMENT is just noise. That's why it gets flagged in Studio but does not cause a syntax error in sqlite.
If you want an autoincrementing column, make it INTEGER PRIMARY KEY. If you really need the rowid reuse avoidance, then the column should be INTEGER PRIMARY KEY AUTOINCREMENT. Note that you can only have one primary key column in a table. See also: https://sqlite.org/autoinc.html
CREATE TABLE yourtablename (id INTEGER primary key autoincrement NOT NULL, name TEXT, description TEXT, expValue INTEGER, category INTEGER NOT NULL REFERENCES categories (id), date TEXT)

Getting null while trying to retrieve rowid in fts3

I'm stuck at this for last 5-6 hours. Nothing is working. I want to get key corresponding to a user entered query so that user can modify the data corresponding to that particular id.
Is something wrong here in this code..??
db.execSQL("CREATE VIRTUAL TABLE " + DATABASE_TABLE
+ " USING fts3 (" + KEY_ID
+ " INTEGER PRIMARY KEY ASC, " + KEY_NAME
+ " TEXT NOT NULL, " + KEY_ADDRESS
+ " TEXT NOT NULL, " + KEY_CONTACT + " TEXT NOT NULL, "
+ KEY_BALANCE + " DOUBLE);");
I'm getting this KEY_ID column empty. I want to get this id (using cursor element like we use cursor.getString(int) ) to let the user modify the values corresponding to the chosen id so I need to get the id from somewhere. Is there any way to call the default ID of fts3 table rather than using an alias?
Is there any way to call the default ID of fts3 table rather than using an alias?
The problem is that Cursors used in Adapters must have an integer primary key column with the name _id, but _id is not a recognized alias by SQLite. So no, you need to use your own alias. You can use docid as _id, rowid as _id, etc.
Addition
Since I don't believe you can alter the docid INTEGER PRIMARY KEY column in anyway inside the CREATE VIRTUAL TABLE statement, you will need to apply the alias in the SELECT statements, for example:
SELECT docid as _id, bar FROM Foo WHERE bar MATCH 'John';

foreign key constraint ON DELETE CASCADE not working in sqlite database on android

I have "days" table created as follows
"create table days(" +
"day_id integer primary key autoincrement, " +
"conference_id integer , " +
"day_date text, " +
"day_start_time text, " +
"day_end_time text, " +
"day_summary text, " +
"day_description text)";
and i have tracks table created as follows
CREATE_TABLE_TRACK = "create table track(" +
"track_id integer primary key autoincrement," +
"day_id integer,"+
"track_name text," +
"track_description text," +
" FOREIGN KEY(day_id) REFERENCES days(day_id) ON DELETE CASCADE )";
as shown above i have foreign key day_id referencing to the day_id of table days...
So what i want is if i delete the day then corresponding track should also be deleted... But it does't happen in my case..
I have sqlite with version 3.5.9
And also i have added 1 line in my helper class as
> db.execSQL("PRAGMA foreign_keys=ON;");
but is still won't work.. please help me out..
Cascading delete isn't supported until Sqlite version 3.6.19, which is first included on Android 2.2.
Fortunately there is an alternative.
You can execute another query like this below your create table query:
db.execSQL("CREATE TRIGGER delete_days_with track BEFORE DELETE ON track "
+ "FOR EACH ROW BEGIN"
+ " DELETE FROM days WHERE track.day_id = days.day_id "
+ "END;");
Note that delete_days_with_track is just a name descriptive of what the trigger does, and this is just the pattern I use; I believe you could name it anything you wish.
According to the SQLite Documentation support for Foreign Keys was not added until 3.6.19.
Using 3.5.9 you'll have to do your cascade deletions in some other manner.

How to do SQL Insert or Replace like operation without changing primary key?

I'm trying to insert a record into a SQLite database table. The table has a Unique constraint on a column "URL" and a primary key auto increment integer "_id".
I'm using insert or replace as my conflict resolution, since I need the new data to be persisted. However, this method is incrementing the primary key and messing up some foreign keys in other tables.
What's the best way to overwrite the record without updating the _id?
The simple answer is to stop using Replace syntax. This causes the old record to be deleted then a new one added ... which would increment your index.
Utilize the UPDATE syntax to handle conflicts instead
EDIT:
If you are really partial to the Replace syntax then it will come at a cost. You will need to write additional code that updates all prev occurrences of the old index to the new one. Not overly hard but this will correct the issue of synchronizing indexes
Documentation [Listed under REPLACE section little ways down the page]: http://www.sqlite.org/lang_conflict.html
this is my code of SQLite
"CREATE TABLE IF NOT EXISTS posts (" +
"_id integer NOT NULL," +
"id_language integer NOT NULL" +
");" +
"CREATE UNIQUE INDEX posts_idx ON posts(_id, id_language);";
"INSERT OR REPLACE INTO " + DB_TABLE + " (" + formulateColumns() + ") " +
"VALUES (" + formulateValues(v) + ");");

Android SQLite - How To Update The Record ID Number After Deleting Records

I can look up records by values and/or ID
I can add records
I can delete All records
I can delete a specific record (by either value or ID)
But, the record ID only increments - it never adjusts to reflect the number of remaining records after deleting a record.
I haven't found anything on this subject (not even in the Doc's)
What/How do I do this?
Thanks
The declaration for this part of the db follows...
String sql =
"create table "
+ TABLE
+ "( " + BaseColumns._ID
+ " integer primary key autoincrement, "
+ THE_NUMBER + " integer, "
+ FAV_NAME + " text not null);";
Rather than deleting this post, I'll post the ref to the answer (it's at SQL's site).
link text

Categories

Resources