-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.cpp
62 lines (51 loc) · 1.02 KB
/
Database.cpp
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
#include "Database.h"
#include <iostream>
Database::Database(char* filename)
{
database = NULL;
open(filename);
}
Database::~Database()
{
}
bool Database::open(char* filename)
{
if(sqlite3_open(filename, &database) == SQLITE_OK)
return true;
return false;
}
vector<vector<string> > Database::query(char* query)
{
sqlite3_stmt *statement;
vector<vector<string> > results;
if(sqlite3_prepare_v2(database, query, -1, &statement, 0) == SQLITE_OK)
{
int cols = sqlite3_column_count(statement);
int result = 0;
while(true)
{
result = sqlite3_step(statement);
if(result == SQLITE_ROW)
{
vector<string> values;
for(int col = 0; col < cols; col++)
{
values.push_back((char*)sqlite3_column_text(statement, col));
}
results.push_back(values);
}
else
{
break;
}
}
sqlite3_finalize(statement);
}
string error = sqlite3_errmsg(database);
if(error != "not an error") cout << query << " " << error << endl;
return results;
}
void Database::close()
{
sqlite3_close(database);
}