Sqlite & flex - insert if doesn't exist?

I use flex to develop my first desktop application, and I am also working with sqlite for the first time.

I create my database and all the tables, and I would also like to add several rows of data to a couple of tables so that users have some data to work with the first installation.

The only problem I encounter is every time I run the program, it continues to insert the same data over and over.

I'm just wondering if this is possible - INSERT INTO IF NOT EXISTS. or some other kind of work.

thank!

+3
source share
2 answers

, .

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();

, - , ,

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();
+1

IGNORE:

sqlite> create table t(i integer primary key not null);
sqlite> insert into t values(1);
sqlite> insert or ignore into t values(1);
sqlite> select * from t;
1

, .

"UNIQUE" :

sqlite> create table t(i integer unique not null);
sqlite> insert into t values(1);
sqlite> insert or ignore into t values(1);
sqlite> select * from t;
1

, , .

+4

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


All Articles