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.
Related
First of all I am using SQLite-Net-Pcl on Xamarin.Android
My question is this, I need to create an event that is recursive,
and I need to connect all those recursive events (in case of someone edditing them or delleting them).
And I need to use the original Event's Id to connect it to the others
-> creates new recursive event
-> gets the id of inserted event
-> uses that id to connect all recursive events together on a "NumDocConnection"
My Id is auto-incremented, and is the primary key.
So, how can I get the id of the last inserted event? or is this even a good way do to deal with the situation?
how can I get the id of the last inserted event?
The int-based primary/auto-inc value is obtained via the object that you inserted (the object instance is updated during the Insert with the new value).
var newRecord = new Record { SomeString = "StackOverflow" };
var numofRecordsInserted = conn.Insert(newRecord);
Console.WriteLine($"Newly inserted id = {newRecord.Key}");
is this even a good way do to deal with the situation
That depends upon your application. A pri-key/auto-inc field does prevent the reuse of the generated IDs, that is until the database is reset (via deletion/re-creation or a reset via the the sqlite_sequence table).
Should you use it as a "foreign" key, lots of opinions here, some yes, some no.... Personally if you never use it externally to your app (i.e. it is not transmitted/sync'd via a remote API) it works well/fast as a foreign key if you understand how SQLite creates it and when it can/could be reset, see the linked docs.
If the AUTOINCREMENT keyword appears after INTEGER PRIMARY KEY, that changes the automatic ROWID assignment algorithm to prevent the reuse of ROWIDs over the lifetime of the database. In other words, the purpose of AUTOINCREMENT is to prevent the reuse of ROWIDs from previously deleted rows.
re: https://sqlite.org/autoinc.html
I currently have an app where I store user data in a SQLite database, and one of my fields is a User ID. I would like to add an option to auto-generate User IDs in an mmddyyXXX format, where XXX is a sequential number per user that resets every day.
Does anyone know how I would approach this? I looked at some of the other similar questions, but they don't seem to be helpful.
This is not complicated at all. If your'e similar with SQLite in android just take the date and the userId using a SELECT and generate that string yourself.
If the XXX is not the userId just save another table containing 'tokens' for users. every userId would have a 'token'.
Every new day just change the contents of this table.
I believe you could use a TRIGGER that will generate the userid when a row is inserted.
The following may suit :-
CREATE TRIGGER IF NOT EXISTS newuserid AFTER INSERT ON users
BEGIN
UPDATE users SET userid = strftime('%m%d',date('now'))||substr(strftime('%Y',date('now')),3)||
(
SELECT CAST(substr('000',1,3-length(count()+1)) AS TEXT)||CAST((count()+1) AS TEXT)
FROM USERS
WHERE substr(userid,1,6) = strftime('%m%d',date('now'))||substr(strftime('%Y',date('now')),3)
)
WHERE userid IS NULL;
END;
The trigger is named newuserid
userid is the column for the auto-generated id. The above relies upon it being NULL so it cannot be a PRIMARY INDEX.
There is no reliance upon other columns.
Testing
Starting with an empty table :-
Inserting 4 rows using INSERT INTO users VALUES(null,'This is a new user'); results in :-
To check for another date the rows are adjusted from 041018??? to 040918??? as per :-
4 more rows are inserted using INSERT INTO users VALUES(null,'This is a new user');, resulting in :-
Note this answer isn't intended to be fail-safe but rather the basis of the concept for the answer.
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.
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 have one orderid field in android form .. now i want to add a number along with the content of orderid such that each time the number should be in incremental manner..
for example
if user input 123 in orderid then i want to store 123100 in database
similarly on next run i want to store 123101 in database ..and so on...how will i do this
plz help
Create a field with INTEGER PRIMARY KEY as a type with the name _id. This field will increase by one each time a value is added into the database.