1. 程式人生 > >SQLite主鍵自增程式碼

SQLite主鍵自增程式碼

                                        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')");