I have a table defined as follows:
VideoID INTEGER,PRIMARY KEY , Status TEXT
I want to update this table every time when user scrolls the list if there is new data for any videoID it should be updated otherwise if new videoIDfound it should be inserted in table
it is working well with insertWithOnConflict() but the problem is it also updates seen column every time with a default value which I don't want I want to store the seen status for a videoID which cant be changed
an alternative i tried is:
String que="INSERT OR REPLACE INTO " + TABLE_NAME + "("+VIDEOTITLE+","+VIDEO_ID+","+VIDEO_TIME+","
+VIDEO_VIEW+","+VIDEO_AMOUNT+","+VIDEO_THUMB+","+VIDEO_URL+","+VIDEO_DETAILS+","+YOUTUBE_KEY+")"+" VALUES ( "+VIDEOTITLE+","+VIDEO_ID+","+VIDEO_TIME+","+VIDEO_VIEW+","+VIDEO_AMOUNT+","+VIDEO_THUMB+","+VIDEO_URL+
","+VIDEO_DETAILS+","+YOUTUBE_KEY+",(SELECT "+STATUS+" FROM "+TABLE_NAME+" WHERE videoId = "+ curr_id+" ));";
db.execSQL(que);
but this is not executing.
curr_id is the videoId.
Unfortunately SQLite doesn't support insert or replace.
You can use the on conflict replace to achieve a similar behaviour. You can read more about it here.
Keep in mind that on conflict replace will actually delete the existing record and create another one. That means that, if you are using an autoincrement column for you primary key, you will get a different record and not update the existing one.
If you want to keep the identity of the record intact, you may have to look into other, more complex options.
Related
I am making an Android App in which there is a List of Users.
And for that I am having a Users table where i am maintaining users' id using AUTO_INCREMENT
db.execSQL("create table Users (u_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, u_name TEXT , u_username text)");
Now my problem is that when i am trying to delete a user from database through ListView.
But after deleting the sequence order is like this : 1,2,4,5,7
So how can i rearrange the AUTO_INCREMENT column to : 1,2,3,4,5 like this.
How to query this thing in SQLite database.
Please help Me, i am stuck.
AUTO_INCREMENT will always increment by one from the last inserted record, no matter if that record still exists or not. if a sequential ordering without gaps is what you want, auto incremental ID is not the proper way to achieve that. Add another int field where you manually keep track of this ordering.
Say I have table Book and table Page. Say that table Page has book's dbId as a foreign key. When I do an INSERT OR REPLACE on a Book row, does that change the dbId of the book?
Say the book is title="Song of Songs", author="King Solomon",pages=50" say that I want to change the title of the book and that will lead to replacement of the row. So the question is: will the replace cause the dbId of the book to change? I imagine it shouldn't, but I just don't know.
So this is about ON CONFLICT REPLACE
The documentation says about INSERT OR REPLACE:
When a UNIQUE or PRIMARY KEY constraint violation occurs, the REPLACE algorithm deletes pre-existing rows that are causing the constraint violation prior to inserting the current row and the command continues executing normally.
If the new dbId has the same value as the old one, it does not change (obviously). But if the constraint violation happens in another column, the new dbId might have a different value (especially if it's an autoincrementing column).
For implementing your idea :
1:) First get to foreign key from the first table using select query.
2:) then create a function with a parameter through which you can pass this foreign key in process of getting relevant data using the foreign key from the foreign table.
3:) Now use below query for fetching to related data using that key-
databaseObject.update(TABLE_NAME, values, COLOMN_NAME + " = ?",
Array_of_replacing_values);
i am getting information from user in sqlite database.
But when i insert same record which is already in database it is added again.
how i can stop duplication of record in sqlite. I am developing this in android.
I am using mobile number as primary key. still it add that record in database.
Please suggest me appropriate solutions.
Thanks in advanced.
Be aware of the limitations of REPLACE or INSERT OR REPLACE as these will overwrite any custom data your app user has added to these rows in the database - it is not as advanced as UPSERT in other SQL databases.
As mentioned in a previous post you really need to identify what the primary key could be and use this information to either update old data or to remove an old row before inserting the fresh one.
If this is not possible then you could always DELETE FROM my_table or DROP my_table before running the insertions so that there will be no duplicates. This will (for better or worse) also make sure that data that is missing from new imports is not left lying around in your app.
make sure you have set your phone number as Primary Key at the time you created the table.
for example:
String query = "CREATE TABLE IF NOT EXISTS PhoneBook ("+
"TelNum VARCHAR(100) PRIMARY KEY NOT NULL,"+
"Address TEXT);";
db.execSQL(query);
and in case you want to enforce foreign keys defined in your table then call the following method before doing anything in your database
db.execSQL("PRAGMA foreign_keys = ON;"); //enforcing FK
Use REPLACE INTO keyword:
REPLACE INTO my_table (pk_id, col1) VALUES (5, '123');
This automatically identifies the primary key and finds a matching row to update, inserting a new one if none is found.
I have a table in my android sqlite database.There is a field 'id' which is auto increment primary key.My application service inserting a row in every minutes in the table.At some point in time i am deleting the entry older than 5 days.So total entry is limited.But id is increasing.So i am confuse about the value of the id as it is increasing with each new entry.What will be the condition when 'id' field exceed maximum value?. what is the maximum value of this field?How to work with this under the circumstances?Can i really work with auto increment field here?
The table Creation statement:
create table datausage (id integer primary key autoincrement,"
+ "date date not null,time text not null,level integer)
See this for the official docs. The largest possible ROWID is 9223372036854775807, which if you are writing 1000 entries a second will last more than 10675199116 days: I don't think you'll have difficulty with that.
When that is reached the database will start picking unused IDs, so you'll also be fine.
Use AUTO INCREMENT: it is the best option available to you.
EDIT: also, it is called AUTO INCREMENT for a reason. It is supposed to automatically increase.
An auto-incrementing primary key will continue to increment forever (in our terms) and will not replace deleted records. Even if you delete old records the id field will increment continuously.
As has been stated in other answers there is more than enough space for the field as well.
You need not use autoincrement if you want put your own values as per your logic,it will increment the value every time you insert a row.Instead you can drop table and recreate table if you want use autoincrement.
I want to generate automatic id of type ame100, ame101 and so on, so that as soon as I start my Android application, the next value of the series ame*** which is currently not in the database, should come automatically in edittext. So that on submitting the form I should have that value in database.
Make your primary key autoincrement, so that db will generate the next value.
Each time you need new id, just ask your databse for MAX(ID) from your table.
Increment it and set to edittext!
Note: this solution works only if you have one place of inserting the values.
I would suggest not showing the number before inserting, but create it after, when you already have generated id of the object.