• 8362阅读
  • 12回复

QT 数据库问题 [复制链接]

上一主题 下一主题
离线314361768
 

只看楼主 倒序阅读 楼主  发表于: 2011-02-15
#include <QtCore/QCoreApplication>
#include <QTextCodec>
#include <QtSql/QtSql>
#include <QDebug>
#include <QString>
#include <QTime>

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("123.db");
//db.setDatabaseName(":memory:");

if(!db.open()){
qDebug()<<QObject::tr("无法打开数据库");
return false;
}

QSqlQuery query;
bool bSuccess = query.exec("CREATE TABLE automobil (id INT PRIMARY KEY,attribute VARCHAR,type VARCHAR,kind VARCHAR,nation INT,carnumber INT,elevaltor INT,distance INT,oil INT,temperature INT)");
if(bSuccess){
qDebug()<<QObject::tr("数据库表创建成功\n");
}else{
qDebug()<<QObject::tr("数据库表创建失败\n");
}
QTime t;
t.start();
query.prepare("INSERT INTO automobil VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
long records = 1000;
for(int i=0;i<records;i++){
query.bindValue(0, i);
query.bindValue(1,"四轮");
query.bindValue(2,"轿车");
query.bindValue(3,"富康");
query.bindValue(4,rand()%100);
query.bindValue(5,rand()%10000);
query.bindValue(6,rand()%300);
query.bindValue(7,rand()%20000);
query.bindValue(8,rand()%152);
query.bindValue(9,rand()%100);
bool bSuccess = query.exec();
if(!bSuccess){
QSqlError lastError = query.lastError();
qDebug()<<lastError.driverText();
qDebug()<<QObject::tr("插入失败");
}
}
qDebug()<<QObject::tr("插入%1 条记录,耗时:%2ms").arg(records).arg(t.elapsed());
t.restart();
bSuccess = query.exec("SELECT * FROM automobil ORDER BY id DESC");
if(bSuccess){
qDebug()<<QObject::tr("排序%1条记录,耗时:%2 ms").arg(records).arg(t.elapsed());
}else{
QSqlError lastError = query.lastError();
qDebug()<<lastError.driverText();
qDebug()<<QObject::tr("排序失败");
}
t.restart();
for(int i = 0;i<records;i++){
query.clear();
query.prepare(QString("UPDATE automobil SET attribute=?,type=?,kind=?,nation=?,carnumber=?,elevaltor=?,distance=?,oil=?,temperature=? WHERE id=%1").arg(i));
query.bindValue(0,"四轮");
query.bindValue(1,"轿车");
query.bindValue(2,"富康");
query.bindValue(3,rand()%100);
query.bindValue(4,rand()%10000);
query.bindValue(5,rand()%300);
query.bindValue(6,rand()%20000);
query.bindValue(7,rand()%52);
query.bindValue(8,rand()%100);
bSuccess = query.exec();
if(!bSuccess){
QSqlError lastError = query.lastError();
qDebug()<<lastError.driverText()<<QString(QObject::tr("更新失败"));
}
}
qDebug()<<QObject::tr("更新%1条记录,耗时:%2 ms").arg(records).arg(t.elapsed());
t.restart();
bSuccess = query.exec("DELETE FROM automobil WHERE id=200");
if(!bSuccess){
qDebug()<<QObject::tr("删除失败");
}else{
qDebug()<<QObject::tr("删除一条记录,耗时:%1 ms").arg(t.elapsed());
}
return a.exec();
}



红色部分时 报错 : 插入失败 Unable to fetch row
绿色部分时 正常

离线314361768

只看该作者 1楼 发表于: 2011-02-15
路过的看看
离线314361768

只看该作者 2楼 发表于: 2011-02-16
路过看看  还是没回复啊
离线314361768

只看该作者 3楼 发表于: 2011-02-16
再看  还是没回复!
离线314361768

只看该作者 4楼 发表于: 2011-02-16
很郁闷啊
离线XChinux

只看该作者 5楼 发表于: 2011-02-16
数据库文件路径是正确的吧?文件不是只读的吧?
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线314361768

只看该作者 6楼 发表于: 2011-02-16
路径没问题 ,有123.db文件 , 不是只读的。
如果把123.db文件删除了 , 第一次执行正确,第二次就出上面的问题了!
离线314361768

只看该作者 7楼 发表于: 2011-02-17
问题解决!
     在第一执行时:
query.prepare("INSERT INTO automobil VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
long records = 1000;
for(int i=0;i<records;i++){
query.bindValue(0, i);
query.bindValue(1,"四轮");
query.bindValue(2,"轿车");
query.bindValue(3,"富康");
query.bindValue(4,rand()%100);
query.bindValue(5,rand()%10000);
query.bindValue(6,rand()%300);
query.bindValue(7,rand()%20000);
query.bindValue(8,rand()%152);
query.bindValue(9,rand()%100);
bool bSuccess = query.exec();
插入的是 id=1,2,3,…………

第二次运行 仍然插入id=1,2,3,………… 插入失败
离线zzh

只看该作者 8楼 发表于: 2011-02-18
db.setDatabaseName("123.db"); 改成
db.setDatabaseName("./123.db"); 应该使用绝对路径
离线314361768

只看该作者 9楼 发表于: 2011-03-02
谢谢
离线mylearnhappy
只看该作者 10楼 发表于: 2011-03-04
SQLite我还没用过,不过也很想尝试使用,对这个不太熟悉,我最近在做QT操作SQL2000的实习,感觉SQL2000数据库与QT类型兼容性不是很好
离线sy1413

只看该作者 11楼 发表于: 2011-03-07
当你第二次使用以前的query的时候,应该要清空query,调用query.clear();
hello world!
离线晓康先森

只看该作者 12楼 发表于: 2015-02-06
把你创建的123.db删除后重新编译,则又能创建
快速回复
限100 字节
 
上一个 下一个