Qt using sqlite database problem

I used MySQL for the database before, which is very convenient, but the program I made recently has to run on the Linux development board, so I used the lightweight database that comes with Qt sqlite, but I encountered some problems during use:

Original code (only relevant code is posted):

head File:

#include <QSqlDatabase> 
QSqlDatabase db; 
public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();
    void DatabaseConnect();

Source File:

void frmMain::DatabaseConnect()
{    //需要进行判断默认的连接名是否存在,
    //如果不存在才使用addDatabase()方法,如果存在则使用database()方法
    if(QSqlDatabase::contains("qt_sql_default_connection"))
        db = QSqlDatabase::database("qt_sql_default_connection");
    else
        db = QSqlDatabase::addDatabase("QSQLITE");
    //连接数据库
    db.setHostName("127.0.0.1"); //数据库服务器IP
    db.setUserName("ente");//用户名
    db.setPassword("000000");//密码
    db.setDatabaseName("ente.db");//使用的数据库,如果不存在,会自动创建
    if(!db.open())
    {
        QMessageBox::warning(this,"Error","Details:DB Open Error!");
    }
    QSqlQuery query;
    QString connectDB = QString("create table entedata (num int primary key auto_increment,time timestamp,ion int,temperature int,humidity int)");
    //新建数据表
    if(!query.exec(connectDB))//数据表创建失败
        {
        QSqlError lastError = query.lastError();
        QMessageBox::warning(this,"数据表创建失败","Details:"+lastError.driverText());
        return;
        } 
}

After the program runs, a dialog box pops up prompting: Database creation failed.
Then I opened the folder and found an empty ente.dbfile, indicating that the database was successfully opened. I thought it was a problem with my SQL statement, and compared it with some reference books. Also put this SQL statement on the MySQL command line and execute it successfully!
With the help of Daniel, I modified the code and finally succeeded:

Changed code (only relevant code is posted)

The header file is the same as the source code
Source file:

void frmMain::DatabaseConnect()
{    //需要进行判断默认的连接名是否存在,
    //如果不存在才使用addDatabase()方法,如果存在则使用database()方法
    if(QSqlDatabase::contains("qt_sql_default_connection"))
        db = QSqlDatabase::database("qt_sql_default_connection");
    else
        db = QSqlDatabase::addDatabase("QSQLITE");
    //连接数据库
    db.setHostName("127.0.0.1"); //数据库服务器IP
    db.setUserName("ente");//用户名
    db.setPassword("000000");//密码
    db.setDatabaseName("ente.db");//使用的数据库
    if(!db.open())
    {
        //QSqlError lastError = query.lastError();
        QMessageBox::warning(this,"Error","Details:DB Open Error!");
    }
    QSqlQuery query; 
    //判断数据表是否存在,不存在则创建
    if(!query.exec("select * from entedata"))
    {
    QString connectDB = QString("create table entedata(num integer primary key autoincrement,time timestamp,ion integer,temperature integer,humidity integer)");
    //创建数据表
    if(!query.exec(connectDB))//数据表创建失败
        {
        QSqlError lastError = query.lastError();
        QMessageBox::warning(this,"数据表创建失败","Details:"+lastError.driverText());
        return;
        }
    } 
    }

The change point is mainly in the SQL statement:
before the change:

create table entedata (num int primary key auto_increment,time timestamp,ion int,temperature int,humidity int)

After the change:

create table entedata(num integer primary key autoincrement,time timestamp,ion integer,temperature integer,humidity integer)

wrong reason:

1. In sqlite, automatic growth is autoincrementnot auto_increment, which is different from MySQL;
2. In sqlite, int can be used as a data type alone, and it is no problem to be a primary key. If it is a primary key and needs autoincrementattributes, it must be
integer primary key autoincrement

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324678241&siteId=291194637