SQLite - C/C++
C, C++ 2021. 8. 27. 16:05 |반응형
C/C++에서 SQLite를 사용해 보자.
sqlite-amalgamation-XXX.zip과 sqlite-dll-win32-x86-XXX.zip를 다운로드하고 압축을 풀어 준다.
sqlite3.def, sqlite3.dll 파일이 있는 sqlite-dll-win32-x86-XXX 폴더에서 아래 명령어를 실행하면 sqlite3.lib 파일이 생성된다.
lib /def:sqlite3.def /machine:x86
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
|
#include <iostream>
#include "sqlite3.h"
#pragma comment(lib, "sqlite3.lib")
using namespace std;
int main()
{
sqlite3* db;
char* ErrMsg = 0;
int rc;
rc = sqlite3_open("test.db", &db);
if (rc == SQLITE_OK) {
cout << "Database opened successfully." << endl;
}
else {
cout << "Can't open database: " << sqlite3_errmsg(db) << endl;
return -1;
}
const char* sql1 = "CREATE TABLE COMPANY(" \
"ID INT PRIMARY KEY NOT NULL, " \
"NAME TEXT NOT NULL, " \
"AGE INT NOT NULL, " \
"ADDRESS CHAR(50), " \
"SALARY REAL);";
rc = sqlite3_exec(db, sql1, NULL, NULL, &ErrMsg);
if (rc != SQLITE_OK) {
cout << "SQL error: " << ErrMsg << endl;
sqlite3_free(ErrMsg);
}
else {
cout << "Table created successfully." << endl;
}
const char* sql2 = "INSERT INTO COMPANY VALUES(1, 'Paul', 32, 'California', 20000.00);" \
"INSERT INTO COMPANY VALUES(2, 'Allen', 25, 'Texas', 15000.00);" \
"INSERT INTO COMPANY VALUES(3, 'Teddy', 23, 'Norway', 20000.00);" \
"INSERT INTO COMPANY VALUES(4, 'Mark', 25, 'Rich-Mond ', 65000.00);";
rc = sqlite3_exec(db, sql2, NULL, NULL, &ErrMsg);
if (rc != SQLITE_OK) {
cout << "SQL error: " << ErrMsg << endl;
sqlite3_free(ErrMsg);
}
else {
cout << "Records created successfully." << endl;
}
sqlite3_close(db);
return 0;
}
|
#include <iostream>
#include "sqlite3.h"
#pragma comment(lib, "sqlite3.lib")
using namespace std;
/*
typedef int (*sqlite3_callback)(
void*, // Data provided in the 4th argument of sqlite3_exec()
int, // The number of columns in row
char**, // An array of strings representing fields in the row
char** // An array of strings representing column names
);
*/
static int callback(void* data, int argc, char** argv, char** azColName) {
int i;
cout << (const char*)data << ":" << endl;
for (i = 0; i < argc; i++) {
cout << azColName[i] << " = " << (argv[i] ? argv[i] : "NULL") << endl;
}
cout << endl;
return 0;
}
int main()
{
sqlite3* db;
char* ErrMsg = 0;
int rc;
rc = sqlite3_open("test.db", &db);
if (rc == SQLITE_OK) {
cout << "Database opened successfully." << endl;
}
else {
cout << "Can't open database: " << sqlite3_errmsg(db) << endl;
return -1;
}
const char* sql = "SELECT * FROM COMPANY;";
const char* data = "Callback function called";
rc = sqlite3_exec(db, sql, callback, (void*)data, &ErrMsg);
if (rc != SQLITE_OK) {
cout << "SQL error: " << ErrMsg << endl;
sqlite3_free(ErrMsg);
}
else {
cout << "Operation done successfully." << endl;
}
sqlite3_close(db);
return 0;
}
|
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
|
#include <iostream>
#include "sqlite3.h"
#pragma comment(lib, "sqlite3.lib")
using namespace std;
/*
typedef int (*sqlite3_callback)(
void*, // Data provided in the 4th argument of sqlite3_exec()
int, // The number of columns in row
char**, // An array of strings representing fields in the row
char** // An array of strings representing column names
);
*/
static int callback(void* data, int argc, char** argv, char** azColName) {
int i;
cout << (const char*)data << ":" << endl;
for (i = 0; i < argc; i++) {
cout << azColName[i] << " = " << (argv[i] ? argv[i] : "NULL") << endl;
}
cout << endl;
return 0;
}
int main()
{
sqlite3* db;
char* ErrMsg = 0;
int rc;
rc = sqlite3_open("test.db", &db);
if (rc == SQLITE_OK) {
cout << "Database opened successfully." << endl;
}
else {
cout << "Can't open database: " << sqlite3_errmsg(db) << endl;
return -1;
}
const char* sql = "UPDATE COMPANY SET SALARY = 25000.00 WHERE ID = 1; " \
"DELETE FROM COMPANY WHERE ID = 2; " \
"SELECT * FROM COMPANY;";
const char* data = "Callback function called";
rc = sqlite3_exec(db, sql, callback, (void*)data, &ErrMsg);
if (rc != SQLITE_OK) {
cout << "SQL error: " << ErrMsg << endl;
sqlite3_free(ErrMsg);
}
else {
cout << "Operation done successfully." << endl;
}
sqlite3_close(db);
return 0;
}
|
반응형
'C, C++' 카테고리의 다른 글
Qt6 설치 및 간단한 사용법 (0) | 2021.09.25 |
---|---|
MariaDB(MySQL) C API (0) | 2021.08.29 |
Linux(Ubuntu) Qt5 Image Display - 리눅스(우분투) Qt5 이미지 디스플레이 (0) | 2021.02.13 |
Linux(Ubuntu) Qt5 GUI Programming - 리눅스(우분투) Qt5 GUI 프로그래밍 (0) | 2021.02.12 |
Linux(Ubuntu) GTK+3(2) Image Display - 리눅스(우분투) GTK+3(2) 이미지 디스플레이 (0) | 2021.02.12 |