sqlite简单实例
发布时间:2020-12-12 19:33:50 所属栏目:百科 来源:网络整理
导读:#include"stdlib.h"#include"stdio.h"#include"sqlite3.h"intmain(){charcSql[1024]={0};sqlite3*pSql=NULL;char*pError=NULL;inti=0,j=0;char**ppTableData=NULL;intnRow=0,nColumn=0;intpos=0;//打开数据库sqlite3_open("server.db",pSql);//如果userInfo
#include"stdlib.h" #include"stdio.h" #include"sqlite3.h" intmain() { charcSql[1024]={0}; sqlite3*pSql=NULL; char*pError=NULL; inti=0,j=0; char**ppTableData=NULL; intnRow=0,nColumn=0; intpos=0; //打开数据库 sqlite3_open("server.db",&pSql); //如果userInfo表不存在,则创建一个。 sprintf(cSql,"createtableifnotexistsuserInfo" "(" "cUserNamevarchar(32)notnullprimarykey,"//用户名关键字不能为空 "cUserPwdvarchar(32)notnull,"//用户密码不能为空 "nUserPowerintergerdefault1,"//用户权限默认为1 "cCreateTimevarchar(32)default(datetime('now','localtime')),"//创建时间默认为当前本地时间 "cModifyTimevarchar(32)default(datetime('now',"//最后一次修改时间 "cLoginTimevarchar(32)default(datetime('now',"//最后一次登录时间 "cDescribevarchar(256)default('nodescribe')"//用户描述信息 ")"); if(sqlite3_exec(pSql,cSql,&pError)!=SQLITE_OK) { printf("(%s)rn",pError); } //删除一项 sprintf(cSql,"deletefromuserInfowherecUserName='%s'","admin"); if(sqlite3_exec(pSql,pError); } //插入一项UserName=admin,cUserPwd=password sprintf(cSql,"insertintouserInfo(cUserName,cUserPwd)values('%s','%s')","admin","password"); if(sqlite3_exec(pSql,pError); } //修改一项 sprintf(cSql,"updateuserInfosetcUserPwd='%s',nUserPower=%d,cDescribe='%s'wherecUserName='%s'","88888888",2,"superuser",pError); return-1; } //查找所有项,并显示 sprintf(cSql,"select*fromuserInfo"); if(sqlite3_exec(pSql,pError); } else { //获取选择的项目 sqlite3_get_table(pSql,&ppTableData,&nRow,&nColumn,&pError); printf("nRow=%d,nColumn=%drn",nRow,nColumn); pos=nColumn; for(i=0;i<nRow;i++) { for(j=0;j<nColumn;j++) { printf("i%d,j%d,value=%srn",i,j,ppTableData[pos++]); } } //释放空间 sqlite3_free_table(ppTableData); } //查找cUserName=admin,cUserPwd=password的项 sprintf(cSql,"select*fromuserInfowherecUserName='admin'andcUserPwd='password'"); if(sqlite3_exec(pSql,pError); } else { sqlite3_get_table(pSql,&pError); if(nRow<=0) { printf("nofindcUserName='admin'andcUserPwd='password'rn"); } else { printf("findsuccessrn"); pos=nColumn; for(i=0;i<nRow;i++) { for(j=0;j<nColumn;j++) { printf("i%d,ppTableData[pos++]); } } } sqlite3_free_table(ppTableData); } //关闭数据库 sqlite3_close(pSql); return0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |