文本作为android中的主键

时间:2012-02-09 07:48:35

标签: android

  

可能重复:
  Is it possible to apply primary key on the text fields in android database

private static final String CREATE_NEWCUSTOMER = 
                "create table newcustomer (_id integer  autoincrement, " + 
                "cname text  , date text , " + "caddress text);";

在此我如何使用cname作为主键。

2 个答案:

答案 0 :(得分:0)

...写

cname text primary key not null

答案 1 :(得分:0)

在Android中,你总是需要在你运行的任何查询的结果中都有“_id”(我实际上不确定它是否需要成为主键,但我认为这是主意。)

如果您确实希望将cname作为主键,但在数据库中将其标记为 cname (而非_id),则可以执行以下操作:

SELECT cname AS _id, date, caddress FROM newcustomer

之前我知道文本字段总是独一无二的。但是有条件是你需要确保cname始终是唯一的,否则你可能会遇到问题。我仍然建议将_id列作为整数并使用它。