加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

QT7 How to connect Qt to SQLite

发布时间:2020-12-12 19:32:31 所属栏目:百科 来源:网络整理
导读: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#

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读