You can limit the number of rows using a CHECK constraint in the SQL DDL. Your application will have to trap the error that results from trying to add too many rows. There are other errors your app will have to trap, too. Disk full, use of NULL, etc.
The key point seems to be guaranteeing that the integer ID number is, in fact, an integer. SQLite's type affinities let you insert nonsense into an integer column.
sqlite> create table foo (n integer);
sqlite> insert into foo values ('wibble');
sqlite> select n from foo;
wibble
But the typeof() function can protect you from nonsense.
The typeof() function is a SQLite function. It's not standard SQL, so you'll have to rewrite the CHECK constraint below if you move to another dbms. (Not very likely since you're programming for android, but others who see this might be working in a different environment.)
create table test_limit (
n integer primary key
check (
(typeof(n)='integer') and
(n >=1 and n <= 10)
)
);
Using typeof(n)
allows inserting only integers. The range condition limits the number of integers you can insert. Briefly tested in version 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
Later...
This seems be a simple, workable solution if your goal can be expressed as "limit the column 'id' to integers from 1 to 10 inclusive". I took some liberties with your code, so I could work directly in SQLite. You should look hard at the non-key columns. Since you have no natural key (no set of columns are unique except the id number), you don't really have much of a table. My SQL inserts below should make that problem clear, but it's a different problem than limiting the table to 10 rows.
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');
Attempting to repeat one of these inserts results in Error: constraint failed
. Attempting to insert anything but an integer into the "id" column fails with Error: datatype mismatch
.