Skip to content Skip to sidebar Skip to footer

Sqlite & Flex - Insert Into If Not Exists?

I'm using flex to develop my first desktop app and I'm working with sqlite for the first time as well. I'm creating my database and all the tables and I would also like to add a f

Solution 1:

Insert duplicate data with the same primary key and use a "IGNORE" conflict clause:

sqlite>createtable t(i integerprimary key notnull);
sqlite>insertinto t values(1);
sqlite>insertor ignore into t values(1);
sqlite>select*from t;
1

Duplicate values will not be inserted, and the statement will complete successfully.

Alternatively, you can use "UNIQUE" constraint instead of a primary key:

sqlite>createtable t(i integeruniquenotnull);
sqlite>insertinto t values(1);
sqlite>insertor ignore into t values(1);
sqlite>select*from t;
1

The idea is that some constraint will get violated and the row will be ignored.

Solution 2:

Thanks for the insight but I'm still not having any luck.

Here's my code

stmt.text = "CREATE TABLE IF NOT EXISTS tbl_breed ("+" breed_id INTEGER PRIMARY KEY AUTOINCREMENT, "+"  breed_breed TEXT)";
stmt.execute();
stmt.text = "INSERT OR IGNORE INTO tbl_breed (breed_breed)"+" VALUES ('Test')";
stmt.execute();

Ok so I fixed the problem - I guess you have to hard code primary key here's what i did

stmt.text = "CREATE TABLE IF NOT EXISTS tbl_breed ("+" breed_id INTEGER PRIMARY KEY AUTOINCREMENT, "+"  breed_breed TEXT)";
stmt.execute();
stmt.text = "INSERT OR IGNORE INTO tbl_breed (breed_id,breed_breed)"+" VALUES ('1','test')";
stmt.execute();

Post a Comment for "Sqlite & Flex - Insert Into If Not Exists?"