QT7 How to connect Qt to SQLite

前端之家收集整理的这篇文章主要介绍了QT7 How to connect Qt to SQLite前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1. create a qt widgets application,name it as sqlite_DB,modify the class name as Login

2. switch to ui designer,drag a label onto the window used for showing the prompting information

3. modify the login.h file as follows:

#ifndef LOGIN_H
#define LOGIN_H




#include <QMainWindow>
#include <Qtsql>
#include <QDebug>
#include <QFileInfo>



namespace Ui {
class login;
}

class login : public QMainWindow
{
    Q_OBJECT

public:
    explicit login(QWidget *parent = 0);
    ~login();

private:
    Ui::login *ui;
};

#endif // LOGIN_H

4. modify the login.cpp file as follows:
#include "login.h"
#include "ui_login.h"

login::login(QWidget *parent) :
    QMainWindow(parent),ui(new Ui::login)
{
    ui->setupUi(this);

    QsqlDatabase mydb = QsqlDatabase::addDatabase("QsqlITE");
    mydb.setDatabaseName("C:/work_files/sqlite-tools-win32-x86-3110100/company.db");//note: the separator used in the path must be forward slash,or errors will occur 

    if(!mydb.open())
        ui->label->setText("Failed to open the database");
    else
        ui->label->setText("Connected...");
}

login::~login()
{
    delete ui;
}

5. run the project

猜你在找的Sqlite相关文章