#include <stdio.h>
#include <sqlite3.h>
#include <string.h>
int callback(void *arg,int num,char **f_name,char **f_num)
{
static int flag = 0;
if(flag == 0)
{
for(int i = 0;i < num;i++)
{
printf("%-15s",f_num[i]);
}
flag = 1;
}
printf("\n");
for(int i = 0;i < num;i++)
{
printf("%-15s",f_name[i]);
}
printf("\n");
return 0;
}
int main()
{
sqlite3 *db=NULL;
int ret = sqlite3_open("/mnt/hgfs/sf_share/2024.8.22database/database.db",&db);
if(ret==-1)
{
return -1;
}
while(1)
{
printf("1.遍历数据表 2.增加数据 3.删除数据 4.修改数据 5.退出\n");
int key;
scanf("%d",&key);
getchar();
char buf1[100],buf2[100],buf3[100];
switch(key)
{
case 1:
sqlite3_exec(db,"select *from students;",callback,NULL,NULL);
break;
case 2:
printf("请输入要增加的数据名:\n");
fgets(buf1,100,stdin);
buf1[strcspn(buf1,"\n")]=0;
printf("请输入要增加的数据:\n");
fgets(buf2,100,stdin);
buf2[strcspn(buf2,"\n")]=0;
sqlite3_exec(db,"insert into students(buf1)values(buf2));",NULL,NULL,NULL);
memset(buf1,0,100);
memset(buf2,0,100);
break;
case 3:
printf("请输入要删除的数据名:\n");
fgets(buf1,100,stdin);
buf1[strcspn(buf1,"\n")]=0;
sqlite3_exec(db,"delete from students where name='buf1';",NULL,NULL,NULL);
memset(buf1,0,100);
break;
case 4:
printf("请输入要修改的数据名:\n");
fgets(buf1,100,stdin);
buf1[strcspn(buf1,"\n")]=0;
printf("请输入要修改的数据:\n");
fgets(buf2,100,stdin);
buf2[strcspn(buf2,"\n")]=0;
printf("请输入修改后的数据:\n");
fgets(buf3,100,stdin);
buf3[strcspn(buf3,"\n")]=0;
sqlite3_exec(db,"update students set buf3=buf2 where name=buf1;",NULL,NULL,NULL);
memset(buf1,0,100);
memset(buf2,0,100);
memset(buf3,0,100);
break;
case 5:
break;
}
}
}
08-22
267
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-21
784
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-18
1098
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)