#include <winsqlite/winsqlite3.h>
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <vld.h>
#define SQL_MAX_LENTH 256
#pragma comment(lib,"winsqlite3.lib")
//回调函数一般都是static静态函数,静态函数只能在本文件中调用
static int SQLITE_CALLBACK callback(void *NotUsed, int argc, char **argv, char **azColName)
{
int i = 0;
for (i = 0; i < argc; i++)
{
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
}
printf("\n");
return 0;
}
bool sqlite3_ExecuteSQL(sqlite3* db,char* sql,char*zErrMsg)
{
/* Execute SQL statement */
int ret = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
if (ret != SQLITE_OK)
{
fprintf(stderr, "Error SQL: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
return false;
}
}
int main()
{
sqlite3 *db = NULL;
char * sql = NULL;
char * zErrMsg = NULL;
//打开指定得数据库文件,如果不存在就创建一个同名数据库文件
int r = sqlite3_open((const char*)"PhoneNum.db", &db);
if (r)
{
printf("sqlite3 open fail\n");
}
else
{
printf("sqlite open sucsucess\n");
}
//增删查改
/* Create SQL statement */
//sql = (char*)("CREATE TABLE PhoneNum("
// "PhNum CHAR(11) PRIMARY KEY NOT NULL,"
// "NetN CHAR(3),"
// "ISP TEXT,"
// "AreaN CHAR(4),"
// "City TEXT,"
// "UserN CHAR(4));");
/*sql = (char*)("insert into PhoneNum(PhNum,NetN,ISP,AreaN,City,UserN) \
values(\"13476758962\", \"134\", \"移动\", \"7675\",\"湖北黄冈\", \"8962\");");*/
sql = (char*)"select * from PhoneNum;";
sqlite3_ExecuteSQL(db, sql, zErrMsg);
//关闭数据库
sqlite3_close(db);
system("pause");
return 0;
}