//'QMessageBox' file not found,pro里面添加:QT += sql widgets
//指定数据库的连接名字
//by txwtech
#ifndef SQL_CONNECTION_H
#define SQL_CONNECTION_H
#include <QMessageBox>
#include <QtSql/QSqlDatabase>
#include <QSqlQuery>
//'QMessageBox' file not found,pro里面添加:QT += sql widgets
//指定数据库的连接名字
//by txwtech
static bool createSqlCon()
{
// QSqlDatabase sql_db=QSqlDatabase::addDatabase("QSQLITE");//创建默认连接-一个没有命名的连接
QSqlDatabase sql_db2=QSqlDatabase::addDatabase("QSQLITE","connect1");//创建指定的db1连接名字
QSqlDatabase sql_db21=QSqlDatabase::addDatabase("QSQLITE","connect2");//创建指定的db1连接名字
//sql_db.setDatabaseName(":memory:");//表示数据库在运行期间有效,程序结束自动撤销,不会创建数据库名字
sql_db2.setDatabaseName("tt1.db");//在项目中创建tt.db数据库
if(!sql_db2.open())
{
QMessageBox::critical(0,"无法打开数据库","不能创建数据库",QMessageBox::Cancel);
return false;
}
//QSqlQuery query;//没有指定连接名字,就是默认连接名
QSqlQuery query1(sql_db2);//打开自定数据库连接对象by txwtech
query1.exec("create table student(id int primary key,"
"name varchar(20))"); //创建表时,换行写,直接是双引号连接语句, p383
query1.exec("insert into student values(0,'bob2')");//插入数据
query1.exec("insert into student values(1,'bian2')");//插入数据
query1.exec("insert into student values(2,'machiel2')");//插入数据
sql_db21.setDatabaseName("tt2.db");//在项目中创建tt.db数据库
if(!sql_db21.open())
{
QMessageBox::critical(0,"无法打开数据库","不能创建数据库",QMessageBox::Cancel);
return false;
}
//QSqlQuery query;//没有指定连接名字,就是默认连接名
QSqlQuery query21(sql_db21);//打开自定数据库连接对象by txwtech
query21.exec("create table student(id int primary key,"
"name varchar(20))"); //创建表时,换行写,直接是双引号连接语句, p383
query21.exec("insert into student values(0,'bob21')");//插入数据
query21.exec("insert into student values(1,'bian21')");//插入数据
query21.exec("insert into student values(2,'machiel21')");//插入数据
return true;
}
#endif // SQL_CONNECTION_H
#include <QCoreApplication>
#include "sql_connection.h"
#include <QDebug>
#include <QVariant>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
//创建数据库的连接
qDebug()<<"开始连接";
if(!createSqlCon())
{
qDebug()<<"创建连接失败";
return 1;
}
QSqlDatabase db2 =QSqlDatabase::database("connect1");
QSqlQuery query(db2);//打开默认的连接
query.exec("select * from student");//开始查询
while(query.next())
{
qDebug()<<query.value(0).toInt()<<query.value(1).toString();
}
QSqlDatabase db21 =QSqlDatabase::database("connect2");
QSqlQuery query21(db21);//打开默认的连接
query21.exec("select * from student");//开始查询
while(query21.next())
{
qDebug()<<query21.value(0).toInt()<<query21.value(1).toString();
}
return a.exec();
}
QT -= gui
#CONFIG += c++11 console
CONFIG -= app_bundle
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp
QT+= sql widgets
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
HEADERS += \
sql_connection.h