前端之家收集整理的这篇文章主要介绍了
Sqlite的操作(增加,删除,查询,修改),
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
int OpenDB(sqlite3** db)
{
int result = sqlite3_open(DBNAME,db);
#ifdef DEBUG
if(result!=0)
{
printf("Open DataBase Fail,Result Code:%d \n",result);
}
else
{
printf("Open DataBase Sucess\n");
}
#endif
return result;
}
int CloseDB(sqlite3* db)
{
int result = sqlite3_close(db);
#ifdef DEBUG
if(result!=0)
{
printf("Close DataBase fail,ResultCode:%d \n",result);
}
else
{
printf("Close DataBase Success\n");
}
#endif
return result;
}
创建表:
//创建表操作
void CreateTable()
{
sqlite3 *db;
sqlite3_stmt *stmt = NULL;
char * ErrMessage = NULL;
const char * Error = NULL;
char * str_CreateTable="Create Table Student ( studentID INTEGER,studentName Text,HeadImage Blob)"; //创建表的sql语句
int rc =0;
if(OpenDB(&db)!=sqlITE_OK)
{
return;
}
rc = sqlite3_exec(db,str_CreateTable,&ErrMessage); //执行
if(rc!=sqlITE_OK)
{
printf("操作数据库失败!ErrorMessge:%s\n",ErrMessage);
return;
}
sqlite3_finalize(stmt);
CloseDB(db);
}
Insert操作
//Insert操作
void InsertOperation(void)
{
sqlite3 *db;
sqlite3_stmt *stmt = NULL;
char * ErrMessage = NULL;
const char * Error = NULL;
char * str_Insert="Insert Into Student ( [studentID],[studentName],[HeadImage]) values(1,'Alex',?)"; //sql语句
int rc =0;
if(OpenDB(&db)!=sqlITE_OK)
{
return;
}
rc = sqlite3_prepare(db,str_Insert,-1,&stmt,&Error);
if(rc!=sqlITE_OK)
{
printf("准备执行插入语句失败!Result Code:%d \n ErrorMessage:%s",rc,Error);
return;
}
char _headImage[3] = {0x41,0x42,0x43};
sqlite3_bind_blob(stmt,1,&_headImage,3,NULL); //二进制数据
rc = sqlite3_step(stmt);
if(rc!=sqlITE_DONE)
{
printf("插入失败!Result Code:%d ErrorMessage:%s",Error);
return;
}
printf("插入成功!\n");
CloseDB(db);
}
Update操作:
//Insert操作
void InsertOperation(void)
{
sqlite3 *db;
sqlite3_stmt *stmt = NULL;
char * ErrMessage = NULL;
const char * Error = NULL;
char * str_Insert="Insert Into Student ( [studentID],Error);
return;
}
printf("插入成功!\n");
CloseDB(db);
}
Delete操作:
void DeleteOperation(void)
{
sqlite3 *db;
sqlite3_stmt *stmt = NULL;
char * ErrMessage = NULL;
const char * Error = NULL;
char * str_Update="Delete From Student where [studentID] =1"; //sql语句
int rc =0;
if(OpenDB(&db)!=sqlITE_OK)
{
return;
}
rc = sqlite3_prepare(db,str_Update,&Error);
if(rc!=sqlITE_OK)
{
printf("准备执行删除语句失败!Result Code:%d \n ErrorMessage:%s",Error);
return;
}
rc = sqlite3_step(stmt);
if(rc!=sqlITE_DONE)
{
printf("删除失败!Result Code:%d ErrorMessage:%s",Error);
return;
}
printf("删除成功!\n");
CloseDB(db);
}
Select操作:
void SelectOperation(void)
{
sqlite3 *db;
sqlite3_stmt *stmt = NULL;
char * ErrMessage = NULL;
const char * Error = NULL;
char * str_Update="select * From Student "; //sql语句
int rc =0;
if(OpenDB(&db)!=sqlITE_OK)
{
return;
}
rc = sqlite3_prepare(db,&Error);
if(rc!=sqlITE_OK)
{
printf("准备执行查询语句失败!Result Code:%d \n ErrorMessage:%s",Error);
return;
}
while(1)
{
if(sqlite3_step(stmt)!=sqlITE_ROW)
{
sqlite3_finalize(stmt);
break;
}
int studentid =sqlite3_column_int(stmt,0);
const char * studentName = sqlite3_column_text(stmt,1);
const void * image = sqlite3_column_blob(stmt,2);
printf("studentID is %d,Name is %s,HeadImage is %s \n",studentid,studentName,image);
}
CloseDB(db);
}
原文链接:https://www.f2er.com/sqlite/198504.html