Sqlite   发布时间:2022-05-22  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了sqlite3 数据库的简单实用示例大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
1、创建数据库
sqlite3 *db = NulL;
int result = sqlite3_open(db_name,@R_165_4616@; //数据库的名字,和一个sqlite3类型变量

2、创建标头
sprintf(sql,"create table %s (ID int,name varchar,score int)",db_name);
sqlite3_exec(db,sql,NulL,&errMsg);

3、插入
sprintf(sql,"insert into %s values (%d,\"%s\",%d)",
db_name,stu->ID,stu->name,stu->score); //插入的时候,有几列就必须写几样,否则写入不进去数据库

4、打印
(1)sprintf(sql,"SELEct * from %s",db_name);
sqlite3_exec(db,show_myinfo,&errMsg);
(2)sprintf(sql,db_name);
sqlite3_get_table(db,&dbResult,&row,&col,&errMsg);

5、删除
sprintf(sql,"delete from %s where ID=%d",db_name,ID);

6、查找
(1)sprintf(sql,"SELEct * from %s where name='%s'",Name);//查找名字%s必须要加上'',[]加不加都可以,from千万别打错了
sqlite3_exec(db,(void *)sql,"SELEct * from %s where [ID]='%d'",ID);
sqlite3_get_table(db,&errMsg);

7、排序
(1)sprintf(sql,"SELEct * from %s order by name","SELEct * from %s order by ID",&errMsg);

8、关闭数据库
sqlite3_close(db);
demo示例:

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<String.h>
#include"sqlite3.h"

typedef struct str
{
int id;
char name[30];
int score;
}stu;

/****************************************************
*语法格式:int show_myinfo(void *para,@R_607_2532@,
char **column_value,char **column_Name)
*功能描述:是回调函数指针,当这条语句 执行之后,sqlite3会去调用你提供的这个函数。
*参数类型:para=当执行sqlite3_exec的时候传递给回 调函数的参数。
n_column=结果集的列数
column_value=结果集中某一行信息的一维数组 的地址。
column_name=结果集中每列列名的数组的地址。 回调函数的返回值:若为非0值,则通知 sqlite3_exec终止回调。
*返回描述:整形
******************************************************/
int show_myinfo(void *para,
char **column_value,char **column_Name)
{
int i = 0;
//printf("para = %d\n",*((int *)para));
//printf("n_column=%d\n",n_column);
//for(i=0;i<n_column;i++)
//{
//printf("%s\t",column_name[i]);
//}

for(i=0;i<n_column;i++)
{
printf("%s\t",column_value[i]);
}
printf("\n");
return 0;
}

/****************************************************
*语法格式:sqlite3 *create_db(char *db_name)
*功能描述:打开数据库
*参数类型:db_name=数据库文件名,必需是(UTF-8)编码。
*返回描述:返回数据指针
******************************************************/
sqlite3 *create_db(char *db_name)
{
sqlite3 *db = NulL;
int result = sqlite3_open(db_name,@R_165_4616@;
if(result)
{
printf("sqlite3_open %s error\n",db_name);
sqlite3_close(db);
_exit(-1);
}
else
{
printf("create %s succeed!\n",db_name);
}
return db;
}

/****************************************************
*语法格式:voID create_db_table(sqlite3 *db,char *db_name)
*功能描述:创建表头 ID name score
*参数类型:db=数据库指针,db_name=数据库名
*返回描述:无
******************************************************/
voID create_db_table(sqlite3 *db,char *db_name)
{
char sql[100] = "";
char *errmsg = NulL;
sprintf(sql,db_name);
sqlite3_exec(db,&errMsg);

}

/****************************************************
*语法格式:voID insert_db(sqlite3 *db,char *db_name,stu *stu)
*功能描述:插入数
*参数类型:db=数据库指针,db_name=数据库名,stu=插入数据的结构体
*返回描述:无
******************************************************/
voID insert_db(sqlite3 *db,stu *stu)
{
char sql[100] = "";
char *errmsg = NulL;
sprintf(sql,
db_name,stu->score);//插入的时候,有几列就必须写几样,否则写入不进去数据库。不应该啊??

sqlite3_exec(db,&errMsg);

}

/****************************************************
*语法格式:voID print_db_exec(sqlite3 *db,char *db_name)
*功能描述:使用sqlite3_exec函数获取数据库信息,并打印出来
*参数类型:db=数据库指针,db_name=数据库名,
*返回描述:无
******************************************************/
voID print_db_exec(sqlite3 *db,&errMsg);

}
/****************************************************
*语法格式:voID print_db_get_table(sqlite3 *db,char *db_name)
*功能描述:使用SQLite3_get_table函数获取数据库信息,并打印出来
*参数类型:db=数据库指针,db_name=数据库名,
*返回描述:无
******************************************************/
voID print_db_get_table(sqlite3 *db,char *db_name)
{
char sql[100] = "";
char *errmsg = NulL;
char **dbResult = NulL;
int row = 0,col = 0,i = 0,j = 0,index = 0;
sprintf(sql,db_name);
sqlite3_get_table(db,&errMsg);
//printf("ddddddddddrow=%d,col=%d\n",row,col);
for(i=0;i<row+1;i++)
{
for(j=0;j<col;j++)
{
printf("%s\t",dbresult[index]);
++index;
}
printf("\n");
}
}

/****************************************************
*语法格式:voID delete_db_by_ID(sqlite3 *db,int id)
*功能描述:通过ID号删除数据库中的一行数据
*参数类型:db=数据库指针,db_name=数据库名,ID=数据库中的ID号
*返回描述:无
******************************************************/
voID delete_db_by_ID(sqlite3 *db,int id)
{
char sql[100] = "";
char *errmsg = NulL;
sprintf(sql,ID);
sqlite3_exec(db,&errMsg);

}
/****************************************************
*语法格式:voID find_db_by_name_exec(sqlite3 *db,char *Name)
*功能描述:使用sqlite3_exec函数,通过名字查找数据库中的内容
*参数类型:db=数据库指针,db_name=数据库名,name=数据库中的name号
*返回描述:无
******************************************************/
voID find_db_by_name_exec(sqlite3 *db,char *Name)
{
char sql[100] = "";
char *errmsg = NulL;
sprintf(sql,from千万别打错了
sqlite3_exec(db,&errMsg);
}
/****************************************************
*语法格式:voID find_db_by_ID_get_table(sqlite3 *db,int id)
*功能描述:使用SQLite3_get_table函数,通过ID号查找数据库中的内容
*参数类型:db=数据库指针,db_name=数据库名,ID=数据库中的ID号
*返回描述:无
******************************************************/
voID find_db_by_ID_get_table(sqlite3 *db,int id)
{
char sql[100] = "";
char *errmsg = NulL;
char **dbResult = NulL;
int row,col,i,j;
int index = 0;
sprintf(sql,ID);
sqlite3_get_table(db,&errMsg);

//printf("row=%d,col=%d,errmsg=%s\n",errmsg);
for(i=0;i<row+1;i++)
{
for(j=0;j<col;j++)
{
printf("%s\t",dbresult[index++]);
}
printf("\n");
}
}
/****************************************************
*语法格式:voID order_db_by_name_exec(sqlite3 *db,char *db_name)
*功能描述:使用sqlite3_exec函数,通过名字排序数据库中的内容
*参数类型:db=数据库指针,db_name=数据库名
*返回描述:无
******************************************************/
voID order_db_by_name_exec(sqlite3 *db,&errMsg);
}
/****************************************************
*语法格式:voID order_db_by_ID_get_table(sqlite3 *db,char *db_name)
*功能描述:使用SQLite3_get_table函数,通过ID号排序数据库中的内容
*参数类型:db=数据库指针,db_name=数据库名
*返回描述:无
******************************************************/
voID order_db_by_ID_get_table(sqlite3 *db,char *db_name)//排序之后不会改变原来数据中数据的位置
{
char sql[100] = "";
char *errmsg = NulL;
char **dbResult = NulL;
int row,col;
sprintf(sql,&errMsg);

print_db_get_table(db,db_name);
}

int main(voID)
{
sqlite3 *db = NulL;

stu stu;
stu.id = 76;
strcpy(stu.name,"halo");
stu.score = 55;

db = create_db("../test/aaaaaa.db");
create_db_table(db,"aaaaaa");
insert_db(db,"aaaaaa",&stu);

//print_db_exec(db,"aaaaaa");

printf("**********************************\n");

//delete_db_by_ID(db,stu.id);

print_db_get_table(db,"aaaaaa");

//printf("\n**********************************\n");
//find_db_by_name_exec(db,"lihuibo");
//find_db_by_ID_get_table(db,1);

printf("\n**********************************\n");
//order_db_by_name_exec(db,"aaaaaa");

order_db_by_ID_get_table(db,"aaaaaa"); sqlite3_close(db); return 0; }

大佬总结

以上是大佬教程为你收集整理的sqlite3 数据库的简单实用示例全部内容,希望文章能够帮你解决sqlite3 数据库的简单实用示例所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。