· 8 years ago · Aug 19, 2018, 06:18 AM
1open existing sqlite3 database under QT
2void MainWindow::func()
3{
4 QSqlQuery query;
5 accounts_db = new QSqlDatabase();
6 *accounts_db = QSqlDatabase::addDatabase("QSQLITE");
7 perror("? ");
8accounts_db->setDatabaseName("/home/user/xyz.db");
9QSqlError *a = new QSqlError();
10*a = accounts_db->lastError();
11perror(a->text().toLatin1());
12if (!accounts_db->open()) {
13 perror("database open error :");
14}
15if ( !accounts_db->isOpen() ) {
16 perror("database is not open");
17}
18query.exec("select accno,branchcode,fname,lname,curbalance,accdate from accounts");
19while(query.next()) {
20 QString str = query.value(0).toString();
21 std::cerr << qPrintable(str) << std::endl;
22}
23end:
24;
25}
26
27No such file or directory
28: Invalid argument
29QSqlQuery::exec: database not open
30
31QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "connection_name");
32// Open db...
33QSqlQuery query(db);
34if (!query.exec(...)) {
35 // ...
36}
37// ...
38
39#include <QSqlDatabase>
40#include <QSqlQuery>
41#include <QSqlError>
42#include <QVariant>
43
44int main(int argc, char *argv[])
45{
46 // Create database.
47 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "Connection");
48 db.setDatabaseName("/tmp/test.db");
49 if (!db.open()) {
50 qDebug("Error occurred opening the database.");
51 qDebug("%s.", qPrintable(db.lastError().text()));
52 return -1;
53 }
54
55 // Insert table.
56 QSqlQuery query(db);
57 query.prepare("CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, text TEXT)");
58 if (!query.exec()) {
59 qDebug("Error occurred creating table.");
60 qDebug("%s.", qPrintable(db.lastError().text()));
61 return -1;
62 }
63
64 // Insert row.
65 query.prepare("INSERT INTO test VALUES (null, ?)");
66 query.addBindValue("Some text");
67 if (!query.exec()) {
68 qDebug("Error occurred inserting.");
69 qDebug("%s.", qPrintable(db.lastError().text()));
70 return -1;
71 }
72
73 // Query.
74 query.prepare("SELECT * FROM test");
75 if (!query.exec()) {
76 qDebug("Error occurred querying.");
77 qDebug("%s.", qPrintable(db.lastError().text()));
78 return -1;
79 }
80 while (query.next()) {
81 qDebug("id = %d, text = %s.", query.value(0).toInt(),
82 qPrintable(query.value(1).toString()));
83 }
84
85 return 0;
86}
87
88void MainWindow::func()
89{
90 // Note: no pointer!
91 QSqlDatabase accounts_db = QSqlDatabase::addDatabase("QSQLITE");
92 accounts_db.setDatabaseName("/home/user/xyz.db");
93 if (!accounts_db.open())
94 {
95 qDebug() << "Could not open database file:";
96 qDebug() << accounts_db.lastError();
97 return;
98 }
99 // Note: don't construct queries before you have a database!
100 QSqlQuery query;
101 if (!query.exec("select accno,branchcode,fname,lname,curbalance,accdate from accounts"))
102 {
103 qDebug() << "Query failed:";
104 qDebug() << query.lastError();
105 return;
106 }
107 while(query.next()) {
108 QString str = query.value(0).toString();
109 std::cerr << qPrintable(str) << std::endl;
110 }
111}
112
113QSqlError *a = new QSqlError();
114
115accounts_db = new QSqlDatabase();
116*accounts_db = QSqlDatabase::addDatabase("QSQLITE");
117
118accounts_db->setDatabaseName("/home/user/test.db");
119
120if ( !accounts_db->open() ) {
121 qDebug() << accounts_db->lastError();
122 qDebug() << "Could not open database file:";
123}
124QSqlQuery query;
125
126if ( !(accounts_db->isOpen()) ) {
127 qDebug() << accounts_db->lastError();
128 qDebug() << ": Could not open database file:";
129 goto end; // quit if not successful
130}
131
132query.exec("select * from accounts");
133
134while(query.next()) {
135 // loop for i columns
136 QString str = query.value(i).toString();
137 std::cerr << qPrintable(str) << std::endl ;
138 // loop
139}
140end:
141;