this is my first time working with DB in android. So I figured I would follow this tutorial http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/
As you can see, it makes you create a DB which as both an ID and something called UNIQUE_ID
(there's even a function in PHP which creates a unique identifier)
ID is the primary key
UNIQUE_ID is set as UNIQUE
my question is. why do I need both? which one do I use for foreign keys?. Which is the point of having such a long a complicated ID?
Thanks In advance
The first ID it is the primary key for the table which is mainly a unique counter that updates every time a new row is created. It will keep increasing even if the row gets deleted.
UNIQUE_ID is an identifier created to keep track of users; think of it as if it were a unique username field
Related
For an android room interface, I want to get the autogenerated id (as primary key of a record just inserted), so that I can put it in the object without executing a select after insert, where the select might return the wrong record if there is no other unique attribute, or set of attributes for those record types.
For example, for 2 people having the same name being inserted into the same table. You might say generate a composite key to make a unique set. However that might involve the addition of new fields that are otherwise not required.
I've seen various links, including those below. Some mention that it is the row id that is returned if the insert method is declared to return integer (or long), and succeeds.
However it is my understanding that the row id cannot be assumed to be the same as the primary key. (Refer Rowid after Insert in Room).
I cannot comment on any posts because I don't have enough reputation points.
I appreciate any comments regarding what might be a good/typical approach to this problem.
These are the posts I have looked upon:
Android Room - Get the id of new inserted row with auto-generate
https://developer.android.com/training/data-storage/room/accessing-data
https://commonsware.com/AndroidArch/previews/the-dao-of-entities
Late answer just for anyone seeing this question in the future
from SQLite docs it says :
The PRIMARY KEY of a rowid table (if there is one) is usually not the
true primary key for the table, in the sense that it is not the unique
key used by the underlying B-tree storage engine. The exception to
this rule is when the rowid table declares an INTEGER PRIMARY KEY. In
the exception, the INTEGER PRIMARY KEY becomes an alias for the rowid.
therefore it's correct to assume that the rowId returned by insert query is the same as the autoincremented-primary-key
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'm building an app for Android where I'm reading the instances table to get all events.
I've used the "unique" row id as a way to reference to different instances, however I've now found that the row id changes over time, it actually seems to increment by 2 every now and then. Is there a reason for this behaviour? What is the recommended way to handle instances and how to uniquely reference each instance?
This is the only code I use when creating the meeting objects in the app.
int columnID = cursor.getColumnIndex(CalendarContract.Instances._ID);
String uniqueid = cursor.getString(columnID);
meeting.setUniqueId(uniqueid);
I think you should be using CalendarContract.Instances.EVENT_ID instead as the documentation describes that as the unique ID for events: EVENT_ID
_ID might be changing because they are adding/removing rows under the hood without caring for the changes.
I am new in programming, and I want to ask question regarding database schema (I'm using SQLite Database for Android Development)
I have some table, let say :
MsMember
MemberId
Password
MsGroup
GroupId
GroupName
MsAnnouncement
AnnouncementId
AnnouncementName
MsComment
CommentId
CommentContent
MsTodolist
TodolistId
TodolistTitle
And I want everytime a new row has been inserted to (at least one of) all five tables above, it will create a notification to user, as far as I know, with this concept, I should create a table to store every detail of notification then shows it to user..
And my best opinion so far is I create a table let say MsNotification, then to connect all five tables with this MsNotification I should have foreign key referring to each table..
My Question is would it be possible (and effective) to have a column that has more than one references?
Example :
Foreign key (SourceId) Referring MsMember (MemberId),
Foreign key(SourceId) referring MsComment (CommentId),
Foreign key (SourceId) referring MsAnnouncement (AnnouncementId), and so on.
or is there any better way to implement this concept?
Thank you in advance
No ,you can not assign single foreign key to multiple column .
But you can put multiple foreign key in single table
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.