本帖最后由 shasidaran 于 2014-8-6 16:03 编辑
发现一处小错误
3.向项目中添加新的C++头文件,名称为connection.h。完成后将其内容更改如下: #ifndef CONNECTION_H #define CONNECTION_H #include <QSqlDatabase> #include <QSqlQuery> static bool createConnection() { QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName("database.db"); if(!db.open()) return false; QSqlQuery query; query.exec(QString( "create tablestudent (id int primary key, name vchar)")); query.exec(QString("insert into student values (0,'刘明')")); query.exec(QString("insert into student values (1,'陈刚')")); query.exec(QString("insert into student values (2,'王红')")); return true; } #endif // CONNECTION_H 这里因为语句中使用了中文,所以使用了QString()进行编码转换,这个还需要在main()函数中设置编码。
在一、3中create tablestudent应该是create table student |