Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add primary key to text datatype in android sqlite? [duplicate]

Tags:

android

sqlite

Possible Duplicate:
Is it possible to apply primary key on the text fields in android database

I have done like these getting force close.

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

it working for integer if any have idea please help me.

like image 245
sai Avatar asked Feb 08 '12 13:02

sai


2 Answers

You don't create primary keys like that in SQLite. I have not tested the following, but according to the documentation it should work:

private static final String CREATE_NEWCUSTOMER = "create table newcustomer (_id integer autoincrement, cname text, date text, caddress text, PRIMARY KEY(_id, cname));";
like image 76
Thorsten Dittmar Avatar answered Nov 12 '22 17:11

Thorsten Dittmar


You can add primary key attribute to text without any problem. In your query above there is a major problem. You can not apply attribute 'AutoIncrement' if your Primary key is composed of more than one column.

  • AUTOINCREMENT not allowed if primary key is over two or more columns

Here is an example query that creates a table with a primary key which is comprised on two columns, one column has Integer datatype and other has text PRIMARY KEY ("col_1", "col_2").

CREATE TABLE "Table_Name" ("col_1" INTEGER NOT NULL , "col_2" TEXT NOT NULL , "col_3" TEXT, "col_4" VARCHAR, PRIMARY KEY ("col_1", "col_2"))
like image 3
Muhammad Nabeel Arif Avatar answered Nov 12 '22 19:11

Muhammad Nabeel Arif