qt 数据库操作总结
整理一下 QT 操作数据库的一些资料,以备以后的查询学习(主要是操作 mysql )。
首先,要查询相关的驱动是否已经装好了,可以用以下的程序进行验证:
#include <QtCore/QCoreApplication>
#include <QSqlDatabase>
#include <QDebug>
#include <QStringList>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug()<<"Available drivers:";
QStringList drivers = QSqlDatabase::drivers();
foreach(QString driver, drivers)
qDebug() <<"/t" << driver;
return a.exec();
}
结果如下:
接着是连接数据库:
#include <QtGui/QApplication>
#include <QtGui>
#include <QtSql>
bool createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setDatabaseName("test");
db.setUserName("root");
db.setPassword("123456");
bool ok = db.open();
if(!ok){
QMessageBox::critical(0, QObject::tr(" 连接数据库失败!!! "), db.lastError().text());
return false;
}else{
QMessageBox::information(0, QObject::tr("Tips"), QObject::tr(" 连接数据库成功!!! "));
return true;
}
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTextCodec *codec= QTextCodec::codecForName("GB2312");
QTextCodec::setCodecForLocale(codec);
QTextCodec::setCodecForCStrings(codec);
QTextCodec::setCodecForTr(codec);
if(!createConnection())
return 1;
return a.exec();
}
结果如下:
插入操作:
//ODBC 数据库表示方式
QSqlQuery query;
query.prepare( “insert into student (id, name) ”
“values (:id, :name) ”);
query.bindValue(0, 5);
query.bindValue(1, “sixth ”);
query.exec();
//Oracle 表示方式
query.prepare( “insert into student (id, name) ”
“values (?, ?) ”);
query.bindValue(0, 5);
query.bindValue(1, “sixth ”);
query.exec();
// 使用 addBindValue() 函数,省去了编号,它是按属性顺序赋值的
query.prepare( “insert into student (id, name) ”
“values (?, ?) ”);
query.addBindValue(5);
query.addBindValue( “sixth ”);
query.exec();
// 使用 ODBC 方法时,可以将编号用实际的占位符代替
query.prepare( “insert into student (id, name) ”
“values (:id, :name) ”);
query.bindValue( “:id ”, 5);
query.bindValue( “:name ”, “sixth ”);
query.exec();
注意:最后一定要执行 exec() ,否则上面的语句是不会被执行的。
// 进行多个记录的插入时,可以利用绑定进行批处理
QSqlQuery q;
q.prepare( “insert into student values (?, ?) ”);
QVariantList ints;
ints << 10 << 11 << 12 << 13;
q.addBindValue(ints);
QVariantList names;
names << “xiaoming ” << “xiaoliang ” << “xiaogang ” << QVariant(QVariant::String);
// 最后一个是空字符串,应与前面的格式相同
q.addBindValue(names);
if (!q.execBatch()) // 进行批处理,如果出错就输出错误
qDebug() << q.lastError();
查询操作:
// 返回全部的属性和结果集
QSqlQuery query;
query.exec( “select * from student ”);// 执行查询操作
qDebug() << “exec next() : ”;
if(query.next())
// 开始就先执行一次next() 函数,那么query 指向结果集的第一条记录
{
int rowNum = query.at();
// 获取query 所指向的记录在结果集中的编号
int columnNum = query.record().count();
// 获取每条记录中属性(即列)的个数
int fieldNo = query.record().indexOf( “name ”);
// 获取 ” name ”属性所在列的编号,列从左向右编号,最左边的编号为 0
int id = query.value(0).toInt();
// 获取id 属性的值,并转换为int 型
QString name = query.value(fieldNo).toString();
// 获取name 属性的值
qDebug() << “rowNum is : ” << rowNum // 将结果输出
<< ” id is : ” << id
<< ” name is : ” << name
<< ” columnNum is : ” << columnNum;
}
qDebug() << “exec seek(2) : ”;
if(query.seek(2))
// 定位到结果集中编号为2 的记录,即第三条记录,因为第一条记录的编号为 0
{
qDebug() << “rowNum is : ” << query.at()
<< ” id is : ” << query.value(0).toInt()
<< ” name is : ” << query.value(1).toString();
}
qDebug() << “exec last() : ”;
if(query.last())
// 定位到结果集中最后一条记录
{
qDebug() <<