-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlite3.cpp
61 lines (55 loc) · 1.23 KB
/
sqlite3.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
#include <iostream>
#include <sqlite3.h>
#include <string>
#include <strstream>
int
main(int argc, char** argv)
{
std::cout << "SQLite version: " << sqlite3_version << std::endl;
sqlite3* db;
char* errmsg;
int rc = sqlite3_open("test.sqlite3", &db);
if (rc != 0)
{
std::cerr << "error: " << rc << std::endl;
return 1;
}
std::strstream sql;
sql << "select * from test";
if (argc > 1)
{
if (argc > 4)
{
// insert
std::strstream v;
v << "insert into test values(";
v << argv[1] << ", '" << argv[2] << "', " << argv[3] << ", '" << argv[4];
v << "')";
auto str = v.str();
sqlite3_exec(db, str, 0, 0, &errmsg);
return 0;
}
else
{
sql << " " << argv[1];
}
}
rc = sqlite3_exec(
db, sql.str(),
[](void*, int argc, char** argv, char** col) {
int i;
for (i = 0; i < argc; i++)
std::cout << col[i] << " = " << (argv[i] ? argv[i] : "null") << " /";
std::cout << std::endl;
return 0;
},
0, &errmsg);
std::cout << "req done." << std::endl;
if (rc != 0)
{
std::cerr << "error: " << errmsg << std::endl;
}
// データベースを閉じる
sqlite3_close(db);
return 0;
}