sqlite3使用
Posted Overboom
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sqlite3使用相关的知识,希望对你有一定的参考价值。
/*
@brief 本程序测试sqlite数据库的增删改查
@date 2012-09-03
*/
// SQLiteTest.cpp : Defines the entry point for the console application.
//
//#include "stdafx.h"
#include "sqlite3.h"
#include <iostream>
using namespace std;
sqlite3 * pDB = NULL;
bool CreateTable();
//增加用户
bool AddUser(const string& sName, const string& sAge);
//删除用户
bool DeleteUser(const string& sName);
//修改用户
bool ModifyUser(const string& sName, const string& sAge);
//查找用户
bool SelectUser();
int main(void)
{
//打开路径采用utf-8编码
//如果路径中包含中文,需要进行编码转换
CreateTable();
int nRes = sqlite3_open("test.db", &pDB);
if (nRes != SQLITE_OK)
{
cout<<"Open database fail: "<<sqlite3_errmsg(pDB);
goto QUIT;
}
//添加“赵钱孙李”
if ( !AddUser("zhao", "18")
|| !AddUser("qian", "19")
|| !AddUser("sun", "20")
|| !AddUser("li", "21"))
{
goto QUIT;
}
//删除“赵”
if (!DeleteUser("zhao"))
{
goto QUIT;
}
//修改“孙”
if (!ModifyUser("sun", "15"))
{
goto QUIT;
}
cout << "find the user" << endl;
//查找用户
if (!SelectUser())
{
goto QUIT;
}
QUIT:
sqlite3_close(pDB);
return 0;
}
static int 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 CreateTable()
{
char *zErrMsg = 0;
int rc;
char *sql;
rc = sqlite3_open("test.db", &pDB); // 打开数据库
if(rc)
{
fprintf(stderr, "Can't open database:%s\\n", sqlite3_errmsg(pDB));
return false;
}
else
{
fprintf(stderr,"open database succeddfully\\n");
}
// sql = "create table company( "\\
// "ID INT PRIMARY KEY NOT NULL,"\\
// "NAME TEXT NOT NULL,"\\
// "AGE TEXT NOT NULL);";
sql = "CREATE TABLE user(" \\
"NAME TEXT NOT NULL," \\
"AGE INT NOT NULL," \\
"ADDRESS CHAR(50)," \\
"SALARY REAL );";
rc = sqlite3_exec(pDB, sql, callback, 0, &zErrMsg); // 执行上面sql中的命令
if(SQLITE_OK != rc)
{
fprintf(stderr, "SQL error: %s\\n", zErrMsg);
}
else
{
fprintf(stdout, "create table successfully\\n");
}
}
bool AddUser(const string& sName, const string& sAge)
{
string strSql = "";
strSql += "insert into user(name,age)";
strSql += "values('";
strSql += sName;
strSql += "',";
strSql += sAge;
strSql += ");";
char* cErrMsg;
int nRes = sqlite3_exec(pDB , strSql.c_str() ,0 ,0, &cErrMsg);
if (nRes != SQLITE_OK)
{
cout<<"add user fail: "<<cErrMsg<<endl;
return false;
}
else
{
cout<<"add user success: "<<sName.c_str()<<"\\t"<<sAge.c_str()<<endl;
}
return true;
}
bool DeleteUser(const string& sName)
{
string strSql = "";
strSql += "delete from user where name='";
strSql += sName;
strSql += "';";
char* cErrMsg;
int nRes = sqlite3_exec(pDB , strSql.c_str() ,0 ,0, &cErrMsg);
if (nRes != SQLITE_OK)
{
cout<<"delete user fail: "<<cErrMsg<<endl;
return false;
}
else
{
cout<<"delete user success: "<<sName.c_str()<<endl;
}
return true;
}
bool ModifyUser(const string& sName, const string& sAge)
{
string strSql = "";
strSql += "update user set age =";
strSql += sAge;
strSql += " where name='";
strSql += sName;
strSql += "';";
char* cErrMsg;
int nRes = sqlite3_exec(pDB , strSql.c_str() ,0 ,0, &cErrMsg);
if (nRes != SQLITE_OK)
{
cout<<"modify user fail: "<<cErrMsg<<endl;
return false;
}
else
{
cout<<"modify user success: "<<sName.c_str()<<"\\t"<<sAge.c_str()<<endl;
}
return true;
}
// static int UserResult(void *NotUsed, int argc, char **argv, char **azColName)
// {
// for(int i = 0 ; i < argc ; i++)
// {
// cout<<azColName[i]<<" = "<<(argv[i] ? argv[i] : "NULL")<<", ";
// }
// cout<<endl;
// return 0;
// }
// static int UserResult(void *data, int argc, char **argv, char **azColName){
// int i;
// fprintf(stderr, "%s: ", (const char*)data);
// for(i=0; i<argc; i++){
// printf("%s = %s\\n", azColName[i], argv[i] ? argv[i] : "NULL");
// }
// printf("\\n");
// return 0;
// }
bool SelectUser()
{
char* cErrMsg;
int res = sqlite3_exec(pDB, "select * from user;", callback , 0 , &cErrMsg);
if (res != SQLITE_OK)
{
cout<<"select fail: "<<cErrMsg<<endl;
return false;
}
cout << "find the user end---" << endl;
return true;
}
以上是关于sqlite3使用的主要内容,如果未能解决你的问题,请参考以下文章