Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: GUID Primary key

I know if I don't use a field named _id as my primary key in Android, that certain things like the CursorAdapter won't work, but does the _id column need to be an autoincrement int?

Could I use a Guid as the key, as long as it's called _id, and have the CursorAdapter still work?

like image 970
Echilon Avatar asked Mar 03 '11 13:03

Echilon


People also ask

Can I use GUID as primary key?

GUIDs can be considered as global primary keys. Local primary keys are used to uniquely identify records within a table. On the other hand, GUIDs can be used to uniquely identify records across tables, databases, and servers.

Does SQLite support GUID?

SQLite itself does not support GUID as internal type.

What is primary key in Android Studio?

implements Annotation. android.arch.persistence.room.PrimaryKey. Marks a field in an Entity as the primary key. If you would like to define a composite primary key, you should use primaryKeys() method. Each Entity must declare a primary key unless one of its super classes declares a primary key.

Should primary key be a string?

1 Answer. Yes, from a performance standpoint (i.e. inserting or querying or updating) using Strings for primary keys are slower than integers. But if it makes sense to use string for the primary key then you should probably use it.


1 Answers

The yellow box in the storage guide says:

Android does not impose any limitations beyond the standard SQLite concepts. We do recommend including an autoincrement value key field that can be used as a unique ID to quickly find a record. This is not required for private data, but if you implement a content provider, you must include a unique ID using the BaseColumns._ID constant.

Now when you click on the BaseColumns class you will see

public static final String _ID

The unique ID for a row.

Type: INTEGER (long)

Constant Value: "_id"

So I guess, a GUID will not work.

like image 94
Heiko Rupp Avatar answered Oct 02 '22 09:10

Heiko Rupp