- #include"mydb.h"
- sqlite3*pdb=NULL;
- char*szErrMsg=0;
- /*
- *@brief//打开数据库
- *@parampdb类型为sqlite
- */
- 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;
- }
- /*
- //@brif数据库语句执行
- intcreat_table()
- {
- intrc;
- char*strsql="createtablestudent(sidvarchar(10)notnullprimarykey,snamevarchar(10)notnull,timeDatetime)";
- sqlite3_exec(pdb,strsql,&szErrMsg);
- if(rc!=0)
- {
- 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]);
- //getchar();
- 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();
- //printf("添加成功\n");
- break;
- case2:
- printf("修改成功\n");
- break;
- case3:
- printf("删除成功\n");
- break;
- case4:
- search_data();
- printf("查看成功\n");
- break;
- default:
- printf("\nerror\n");
- }
- return0;
- }
原文地址:https://www.jb51.cc/sqlite/201737.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。