How to add primary key to text data type in android sqlite?

Possible duplicate:
Is it possible to apply a primary key in text fields in an Android database .

I made this force come closer.

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

it works for an integer, if you have any ideas, please help me.

+6
source share
3 answers

You do not create primary keys like 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));"; 
+7
source

You can add the primary key attribute to the text without any problems. There is a big problem in your request above. You cannot apply the "AutoIncrement" attribute if your primary key consists of several columns.

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

Here is an example of a query that creates a table with a primary key that consists of two columns, one column has the Integer data type, and the other has the 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")) 
+3
source

Try the following:

 final String CREATE_TABLE_JobTime = "CREATE TABLE jt (q_id INTEGER PRIMARY KEY AUTOINCREMENT,description VARCHAR);"; db.execSQL(CREATE_TABLE_JobTime); 
-3
source

Source: https://habr.com/ru/post/907943/


All Articles