Utman Alami
Utman Alami

Reputation: 131

issue in composite kprimary key in sqlite

i have a problem, i need to add a composite primary key ( _id + col_Region ) to my table but i don't know how to proceed, i know to do it in the usual way, but i tried with this syntax it doesn't work :

 private static final String DATABASE_CREATE1 = "CREATE TABLE " + MENAGE + 
  "(  " + _id +" INTEGER PRIMARY KEY ,"+ col_Region +" TEXT,"+ 
   LastModifiedTime +" TEXT,"+ Comune_Arrondis +" TEXT,"+ N_district +" INT ");

how to solve that, i looked for solution in the forum but i didn't found one , thanks for help

Upvotes: 0

Views: 27

Answers (1)

NigelK
NigelK

Reputation: 8490

The syntax would be:

CREATE TABLE menage (
_id INTEGER NOT NULL,
col_Region TEXT NOT NULL,
LastModifiedTime TEXT,
Comune_Arrondis TEXT,
N_district INT,
PRIMARY KEY (_id, col_Region)
)

or in terms of code:

private static final String DATABASE_CREATE1 = "CREATE TABLE " + MENAGE +
"(  " + _id + " INTEGER NOT NULL, " + col_Region + " TEXT NOT NULL, " +
LastModifiedTime +" TEXT," + Comune_Arrondis + " TEXT, " + N_district +" INT, " +
"PRIMARY KEY ( " + _id + ", " + col_Region + "))";

So you declare all the columns first and then declare which columns form the primary key at the end. Note that the columns that form the primary key should be given the NOT NULL constraint.

Upvotes: 1

Related Questions