Initial commit
This commit is contained in:
commit
3119a7ead9
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
*.sqlite
|
||||
*.exe
|
2
Makefile
Normal file
2
Makefile
Normal file
|
@ -0,0 +1,2 @@
|
|||
main.exe: main.cpp
|
||||
g++ -I./lib -L. -lsqlite3 main.cpp -o main.exe
|
7
README.md
Normal file
7
README.md
Normal file
|
@ -0,0 +1,7 @@
|
|||
### Compile:
|
||||
* make main.exe
|
||||
|
||||
### Useful links
|
||||
* https://inloop.github.io/sqlite-viewer/
|
||||
* https://www.tutorialspoint.com/sqlite/sqlite_c_cpp.htm
|
||||
* https://www.sqlite.org/cintro.html
|
12264
lib/sqlite3.h
Normal file
12264
lib/sqlite3.h
Normal file
File diff suppressed because it is too large
Load Diff
127
main.cpp
Normal file
127
main.cpp
Normal file
|
@ -0,0 +1,127 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
#include "sqlite3.h"
|
||||
|
||||
const char *DB_FILE_NAME = "example.sqlite";
|
||||
|
||||
sqlite3 *db = nullptr;
|
||||
sqlite3_stmt *ins_log_stmt = nullptr;
|
||||
|
||||
int create_tables() {
|
||||
char *err_msg = nullptr;
|
||||
int rc = sqlite3_exec(
|
||||
db,
|
||||
"CREATE TABLE IF NOT EXISTS logs("\
|
||||
"id INTEGER PRIMARY KEY AUTOINCREMENT,"\
|
||||
"data TEXT,"\
|
||||
"datetime TIMESTAMP DEFAULT CURRENT_TIMESTAMP"\
|
||||
")",
|
||||
NULL,
|
||||
0,
|
||||
&err_msg
|
||||
);
|
||||
if (rc) {
|
||||
std::cout << "Error: " << err_msg << std::endl;
|
||||
sqlite3_free(err_msg);
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
int prepare_statements() {
|
||||
std::string sql = "INSERT INTO logs (data) VALUES (?);";
|
||||
return sqlite3_prepare_v2(
|
||||
db,
|
||||
sql.c_str(),
|
||||
sql.length(),
|
||||
&ins_log_stmt,
|
||||
nullptr
|
||||
);
|
||||
}
|
||||
|
||||
static int logs_callback(void *data, int argc, char **argv, char **azColName){
|
||||
if (argc == 3) {
|
||||
std::cout << argv[0] << ". (" << argv[2] << ") " << argv[1] << std::endl;
|
||||
} else {
|
||||
std::cout << "Error: Expected 3 columns, got " << argc << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int list_logs() {
|
||||
char *err_msg = nullptr;
|
||||
std::string sql = "SELECT id, data, datetime FROM logs";
|
||||
int rc = sqlite3_exec(db, sql.c_str(), logs_callback, NULL, &err_msg);
|
||||
if (rc != SQLITE_OK) {
|
||||
std::cout << "Error: " << err_msg << std::endl;
|
||||
sqlite3_free(err_msg);
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
int insert_log(std::string data) {
|
||||
int rc = sqlite3_bind_text(
|
||||
ins_log_stmt,
|
||||
1,
|
||||
data.c_str(),
|
||||
data.length(),
|
||||
SQLITE_STATIC
|
||||
);
|
||||
if (rc) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
rc = sqlite3_step(ins_log_stmt);
|
||||
if (rc != SQLITE_DONE) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
rc = sqlite3_reset(ins_log_stmt);
|
||||
if (rc) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int rc = sqlite3_open(DB_FILE_NAME, &db);
|
||||
if (rc) {
|
||||
std::cout << "Error code: " << rc << std::endl;
|
||||
return rc;
|
||||
}
|
||||
|
||||
rc = create_tables();
|
||||
if (rc) {
|
||||
std::cout << "Error code: " << rc << std::endl;
|
||||
return rc;
|
||||
}
|
||||
|
||||
rc = prepare_statements();
|
||||
if (rc) {
|
||||
std::cout << "Error code: " << rc << std::endl;
|
||||
return rc;
|
||||
}
|
||||
|
||||
rc = list_logs();
|
||||
if (rc) {
|
||||
std::cout << "Error code: " << rc << std::endl;
|
||||
return rc;
|
||||
}
|
||||
|
||||
rc = insert_log("wolf (test log)");
|
||||
if (rc) {
|
||||
std::cout << "Error code123: " << rc << std::endl;
|
||||
return rc;
|
||||
}
|
||||
|
||||
rc = insert_log("lion (test log2)");
|
||||
if (rc) {
|
||||
std::cout << "Error code: " << rc << std::endl;
|
||||
return rc;
|
||||
}
|
||||
|
||||
sqlite3_close(db);
|
||||
|
||||
return 0;
|
||||
}
|
BIN
sqlite3.dll
Normal file
BIN
sqlite3.dll
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user