?1、思維導圖
?2sqlite3在linux中是實現(xiàn)數(shù)據(jù)的增刪,改
#include<myhead.h>
int main(int argc, const char *argv[])
{
? ? ? ? //1、定義一個數(shù)據(jù)庫句柄指針
? ? ? ? sqlite3* ppDb =NULL;
? ? ? ? //2、創(chuàng)建或打開數(shù)據(jù)庫
? ? ? ? if(sqlite3_open("./mydb.db",&ppDb)!=SQLITE_OK)
? ? ? ? {
? ? ? ? ? ? ? ? printf("sqlite3_open error\n");
? ? ? ? ? ? ? ? return -1;
? ? ? ? }
? ? ? ? printf("數(shù)據(jù)庫成功打開\n");
//
? ? ? ? //3、創(chuàng)建數(shù)據(jù)表
? ? ? ? //3.1、準備sql語句
? ? ? ? char sql[128]="create table if not exists student(student_id ?int ,name char,sex char ,score double);";
? ? ? ? char *errmsg =NULL;
? ? ? ? if(sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
? ? ? ? {
? ? ? ? ? ? ? ? printf("%s\n",errmsg);
? ? ? ? ? ? ? ? return -1;
? ? ? ? }
? ? ? ? printf("數(shù)據(jù)表創(chuàng)建成功\n");
?
? ? ? ? int a=0;? ? ? ? int ?a1=0;
? ? ? ? char a2[20]="";
? ? ? ? char a3[5]="";
? ? ? ? int a4=0;
? ? ? ? printf("*****學生信息***********\n");
? ? ? ? printf("*****1、添加學生信息****\n");
? ? ? ? printf("*****2、修改學生信息*****\n");
? ? ? ? printf("*****3、刪除學生信息*****\n");
? ? ? ? printf("*****4、查找學生信息******\n");
? ? ? ? printf("*****0、退出系統(tǒng)**********\n");
? ? ? ? printf("請輸入》》》:");
? ? ? ? scanf("%d",&a);? ? ? ? switch(a)
? ? ? ? {
? ? ? ? case 1:
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? printf("請輸入信息:");
? ? ? ? ? ? ? ? scanf("%d %s %s %d",&a1,a2,a3,&a4);
? ? ? ? ? ? ? ? getchar();
? ? ? ? ? ? ? ? ? ? ? ? sprintf(sql,"insert into student values(%d,\"%s\",\"%s\",%d);",a1,a2,a3,a4);
? ? ? ? ? ? ? ? if(sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? printf("%s\n",errmsg);
? ? ? ? ? ? ? ? ? ? ? ? return -1;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;? ? ? ? ? ? ? ? }
? ? ? ? case 2:
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? printf("請輸入學號和姓名:");
? ? ? ? ? ? ? ? scanf("%d %s %s %d",a1,a2,a3,a4);
? ? ? ? ? ? ? ? getchar();
? ? ? ? ? ? ? ? sprintf(sql,"UPDATE TABLE student student_id=%d AND name=\"%s\" WHERE sex=\"%s\" %d;",a1,a2,a3,a4);
? ? ? ? ? ? ? ? if(sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? printf("%s\n",errmsg);
? ? ? ? ? ? ? ? ? ? ? ? return -1;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? case 3:
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? printf("請輸入信息:");
? ? ? ? ? ? ? ? scanf("%s",a2);
? ? ? ? ? ? ? ? getchar();
? ? ? ? ? ? ? ? sprintf(sql,"DELETE FROM student WHERE name=\"%s\"",a2);
? ? ? ? ? ? ? ? if(sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? printf("%s\n",errmsg);
? ? ? ? ? ? ? ? ? ? ? ? return -1;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }? ? ? ? case 0:
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? sprintf(sql,"DROP TABLE student;");
? ? ? ? ? ? ? ? if(sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? printf("%s\n",errmsg);
? ? ? ? ? ? ? ? ? ? ? ? return -1;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;? ? ? ? ? ? ? ? }
? ? ? ? }文章來源:http://www.zghlxwxcb.cn/news/detail-859771.html? ? ? ? sqlite3_close(ppDb);
? ? ? ? return 0;
}
?文章來源地址http://www.zghlxwxcb.cn/news/detail-859771.html
到了這里,關(guān)于數(shù)據(jù)庫--Sqlite3的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!