Do i have to use _ID as a SQlite primary key? and does it have to be an INT? (Android Dev)

后端 未结 3 1981
梦如初夏
梦如初夏 2020-12-10 15:54

This is probably a silly question but i haven\'t been able to find the answer yet.

I want to use a TEXT column with my own unique names as the primary key in a table

相关标签:
3条回答
  • 2020-12-10 16:05

    You can have anything you want as the primary key.

    But if you for instance want to use a CursorAdapter that automatically shows items from a Cursor-query you need to have an integer column called _ID as this is used here.

    But if this is not something you plan to use with your table, you can do what you want!

    0 讨论(0)
  • 2020-12-10 16:25

    _id must be of type int in sqlite create (long in Java). Various classes assume that. As a result, you cannot use UUID for primary key.

    0 讨论(0)
  • 2020-12-10 16:27

    You can create and define tables as you wish, but if you will have to use the table via a CursorAdapter that won't work without a tweak.

    CursorAdapter: The Cursor must include a column named "_id" or this class will not work.
    

    You must always issue a select col1 as _id ... to work.

    So it's recommended to use _id in the table schema, and if you like to use by some other name you could this this. select _id, _id as customname ... so select the _id column twice with different names.

    0 讨论(0)
提交回复
热议问题