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

相关文章

安装 在Windows上安装SQLite。 访问官网下载下Precompliled Binaries for Windows的两个压缩包。 创建s...
一、安装 下载地址:http://www.sqlite.org/download.html 将Precompiled Binaries for Windows下的包下...
实例: 会员信息管理 功能:1.查看数据库 2.清空数据库 3.增加会员 4.删除会员 5.更新会员 6.查找会员  ...
关于SQLite SQLite是一个轻量的、跨平台的、开源的数据库引擎,它的在读写效率、消耗总量、延迟时间和整...