我是否必须使用_ID作为一个SQLite主键?而且它必须是一个INT? (Android开发)是一个、作为一个、主键、_ID

2023-09-06 04:03:28 作者:只愿得一人心

这可能是一个愚蠢的问题,但我一直没能找到答案。

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

我想用一个TEXT列有我自己独特的名称作为一个表的主键。 的code中的小部分来定义,这看起来像这样在我的项目:

I want to use a TEXT column with my own unique names as the primary key in a table. the little section of code to define this looks something like this in my project:

...blahblah..."CREATE TABLE " + CAT_BUD_TAB + " (" + CAT_ITEM_ID + "_ID TEXT PRIMARY KEY,    "...blahblah..

将这项工作作为我打算?也许我需要使用AS号?我只看到_ID单个表中的自动增量整数。另外这是为了在另一个表的外键,但因为我设计我的数据库,我读过更多的信息,我不知道那将真正重要的使用安卓和放大器; SQLITE?

Will this work as i intend? Or perhaps i need to use "AS ID"? I only ever see single tables with _ID as an autoincrementing integer in. Also this was intended to be a foreign key in another table but since i designed my database i've read more information and i'm not sure that'll really matter using android & SQLITE?

由于下面的海报,但我是一个有点慢,不知道如果我申请的信息正确,你能检查?

所以,如果我有一个创建一个这样的语句:

So if i have a create statement like this:

"CREATE TABLE " + CAT_BUD_TAB + " (" + CAT_ITEM_ID + " TEXT PRIMARY KEY, " + 
    IN_OUT + " TEXT, " + BUDGET_AMOUNT + " REAL, " + ACTUAL_AMOUNT_SPENT + " REAL, "
    + AMOUNT_STRAYED + " REAL, " + OVERBUDGET_TF + " INTEGER, " + AUTOSPEND_TF + 
    " INTEGER);"

然后有:     db.execSQL(SELECT ID,ID AS CAT_ITEM_ID)

Then have: db.execSQL("SELECT ID, ID AS CAT_ITEM_ID")

我可以再使用它们互换?是我甚至附近的任何地方接近?哈哈对不起,我正在尝试!

Can i then use them interchangeably? am i even anywhere near close? haha sorry i am trying!

推荐答案

您可以创建和如你所愿定义表,但如果你将不得不通过,不会没有一个调整上班的CursorAdapter使用该表。

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.

您必须发出选择COL1作为_id ... 工作

因此​​,建议使用 _id 在表模式,如果你想通过一些其他的名字,你会这样此使用。 选择_id,_id为customname ... 所以用不同的名称选择两次_id列。

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.