Sqlite內嵌到C++程式碼中
SQLite,是一款輕型的資料庫,遵守ACID的關係型資料庫管理系統。它包含在一個相對小的C庫中.
它的設計目標是嵌入式的,而且目前已經在很多嵌入式產品中使用了它,它佔用資源非常的低,比起Mysql、PostgreSQL這兩款開源的世界著名資料庫管理系統來講,它的處理速度比他們都快。
SQLite引擎不是個程式與之通訊的獨立程序,而是連線到程式中成為它的一個主要部分。所以主要的通訊協議是在程式設計語言內的直接API呼叫。這在消耗總量、延遲時間和整體簡單性上有積極的作用。
整個資料庫(定義、表、索引和資料本身)都在宿主主機上儲存在一個單一的檔案中。
它的簡單的設計是通過在開始一個事務的時候鎖定整個
sqlite嵌入到C++中程式碼如下:
//連線資料庫
int HandleData::InitDataBase(char *DB_Name)
{
if(SQLITE_OK != sqlite3_open(DB_Name,&conn_db))
{
printf("Connect databasefailed\n");
return -1;
}else{
printf("Connect databasesuccess!\n");
}
return 0;
}
//檢查表格是否在資料庫中
bool HandleData::CheckTableInDB(char*TableName)
{
char sql_query[1000]={0};
sprintf(sql_query,"select count(*) fromsqlite_master where type='table' and name ='%s'",TableName);
sqlite3_stmt *pstmt;
sqlite3_prepare(conn_db, sql_query,strlen(sql_query), &pstmt, NULL);
sqlite3_step(pstmt);
int count = sqlite3_column_int(pstmt,0);
sqlite3_finalize(pstmt);
printf("table count = %d\n",count);
if(count > 0){
return true;
}else{
return false;
}
}
//建立使用者資訊表
int HandleData::CreateUserInfoTable(char* tableName)
{
//建立表格的SQL語句
char sql[1000] = {0};
if(strlen(tableName) > 20){
return -1;
}
memcpy(UserInfoTable,tableName,strlen(tableName));
sprintf(sql, "CREATE TABLE [%s] (\
[username] varchar(20) PRIMARY KEY,\
[password] varchar(20),\
[question1] varchar(50),\
[answer1] varchar(50),\
[question2] varchar(50),\
[answer2] varchar(50)\
)",tableName);
if (SQLITE_OK != sqlite3_exec(conn_db, sql, 0,0, &err_msg))
{
printf("[CreateUserInfoTable]failed: %s\n", err_msg);
return -2;
}else{
printf("[CreateUserInfoTable]success!\n");
}
return 0;
}
//使用者登入
int HandleData::InsertUserInfo(const UserLoginInfo&loginInfo)
{
// 插入資料的SQL語句
char sql[1000] = {0};
sprintf(sql, "INSERT INTO [%s] (\
[username],[password],[question1],[answer1],[question2],[answer2])\
VALUES ('%s','%s','%s','%s','%s','%s')",
UserInfoTable,
loginInfo.UserName,
loginInfo.Password,
loginInfo.Question1,
loginInfo.Answer1,
loginInfo.Question2,
loginInfo.Answer2);
if (SQLITE_OK != sqlite3_exec(conn_db, sql, 0,0, &err_msg))
{
printf("[InsertUserInfo]failed: %s\n",err_msg);
return -1;
}else{
printf("[InsertUserInfo]success\n");
}
return 0;
}
//使用者刪除
int HandleData::DeleteUserInfo(char *UserName)
{
// 刪除資料的SQL語句
char sql[1000] = {0};
sprintf(sql, "delete from [%s] where [username] ='%s'",UserInfoTable, UserName);
if (SQLITE_OK != sqlite3_exec(conn_db, sql, 0,0, &err_msg))
{
printf("[DeleteUserInfo]failed: %s\n",err_msg);
return -1;
}else{
printf("[DeleteUserInfo]success\n");
}
return 0;
}
//使用者更新
int HandleData::UpdateUserInfo(const UserLoginInfo&loginInfo)
{
// 更新資料的SQL語句
char sql[1000] = {0};
sprintf(sql, "update [%s] \
set [password] = '%s',\
[question1] = '%s',\
[answer1] = '%s',\
[question2] = '%s',\
[answer2] = '%s'\
where [username] = '%s'",
UserInfoTable,
loginInfo.Password,
loginInfo.Question1,
loginInfo.Answer1,
loginInfo.Question2,
loginInfo.Answer2,
loginInfo.UserName);
if (SQLITE_OK != sqlite3_exec(conn_db, sql, 0,0, &err_msg))
{
printf("[UpdateUserInfo]failed: %s\n",err_msg);
return -1;
}else{
printf("[UpdateUserInfo]success\n");
}
return 0;
}
//查詢回撥介面
int sqlite3_exec_callback(void *data, int n_columns,char **col_values, char **col_names)
{
printf("n_columns = %d \n", n_columns);
for (int i = 0; i< n_columns; i++)
{
printf("%s : %s",col_names[i],col_values[i]);
printf("\n");
}
return 0;
}
//使用者查詢
int HandleData::SearchUserInfo(char *UserName)
{
char sql[1000] = {0};
sprintf(sql, "select * from [%s] where [username]= '%s' order by [username] desc",UserInfoTable, UserName);
if (SQLITE_OK != sqlite3_exec(conn_db, sql,&sqlite3_exec_callback, 0, &err_msg))
{
printf("[SearchUserInfo]failed: %s\n",err_msg);
return -1;
}else{
printf("[SearchUserInfo]success\n");
}
return 0;
}
//所有使用者查詢
int HandleData::SearchAllUserInfo()
{
char sql[1000] = {0};
sprintf(sql, "select * from [%s] order by[username] desc",UserInfoTable);
if (SQLITE_OK != sqlite3_exec(conn_db, sql,&sqlite3_exec_callback, 0, &err_msg))
{
printf("[SearchUserInfo]failed: %s\n",err_msg);
return -1;
}else{
printf("[SearchUserInfo]success\n");
}
return 0;
}
//檢查使用者是否存在
bool HandleData::CheckUserIn(char *UserName)
{
char sql_query[1000]={0};
sprintf(sql_query,"select count(*) from [%s]where [username] ='%s'",UserInfoTable, UserName);
sqlite3_stmt *pstmt;
sqlite3_prepare(conn_db, sql_query,strlen(sql_query), &pstmt, NULL);
sqlite3_step(pstmt);
int count = sqlite3_column_int(pstmt,0);
sqlite3_finalize(pstmt);
printf("User Count = %d\n",count);
if(count > 0)
return true;
else{
return false;
}
}