database.h
#ifndef DATABASE_H
#define DATABASE_H
#include <QTextCodec>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QTime>
#include <QSqlError>
#include <QtDebug>
#include <QSqlDriver>
#include <QSqlRecord>
class DataBase
{
public:
bool createConnection();
bool createTable();
bool insert();
bool queryAll();
bool updateById(int id);
bool deleteById(int id);
bool sortById();
};
#endif // DATABASE_H
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
database.cpp
//建立一个数据库连接
bool DataBase::createConnection()
{
//以后就可以用"sqlite1"与数据库进行连接了
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "sqlite1");
db.setDatabaseName(".//qtDb.db");
if( !db.open())
{
qDebug() << "无法建立数据库连接";
return false;
}
return true;
}
//创建数据库表
bool DataBase::createTable()
{
QSqlDatabase db = QSqlDatabase::database("sqlite1"); //建立数据库连接
QSqlQuery query(db);
bool success = 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(success)
{
qDebug() << QObject::tr("数据库表创建成功!\n");
return true;
}
else
{
qDebug() << QObject::tr("数据库表创建失败!\n");
return false;
}
}
//向数据库中插入记录
bool DataBase::insert()
{
QSqlDatabase db = QSqlDatabase::database("sqlite1"); //建立数据库连接
QSqlQuery query(db);
query.prepare("insert into automobil values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
long records = 10;
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()%200000);
query.bindValue(8, rand()%52);
query.bindValue(9, rand()%100);
bool success=query.exec();
if(!success)
{
QSqlError lastError = query.lastError();
qDebug() << lastError.driverText() << QString(QObject::tr("插入失败"));
return false;
}
}
return true;
}
//查询所有信息
bool DataBase::queryAll()
{
QSqlDatabase db = QSqlDatabase::database("sqlite1"); //建立数据库连接
QSqlQuery query(db);
query.exec("select * from automobil");
QSqlRecord rec = query.record();
qDebug() << QObject::tr("automobil表字段数:" ) << rec.count();
while(query.next())
{
for(int index = 0; index < 10; index++)
qDebug() << query.value(index) << " ";
qDebug() << "\n";
}
}
//根据ID删除记录
bool DataBase::deleteById(int id)
{
QSqlDatabase db = QSqlDatabase::database("sqlite1"); //建立数据库连接
QSqlQuery query(db);
query.prepare(QString("delete from automobil where id = %1").arg(id));
if(!query.exec())
{
qDebug() << "删除记录失败!";
return false;
}
return true;
}
//根据ID更新记录
bool DataBase::updateById(int id)
{
QSqlDatabase db = QSqlDatabase::database("sqlite1"); //建立数据库连接
QSqlQuery query(db);
query.prepare(QString("update automobil set attribute=?,type=?,"
"kind=?, nation=?,"
"carnumber=?, elevaltor=?,"
"distance=?, oil=?,"
"temperature=? where id=%1").arg(id));
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()%200000);
query.bindValue(7,rand()%52);
query.bindValue(8,rand()%100);
bool success=query.exec();
if(!success)
{
QSqlError lastError = query.lastError();
qDebug() << lastError.driverText() << QString(QObject::tr("更新失败"));
}
return true;
}
//排序
bool DataBase::sortById()
{
QSqlDatabase db = QSqlDatabase::database("sqlite1"); //建立数据库连接
QSqlQuery query(db);
bool success=query.exec("select * from automobil order by id desc");
if(success)
{
qDebug() << QObject::tr("排序成功");
return true;
}
else
{
qDebug() << QObject::tr("排序失败!");
return false;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
main.cpp
#include <QCoreApplication>
#include "database.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QTextCodec::setCodecForLocale(QTextCodec::codecForLocale());
DataBase d;
d.createConnection();
d.queryAll();
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
运行结果
![这里写图片描述](https://img-blog.csdn.net/20160723223556985)
项目名称为”sqlex”,然后就可以在与当前项目同一级目录下的“build-SQLEx-Desktop_Qt_5_7_0_MinGW_32bit-Debug”中可以看到qtDb.db数据库文件,该数据库可以用软件SQLite直接打开
转自:http://blog.csdn.net/u012319493/article/details/52007473