test integrate SQLite

This commit is contained in:
Hongze Cheng 2021-12-01 17:10:40 +08:00
parent 881dcf0dbf
commit e2ea6c5158
3 changed files with 63 additions and 2 deletions

View File

@ -46,7 +46,7 @@ option(
option( option(
BUILD_DEPENDENCY_TESTS BUILD_DEPENDENCY_TESTS
"If build dependency tests" "If build dependency tests"
OFF ON
) )
option( option(

5
deps/CMakeLists.txt vendored
View File

@ -96,6 +96,11 @@ if(${BUILD_WITH_SQLITE})
IMPORTED_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}/sqlite/.libs/libsqlite3.a" IMPORTED_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}/sqlite/.libs/libsqlite3.a"
INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/sqlite" INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/sqlite"
) )
target_link_libraries(sqlite
INTERFACE m
INTERFACE pthread
INTERFACE dl
)
endif(${BUILD_WITH_SQLITE}) endif(${BUILD_WITH_SQLITE})

View File

@ -1,6 +1,62 @@
#include <stdio.h> #include <stdio.h>
#include "sqlite3.h"
int main(int argc, char const *argv[]) { int main(int argc, char const *argv[]) {
printf("Hello world!\n"); sqlite3 *db;
char * err_msg = 0;
int rc = sqlite3_open("test.db", &db);
if (rc != SQLITE_OK) {
fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
char *sql =
"DROP TABLE IF EXISTS t;"
"CREATE TABLE t(id BIGINT);";
rc = sqlite3_exec(db, sql, 0, 0, &err_msg);
if (rc != SQLITE_OK) {
fprintf(stderr, "SQL error: %s\n", err_msg);
sqlite3_free(err_msg);
sqlite3_close(db);
return 1;
}
// Write a lot of data
int nrows = 100000;
int batch = 1000;
char tsql[1024];
int v = 0;
for (int k = 0; k < nrows / batch; k++) {
sqlite3_exec(db, "begin;", 0, 0, &err_msg);
for (int i = 0; i < batch; i++) {
v++;
sprintf(tsql, "insert into t values (%d)", v);
rc = sqlite3_exec(db, tsql, 0, 0, &err_msg);
if (rc != SQLITE_OK) {
fprintf(stderr, "SQL error: %s\n", err_msg);
sqlite3_free(err_msg);
sqlite3_close(db);
return 1;
}
}
sqlite3_exec(db, "commit;", 0, 0, &err_msg);
}
sqlite3_close(db);
return 0; return 0;
} }