前端之家收集整理的这篇文章主要介绍了
C语言操作sqlite3对表的插入和查询,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
- #ifndef__MYDB_H
- #define__MYDB_H
- #include<stdio.h>
- #include<string.h>
- #include"sqlite3.h"
- #defineDB_NAME"test.db"
- #defineTABLE_NAME"student"
- #defineTRUE1
- #defineFALSE0
- intopen_db();
- intmodefiy_data();
- intinsert_data();
- intsearch_data();
- #endif
- #include"mydb.h"
- sqlite3*pdb=NULL;
- char*szErrMsg=0;
- intopen_db()
- {
- intrc;
- rc=sqlite3_open(DB_NAME,&pdb);
- if(rc)
- {
- fprintf(stderr,"can'topendatabase:%s\n",sqlite3_errmsg(pdb));
- sqlite3_close(pdb);
- return-1;
- }
- return0;
- }
- intmodefiy_data()
- {
- if(open_db!=0)
- {
- open_db();
- }
- return0;
- }
- intinsert_data()
- {
- intflag=TRUE;
- charbuff[1024];
- char*strsql;
- intrc=0;
- chartemp[3][20];
- strsql=buff;
- if(open_db!=0)
- {
- open_db();
- }
- strcpy(strsql,"insertinto");
- strcat(strsql,TABLE_NAME);
- strcat(strsql,"values(?,?,?);");
- sqlite3_stmt*stmt=NULL;
- rc=sqlite3_prepare_v2(pdb,strlen(strsql),&stmt,NULL);
- if(rc!=sqlITE_OK)
- {
- if(stmt)
- {
- sqlite3_finalize(stmt);
- }
- sqlite3_close(pdb);
- return-1;
- }
- while(flag)
- {
- getchar();
- memset(temp,sizeof(temp));
- printf("学号:");
- scanf("%s",temp[0]);
- printf("姓名:");
- scanf("%s",temp[1]);
- printf("性别:");
- scanf("%s",temp[2]);
- sqlite3_bind_text(stmt,1,temp[0],strlen(temp[0]),NULL);
- sqlite3_bind_text(stmt,2,temp[1],strlen(temp[1]),3,temp[2],strlen(temp[2]),NULL);
- if(sqlite3_step(stmt)!=sqlITE_DONE)
- {
- sqlite3_finalize(stmt);
- sqlite3_close(pdb);
- printf("faile\n");
- return0;
- }
- sqlite3_reset(stmt);
- printf("Doyouwanttodoinsert?[0~1]:");
- scanf("%d",&flag);
- }
- sqlite3_finalize(stmt);
- printf("insertsuccess!\n");
- sqlite3_close(pdb);
- return0;
- }
- intsearch_data()
- {
- intrc;
- if(open_db!=0)
- {
- open_db();
- }
- char*strsql="select*fromstudent";
- sqlite3_stmt*stmt=NULL;
- rc=sqlite3_prepare_v2(pdb,NULL);
- if(rc!=sqlITE_OK)
- {
- if(stmt)
- {
- sqlite3_finalize(stmt);
- }
- sqlite3_close(pdb);
- return-1;
- }
- intnColumn=sqlite3_column_count(stmt);
- intvtype,i;
- do{
- rc=sqlite3_step(stmt);
- if(rc==sqlITE_ROW)
- {
- for(i=0;i<nColumn;i++)
- {
- vtype=sqlite3_column_type(stmt,i);
- if(vtype==sqlITE_INTEGER)
- {
- printf("%s:%d\n",sqlite3_column_name(stmt,i),sqlite3_column_int(stmt,i));
- }
- elseif(vtype==sqlITE_TEXT)
- {
- printf("%s:%s\n",sqlite3_column_text(stmt,i));
- }
- elseif(vtype==sqlITE_NULL)
- {
- printf("novalues\n");
- }
- }
- printf("\n****************\n");
- }
- elseif(rc==sqlITE_DONE)
- {
- printf("Selectfinish\n");
- break;
- }
- else
- {
- printf("Selectfaile\n");
- sqlite3_finalize(stmt);
- break;
- }
- }while(1);
- sqlite3_finalize(stmt);
- sqlite3_close(pdb);
- return0;
- }
- #include<stdio.h>
- #include"mydb.h"
- intmain(intargc,char**argv)
- {
- intn;
- printf("\n1.添加2.修改3.删除4.查看\n");
- printf("choose[1-4]:");
- scanf("%d",&n);
- switch(n)
- {
- case1:
- insert_data();
- break;
- case2:
- printf("修改成功\n");
- break;
- case3:
- printf("删除成功\n");
- break;
- case4:
- search_data();
- printf("查看成功\n");
- break;
- default:
- printf("\nerror\n");
- }
- return0;
- }
原文链接:https://www.f2er.com/sqlite/201737.html