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';
Related
In the snippet below I have two columns with UNIQUE (MEMBER_NAME & KEY_CALORIES). I only want it to be UNIQUE if both column are the same. I am trying to get UNIQUE (MEMBER_NAME, KEY_CALORIES), or something on those lines.
Example of what I am trying to accomplish:
Perhaps the user enters a MEMBER_NAME(but that name is already in the databsae) but KEY_CALORIES is different I want to be able to add that item to the SQLite database. If the user enters an already existing MEMBER_NAME and KEY_CALORIES (only when they are both the same) I want it to be UNIQUE. If there is trouble understanding what I am trying to accomplish, I will update my question.
// TABLE CREATION STATEMENT
private static final String CREATE_TABLE = "create table " + TABLE_MEMBER
+ "(" + MEMBER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ MEMBER_NAME + " TEXT NOT NULL UNIQUE," + KEY_CALORIES
+ " INT NOT NULL UNIQUE);";
Using UNIQUE(MEMBER_NAME) constraint means that you can only insert a MEMBER_NAME once. What you want is a UNIQUE constraint on MEMBER_KEY and KEY_CALORIES.
You can use UNIQUE(MEMBER_KEY,KEY_CALORIES) as the constraint, which means that their combined values should be unique. Duplicates are allowed on both columns, as long as this combination is unique.
Possible duplicate / reference answer :
Sqlite table constraint - unique on multiple columns
I want to replace all the values of an entire column(say column 1) with other column (say column 2) of the table using content provider. I have tried multiple options but was unable to find a proper answer.I Know that SET clause can be used but how can i use the SET clause with content provider?
The table is as below:
String SOLUTION_CATEGORY_TABLE_CREATE = "CREATE TABLE " + Constants.DataBaseConstants.LOOK_SOLUTION_CATEGORY_TABLE+"("
+ Constants.DataBaseConstants.LOADER_ID+" NUMERIC AUTO_INCREMENT,"
+Constants.DataBaseConstants.SOLUTION_CODE+" CHAR(15) NOT NULL,"
+Constants.DataBaseConstants.CATEGORY_ID+" NUMERIC(6) NOT NULL,"
+Constants.DataBaseConstants.CATEGORY_NAME+" CHAR,"
+Constants.DataBaseConstants.DEFAULT_PRODUCTS+" CHAR,"
+Constants.DataBaseConstants.RECOMMENDED_PRODUCTS+" CHAR,"
+Constants.DataBaseConstants.LOCAL_DEFAULT+" CHAR,"
+"CONSTRAINT "+ Constants.DataBaseConstants.SOL_CAT_FK1+" FOREIGN KEY("+Constants.DataBaseConstants.SOLUTION_CODE+")REFERENCES "
+ Constants.DataBaseConstants.SOLUTION_TABLE+"("+Constants.DataBaseConstants.SOLUTION_CODE+"),"
+"CONSTRAINT "+ Constants.DataBaseConstants.SOL_CAT_FK2+" PRIMARY KEY ("
+Constants.DataBaseConstants.SOLUTION_CODE+","+Constants.DataBaseConstants.CATEGORY_ID+"))";
Now i want to replace local default product with default products column.
I know the update query should be something like below:
UPDATE Constants.DataBaseConstants.LOOK_SOLUTION_CATEGORY_TABLE SET Constants.DataBaseConstants.LOCAL_DEFAULT=Constants.DataBaseConstants.DEFAULT_PRODUCTS;
But I do not know how to do the same query using content Provider.
What should i send in the arguments of update method(mentioned below) so that the end query is same as above.
getContentResolver().update(DataProvider.CONTENT_URI_LOOK_SOLUTION_CATEGORY, values, selection, args);
thanks in advance.
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.
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) + ");");
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