Browse Source

LIB-12: add test_sqlite3.cc as a playground

LIB-12
heck 8 months ago
parent
commit
cf262572ab
2 changed files with 56 additions and 0 deletions
  1. +2
    -0
      .gitignore
  2. +54
    -0
      test/test_sqlite3.cc

+ 2
- 0
.gitignore View File

@ -25,3 +25,5 @@ test_message_cache
test_passphrase_cache
test_semaphore
test_lm_dummy
test_sqlite3
test.db

+ 54
- 0
test/test_sqlite3.cc View File

@ -0,0 +1,54 @@
#include "../src/sqlite3.h"
#include <iostream>
using namespace std;
sqlite3 *db;
static int callback(void *NotUsed, int argc, char **argv, char **azColName)
{
int i;
for (i = 0; i < argc; i++) {
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
}
printf("\n");
return 0;
}
void execute_sql(string stmt)
{
char *zErrMsg = 0;
int rc = sqlite3_exec(db, stmt.c_str(), callback, 0, &zErrMsg);
if (rc != SQLITE_OK) {
cout << "SQL error: " << zErrMsg << endl;
sqlite3_free(zErrMsg);
}
}
void open_db()
{
int rc = sqlite3_open("test.db", &db);
if (rc) {
cout << "Can't open database:" << sqlite3_errmsg(db) << endl;
} else {
cout << "Opened database successfully" << endl;
}
}
int main(int argc, char *argv[])
{
string sql;
open_db();
sql = "CREATE TABLE GROUPS("
"ADDRESS TEXT PRIMARY KEY NOT NULL,"
"MANAGER_ADDR TEXT NOT NULL);";
execute_sql(sql);
sql = "INSERT INTO GROUPS (ADDRESS, MANAGER_ADDR) "
"VALUES ('fdjklsf@fsffd.com', 'mgr1@fsd.com');";
execute_sql(sql);
sqlite3_close(db);
}

Loading…
Cancel
Save