views:

461

answers:

2

I can find syntax "charts" on this on the sqlite website, but no examples and my code is crashing. I have other tables with unique constraints on a single column, but I want to add a constraint to the table on two columns. This is what I have that is causing a SQLiteException with the message "syntax error".

CREATE TABLE name (column defs) UNIQUE (col_name1, col_name2) ON CONFLICT REPLACE

I'm doing this based on the following:

table-constraint

EDIT: To be clear, the documentation on the link I provided says that CONTSTRAINT name should come before my constraint definition.

Something that may lead to the solution though is that whatever follows my parenthesized column definitions is what the debugger complains about.

If I put

...last_column_name last_col_datatype) CONSTRAINT ...

the error is near "CONSTRAINT": syntax error

If I put

...last_column_name last_col_datatype) UNIQUE ...

the error is near "UNIQUE": syntax error

A: 

Well, your syntax doesn't match the link you included, which specifies:

 CREATE TABLE name (column defs) 
    CONSTRAINT constraint_name    -- This is new
    UNIQUE (col_name1, col_name2) ON CONFLICT REPLACE
Larry Lustig
I initially did that...didn't work. I tried it again just in case...still doesn't work
Rich
Ayman has the answer. . .
Larry Lustig
+7  A: 

Put the UNIQUE declaration within the column definition section:

CREATE TABLE name (column defs, UNIQUE (col_name1, col_name2) ON CONFLICT REPLACE);

Working example:

CREATE TABLE a (i INT, j INT, UNIQUE(i, j) ON CONFLICT REPLACE);
Ayman Hourieh
awesome...much appreciated
Rich
Welcome to 10K. Now lets make it count X-)
astander
@astander - Woohoo! Thank you. :)
Ayman Hourieh