sqlite& flex - 如果不存在则插入?

时间:2010-02-04 23:03:35

标签: database flex sqlite insert

我正在使用flex开发我的第一个桌面应用程序,我也是第一次使用sqlite。

我正在创建我的数据库和所有表格,我还想在几个表格中添加几行数据,以便用户在首次安装时可以使用一些数据。

我遇到的唯一问题是每次运行程序时都会一遍又一遍地插入相同的数据。

我只是想知道它是否可以做 - 如果不存在则插入。或其他一些工作。

谢谢!

2 个答案:

答案 0 :(得分:4)

使用相同的主键插入重复数据并使用“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

这个想法是会违反某些约束,并且会忽略该行。

答案 1 :(得分:1)

感谢您的见解,但我仍然没有运气。

这是我的代码

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();
相关问题