Repository navigation
Expand file tree
/
Copy pathdatabase.cpp
More file actions
143 lines (135 loc) · 4.3 KB
/
Copy pathdatabase.cpp
File metadata and controls
143 lines (135 loc) · 4.3 KB
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
//#include "database.h"
////建立一个数据库连接
//bool Database::createConnection()
//{
// //以后就可以用"sqlite1"与数据库进行连接了
// QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "sqlite1");
// db.setDatabaseName(".//qtDb.db");
// if(!db.open())
// {
// qDebug()<<"can not set up a database";
// 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("database is set up");
// return true;
// }
// else
// {
// qDebug()<<QObject::tr("database is not set up");
// 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, "luxiang");
// query.bindValue(2, "zhaodandan");
// query.bindValue(3, "love");
// 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("Insert Failed"));
// 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";
// }
// return true;
//}
////根据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() << "Delete Failed";
// 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,"luxiang");
// query.bindValue(1,"zhaodandan");
// query.bindValue(2,"love");
// 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("Update Failed"));
// }
// 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("Sort Successfully");
// return true;
// }
// else
// {
// qDebug() << QObject::tr("Sort Failed");
// return false;
// }
//}