#include <QApplication>
#include <QSqlDatabase>
#include <QSqlError>
#include <QDebug>
#include <QStringList>
#include <QMessageBox>
#include <QSqlQuery>
#include <thread>
#include <QVariant>
#include <QSqlDriver>
#include <QSqlRecord>
#include <QSqlField>
int main(int argc,char* argv[])
{
QApplication a(argc,argv);
qDebug()<<"Available drivers:";
QStringList drivers = QSqlDatabase::drivers();
for(auto item:drivers)
{
qDebug()<<item;
}
QSqlDatabase db=QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName(QString("DRIVER={SQL SERVER};"
"SERVER=%1;" //服务器名称
"DATABASE=%2;"//数据库名
"UID=%3;" //登录名
"PWD=%4;" //密码
).arg("192.168.1.9")//192.168.1.252
.arg("test")//数据库名 TSOMV2 TSOMV2 testdsn
.arg("sa")//登录名
.arg("sa")//密码
);
if (!db.open())
{
QMessageBox::critical(0, qApp->tr("Cannot open database"),
db.lastError().databaseText(), QMessageBox::Cancel);
}
//创建一个表,并添加记录
QSqlQuery query;
bool result = query.exec("create table student(id int primary key, name varchar(20))");
if(!result)
{
qDebug()<<query.lastError().databaseText();
}
query.exec("insert into student values(0,'LiMing')");
if(!result)
{
qDebug()<<query.lastError().databaseText();
}
query.exec("insert into student values(1,'LiuTao')");
if(!result)
{
qDebug()<<query.lastError().databaseText();
}
query.exec("insert into student values(2,'WangHong')");
if(!result)
{
qDebug()<<query.lastError().databaseText();
}
//查询这个表
//query.setForwardOnly(true);//如果只使用next(),加快结果集的查询速度
query.exec("select * from student");
while(query.next())
{
qDebug()<<query.value(0).toInt()<<query.value(1).toString();
}
//获取查询的记录数next()定位下一条记录,previous()定位到前一条记录,first()定位到第一条记录,last()定位到最后一条记录
//seek(n)定位到第n条记录
//
int numRows;
if(db.driver()->hasFeature(QSqlDriver::QuerySize))
{
qDebug()<<"has feature:query size";
numRows = query.size();
}
else
{
qDebug()<<"no feature:query size";
query.last();
numRows = query.at()+1;
}
qDebug()<<numRows;
query.seek(1);//指向索引为1的记录,即第二条记录
qDebug()<<"current index:"<<query.at();//获取当前行的索引值
//获取当前行的记录
QSqlRecord record = query.record();
int id = record.value("id").toInt();
QString name = record.value("name").toString();
qDebug()<<"id:"<<id<<" name:"<<name;
//获取索引为1的字段,即第2个字段
QSqlField field = record.field(1);
qDebug()<<"second field:"<<field.name()<<"filed value:"<<field.value().toString();
//名称绑定
query.prepare("insert into student(id,name) values(:id,:name)");
int idValue = 100;
QString nameValue= "ChenYun";
query.bindValue(":id",idValue);
query.bindValue(":name",nameValue);
query.exec();
//位置绑定
query.prepare("insert into student(id,name) values(?,?)");
idValue = 101;
nameValue = "LiQiang";
query.addBindValue(idValue);
query.addBindValue(nameValue);
query.exec();
//插入多条记录
query.prepare("insert into student(id,name) values(?,?)");
QVariantList ids;
ids<<20<<21<<22;
query.addBindValue(ids);
QVariantList names;
names<<"xiaoming"<<"xiaoliang"<<"xiaogang";
query.addBindValue(names);
if(!query.execBatch())
{
qDebug()<<query.lastError();
}
//删除一条记录
query.exec("delete from student where id = 21");
// 数据库事务
if(db.driver()->hasFeature(QSqlDriver::Transactions))
{
qDebug()<<"has feature:QSqlDriver::Transactions";
QSqlDatabase::database().transaction();
//更新一条记录
if(!query.exec("update student set name = 'xiaohong' where id = 20"))
{
qDebug()<<query.lastError().databaseText();
qDebug()<<"update failed,rool back";
//db.rollback();
}
if(!query.exec("insert into student values(0,'LiMing')"))
{
qDebug()<<query.lastError().databaseText();
qDebug()<<"insert failed,rool back";
//db.rollback();
}
QSqlDatabase::database().commit();
}
else
{
qDebug()<<"no feature:QSqlDriver::Transactions";
}
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
db.close();//关闭数据库
QSqlDatabase::removeDatabase(db.connectionName());//关闭连接
return a.exec();
}