如何限制sqlite中的行数?我希望将我的数据库限制为10行,并在尝试超过该数字时给出一条消息。这是在创建数据库时还是在查询db时完成的?
创作:
String sqlDataStore = "create table if not exists "
+ TABLE_NAME_INFOTABLE + " ("+ BaseColumns._ID + " integer primary key autoincrement,"
+ COLUMN_NAME_SITE + "text not null,"
+ COLUMN_NAME_ADDRESS + "text not null,"
+ COLUMN_NAME_USERNAME + "text not null,"
+ COLUMN_NAME_PASSWORD + "text not null)";
db.execSQL(sqlDataStore); 查询:
Cursor cursor = database.query(databaseName.TABLE_NAME, null, null, null, null, null, databaseName.COLUMN_NAME, null);发布于 2012-08-27 15:41:06
不能仅用表定义来施加此限制。
办法包括:
发布于 2012-08-27 19:14:48
可以使用SQL中的CHECK约束限制行数。您的应用程序将不得不捕获由于试图添加太多行而导致的错误。还有其他的错误,你的应用程序将不得不陷阱,也。磁盘满,空的使用等。
关键似乎是确保整数ID号实际上是一个整数。SQLite的类型亲缘关系允许您将无意义插入整数列中。
sqlite> create table foo (n integer);
sqlite> insert into foo values ('wibble');
sqlite> select n from foo;
wibble但是,typeof()函数可以保护您不受无稽之谈的影响。
函数的类型是SQLite函数。这不是标准SQL,所以如果您转到另一个dbms,就必须重写下面的CHECK约束。(不太可能是因为您正在为android编程,但其他看到此功能的人可能在不同的环境中工作。)
create table test_limit (
n integer primary key
check (
(typeof(n)='integer') and
(n >=1 and n <= 10)
)
);使用typeof(n)只允许插入整数。范围条件限制了可以插入的整数的数量。在3.7.9版中进行了简要测试。
sqlite> insert into test_limit values (1.13);
Error: datatype mismatch
sqlite> insert into test_limit values (-2);
Error: constraint failed
sqlite> insert into test_limit values ('wibble');
Error: datatype mismatch
sqlite> insert into test_limit values (1);
sqlite> insert into test_limit values (2);
sqlite> insert into test_limit values (17);
Error: constraint failed以后..。
如果您的目标可以表示为“将列'id‘限制为从1到10包含的整数”,这似乎是一个简单、可行的解决方案。我随意使用了您的代码,这样我就可以直接使用SQLite了。你应该仔细看看非键列。由于您没有自然键(除了id号之外,没有一组列是唯一的),所以您实际上没有太多的表。下面的SQL插入应该清楚地说明了这个问题,但这与将表限制为10行不同。
create table test_limit (
id integer primary key autoincrement,
site text not null,
address text not null,
username text not null,
password text not null,
check (
typeof(id) = 'integer' and
(id >= 1 and id <= 10)
)
);
-- 10 inserts.
insert into test_limit (site, address, username, password)
values ('site', 'address', 'username', 'password');
insert into test_limit (site, address, username, password)
values ('site', 'address', 'username', 'password');
insert into test_limit (site, address, username, password)
values ('site', 'address', 'username', 'password');
insert into test_limit (site, address, username, password)
values ('site', 'address', 'username', 'password');
insert into test_limit (site, address, username, password)
values ('site', 'address', 'username', 'password');
insert into test_limit (site, address, username, password)
values ('site', 'address', 'username', 'password');
insert into test_limit (site, address, username, password)
values ('site', 'address', 'username', 'password');
insert into test_limit (site, address, username, password)
values ('site', 'address', 'username', 'password');
insert into test_limit (site, address, username, password)
values ('site', 'address', 'username', 'password');
insert into test_limit (site, address, username, password)
values ('site', 'address', 'username', 'password');尝试重复其中一个插入会导致Error: constraint failed。试图在"id“列中插入一个整数以外的任何内容都会在Error: datatype mismatch中失败。
https://stackoverflow.com/questions/12144948
复制相似问题