sqlite& flex - 如果不存在插入? [英] sqlite & flex - insert into if not exists?

查看:103
本文介绍了sqlite& flex - 如果不存在插入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



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



我只是想知道是否可能做一个 - INSERT INTO IF NOT EXISTS。

解决方案



这是我的代码

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


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 few rows of data into a couple of the tables so that the users have some data to work with on first install.

The only problem I'm having is every time I run the program it keeps inserting the same data over and over again.

I'm just wondering if its possible to do a - INSERT INTO IF NOT EXISTS. or some other kind of work around.

thanks!

解决方案

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

这篇关于sqlite& flex - 如果不存在插入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆