如何在每个SQLite行中插入唯一的ID? [英] How to insert a unique ID into each SQLite row?
本文介绍了如何在每个SQLite行中插入唯一的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下SQLite代码。如何在每行中插入自动生成唯一ID?
tx.executeSql('DROP TABLE IF EXISTS ENTRIES');
tx.executeSql('CREATE TABLE IF NOT EXISTS ENTRIES(id unique,data)');
tx.executeSql('INSERT INTO ENTRIES(id,data)VALUES(1,First row)');
tx.executeSql('INSERT INTO ENTRIES(id,data)VALUES(2,Second row)');
解决方案
您可以定义 id
作为自动增加列:
create table entries(id integer primary key autoincrement,data)
MichaelDorner指出, SQLite文档说,整数主键
会同样的事情和稍快。该类型的列是 rowid
的别名,的行为类似于自动增量列。
创建表项(id整数主键,数据)
这种行为是隐含的,可能会导致无经验的SQLite开发人员无所作为。
I have the following SQLite code. How do I insert an auto generating unique ID into each row?
tx.executeSql('DROP TABLE IF EXISTS ENTRIES'); tx.executeSql('CREATE TABLE IF NOT EXISTS ENTRIES (id unique, data)'); tx.executeSql('INSERT INTO ENTRIES (id, data) VALUES (1, "First row")'); tx.executeSql('INSERT INTO ENTRIES (id, data) VALUES (2, "Second row")');
解决方案You could define
id
as an auto increment column:create table entries (id integer primary key autoincrement, data)
As MichaelDorner notes, the SQLite documentation says that an
integer primary key
does the same thing and is slightly faster. A column of that type is an alias forrowid
, which behaves like an autoincrement column.create table entries (id integer primary key, data)
This behavior is implicit and could catch inexperienced SQLite developers off-guard.
这篇关于如何在每个SQLite行中插入唯一的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文