SQLite主键自增代码
在使用Qt编码创建SQLite数据库表的时候遇到问题。
需求:创建一个自增ID的的student表
1.当ID无需自增时使用代码如下,可创建表student:
query.exec("create table student(id int primary key , name vchar, sex vchar, age int, deparment vchar)");
2.在上边的代码基础上直接加AUTOINCREMENT,无法创出自增的表student,代码如下:
query.exec("create table student(id int primary key AUTOINCREMENT, name vchar, sex vchar, age int, deparment vchar)");
3.此时需要将int修改为INTEGER即可,代码如下:
query.exec("create table student(id INTEGER primary key AUTOINCREMENT, name vchar, sex vchar, age int, deparment vchar)");
可创建ID自增的表student
4.插入数据代码如下:
query.exec("insert into student values(NULL,'张三','男',12,'1111')");