安卓:用自动增量列插入sqlite的纪录 [英] Android: inserting sqlite record with AUTOINCREMENT column

查看:221
本文介绍了安卓:用自动增量列插入sqlite的纪录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样创建的Andr​​oid SQLite数据库:

 源码> .schema
CREATE TABLE标准(_idINTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,活动文本,重要的文本,排序INT,摘要文本,用户文本,CATEGORY_ID INT,entrytype INT);
 

我可以将记录插入该表中的唯一方法是通过指定的值 _id 我要自动递增。这是我可以插入工作的唯一方法:

  recid = totalrecs + 1;
串Q =插入标准(_id,积极的,重要的是,排序,汇总,用户,CATEGORY_ID,entrytype)值(+ recid +,\1 \,\0 \,99,\foobar的\ \1 \,99,0);
Log.d(TAG的问题:+ Q);
mDb.execSQL(q)的;
 

如果我离开_id列出来,不指定_id的值,我得到一个错误:

  android.database.sqlite.SQLiteConstraintException:criterion._id不能为null:插入标准(积极,重要的是,排序,汇总,用户,CATEGORY_ID,entrytype)值(1 ,0,99,foobar的,1,99,0)
 

如何安排的查询(或模式)将因此Android照顾递增的 _id 列?

更新/固定2行以上(除去NOT NULL,删除_id从查询):

  CREATE TABLE准则(_idINTEGER PRIMARY KEY AUTOINCREMENT,活动文本,重要的文本,排序INT,摘要文本,用户文本,CATEGORY_ID INT,entrytype INT);

串Q =插入标准(积极,重要的是,排序,汇总,用户,CATEGORY_ID,entrytype)值(\1 \,\0 \,99,\foobar的\,\1 \, 99,0);
 

解决方案

从架构中删除NOT NULL,你是金色的。

澄清: 指定NOT NULL上自动增量列使它如此的自动递增不起作用。该NOT NULL是什么使得它,所以你必须指定_id。

一旦你删除它,并且不包括在INSERT语句中的_id,SQL将获得_id为NULL,并自动处理设置_id你。

I have an sqlite database on android created like this:

sqlite> .schema
CREATE TABLE criterion ('_id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, active text, important text, sort int, summary text, user text, category_id int, entrytype int);

The only way I can insert a record into this table is by specifying a value for _id which I want to auto-increment. This is the only way I can get the insert working:

recid = totalrecs + 1;
String q = "insert into criterion (_id, active, important, sort, summary, user, category_id, entrytype) values (" + recid + ", \"1\", \"0\", 99, \"foobar\", \"1\", 99, 0)";
Log.d (TAG, "query:" + q);
mDb.execSQL (q);

If I leave the _id column out and don't specify a value for _id, I get an error:

android.database.sqlite.SQLiteConstraintException: criterion._id may not be NULL: insert into criterion(active, important, sort, summary, user, category_id, entrytype) values ("1", "0", 99, "foobar", "1", 99, 0)

How do I arrange the query (or schema) so Android will take care of incrementing the _id column?

Update/fixed 2 lines above (removed NOT NULL, removed _id from query):

CREATE TABLE criterion ('_id' INTEGER PRIMARY KEY AUTOINCREMENT, active text, important text, sort int, summary text, user text, category_id int, entrytype int);

String q = "insert into criterion (active, important, sort, summary, user, category_id, entrytype) values (\"1\", \"0\", 99, \"foobar\", \"1\", 99, 0)";

解决方案

Remove the NOT NULL from the schema and you're golden.

Clarifying: Specifying NOT NULL on an auto-increment column makes it so the auto-increment doesn't function. The NOT NULL is what is making it so you have to specify the _id.

Once you remove it, and don't include the _id in the insert statement, SQL will receive the _id as NULL and automatically handle setting the _id for you.

这篇关于安卓:用自动增量列插入sqlite的纪录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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