#include <Windows.h>
#include <stdio.h>
#include "sqlite3.h"
void test_sql_open()
{
int nret = 0;
sqlite3* db = 0;
nret = sqlite3_open("test.db", &db);
if(nret==SQLITE_OK)
printf("test_sql_open: success! sqlite3*:%u /n", db);
else
printf("test_sql_open: failed! sqlite3*:%u /n", db);
sqlite3_close(db);
}
void test_sql_open_create_table()
{
int nret = 0;
sqlite3* db = 0;
nret = sqlite3_open("test.db", &db);
if(nret==SQLITE_OK)
printf("test_sql_open: success! sqlite3*:%u /n", db);
else
printf("test_sql_open: failed! sqlite3*:%u /n", db);
char* errmsg = 0;
nret = sqlite3_exec(db, "create table Table1 (id int, comments varchar(128))", 0, 0, &errmsg);
if(nret==SQLITE_OK)
printf("sqlite3_exec: success! sqlite3*:%u, error message: %s /n", db, errmsg);
else
printf("sqlite3_exec: failed! sqlite3*:%u, error message: %s /n", db, errmsg);
sqlite3_close(db);
}
void test_sql_open_insert_data()
{
int nret = 0;
sqlite3* db = 0;
nret = sqlite3_open("test.db", &db);
if(nret==SQLITE_OK)
printf("test_sql_open: success! sqlite3*:%u /n", db);
else
printf("test_sql_open: failed! sqlite3*:%u /n", db);
char* errmsg = 0;
nret = sqlite3_exec(db, "insert into Table1 (id, comments) values (1, 'Hello, how are you?')", 0, 0, &errmsg);
if(nret==SQLITE_OK)
printf("sqlite3_exec: success! sqlite3*:%u, error message: %s /n", db, errmsg);
else
printf("sqlite3_exec: failed! sqlite3*:%u, error message: %s /n", db, errmsg);
sqlite3_close(db);
}
void test_sql_open_insert_data_using_prepare()
{
int nret = 0;
sqlite3* db = 0;
nret = sqlite3_open("test.db", &db);
if(nret==SQLITE_OK)
printf("test_sql_open: success! sqlite3*:%u /n", db);
else
printf("test_sql_open: failed! sqlite3*:%u /n", db);
sqlite3_stmt *stmt=0;
char* errmsg = 0;
nret = sqlite3_prepare(db, "insert into Table1 (id,comments) values (99,?)", -1, &stmt, 0);
if(nret==SQLITE_OK)
printf("sqlite3_prepare: success! sqlite3*:%u, error message: %s /n", db, errmsg);
else
printf("sqlite3_prepare: failed! sqlite3*:%u, error message: %s /n", db, errmsg);
char str[] = "How do you do?";
nret = sqlite3_bind_text(stmt, 1, str, -1, 0);
if(nret==SQLITE_OK)
printf("sqlite3_prepare: success! sqlite3*:%u, error message: %s /n", db, errmsg);
else
printf("sqlite3_prepare: failed! sqlite3*:%u, error message: %s /n", db, errmsg);
//int id = 888;
//nret = sqlite3_bind_int(stmt, 1, id);
//if(nret==SQLITE_OK)
// printf("sqlite3_prepare: success! sqlite3*:%u, error message: %s /n", db, errmsg);
//else
// printf("sqlite3_prepare: failed! sqlite3*:%u, error message: %s /n", db, errmsg);
nret = sqlite3_step(stmt);
nret = sqlite3_finalize(stmt);
sqlite3_close(db);
}
void test_sql_open_insert_data_using_prepare2()
{
int nret = 0;
sqlite3* db = 0;
nret = sqlite3_open("test.db", &db);
if(nret==SQLITE_OK)
printf("test_sql_open: success! sqlite3*:%u /n", db);
else
printf("test_sql_open: failed! sqlite3*:%u /n", db);
sqlite3_stmt *stmt=0;
char* errmsg = 0;
nret = sqlite3_prepare(db, "insert into Table1 (id,comments) values (?,?)", -1, &stmt, 0);
if(nret==SQLITE_OK)
printf("sqlite3_prepare: success! sqlite3*:%u, error message: %s /n", db, errmsg);
else
printf("sqlite3_prepare: failed! sqlite3*:%u, error message: %s /n", db, errmsg);
char str[] = "How do you do?";
nret = sqlite3_bind_text(stmt, 2, str, -1, 0);
if(nret==SQLITE_OK)
printf("sqlite3_prepare: success! sqlite3*:%u, error message: %s /n", db, errmsg);
else
printf("sqlite3_prepare: failed! sqlite3*:%u, error message: %s /n", db, errmsg);
int id = 888;
nret = sqlite3_bind_int(stmt, 1, id);
if(nret==SQLITE_OK)
printf("sqlite3_prepare: success! sqlite3*:%u, error message: %s /n", db, errmsg);
else
printf("sqlite3_prepare: failed! sqlite3*:%u, error message: %s /n", db, errmsg);
nret = sqlite3_step(stmt);
nret = sqlite3_finalize(stmt);
sqlite3_close(db);
}
int main(__in int _Argc, __in char ** _Argv)
{
printf("Hello db!/n");
//test_sql_open();
//test_sql_open_create_table();
//test_sql_open_insert_data();
test_sql_open_insert_data_using_prepare();
return 0;
}