Linux下,sqlite简单实例

来源:互联网 发布:2017程序员考试真题 编辑:程序博客网 时间:2024/05/17 04:18
#include "stdlib.h"
#include "stdio.h"

#include "sqlite3.h"

int main()
{
 
   charcSql[1024] = {0};
    sqlite3*pSql = NULL;
    char *pError= NULL;
    int i = 0, j= 0;
    char**ppTableData = NULL;
    int nRow =0, nColumn = 0;
    int pos =0;

   //打开数据库
   sqlite3_open("server.db",&pSql);

   //如果userInfo表不存在,则创建一个。
   sprintf(cSql, "create table if not existsuserInfo"
      "("
      "cUserName varchar(32) not null primarykey,"//用户名 关键字 不能为空
      "cUserPwd varchar(32) not null,"//用户密码 不能为空
      "nUserPower interger default 1,"//用户权限 默认为1
      "cCreateTime varchar(32)default(datetime('now','localtime')),"//创建时间 默认为当前本地时间
      "cModifyTime varchar(32)default(datetime('now','localtime')),"//最后一次修改时间
      "cLoginTime varchar(32)default(datetime('now','localtime')),"//最后一次登录时间
      "cDescribe varchar(256) default('nodescribe')"//用户描述信息
      ")");

   if(sqlite3_exec(pSql, cSql, 0, 0,&pError) != SQLITE_OK)
    {
      printf("(%s)\r\n", pError);
    }

    //删除一项
   sprintf(cSql, "delete from userInfo where cUserName='%s'","admin");
   if(sqlite3_exec(pSql, cSql, 0, 0, &pError) !=SQLITE_OK)
    {
      printf("(%s)\r\n", pError);
    }

    //插入一项UserName = admin, cUserPwd = password
   sprintf(cSql, "insert into userInfo (cUserName,cUserPwd) values ('%s', '%s')", "admin", "password");
   if(sqlite3_exec(pSql, cSql, 0, 0,&pError) != SQLITE_OK)
    {
      printf("(%s)\r\n", pError);
    }

   //修改一项
   sprintf(cSql, "update userInfo set cUserPwd='%s', nUserPower=%d,cDescribe='%s' where cUserName='%s'", "88888888", 2, "super user","admin");
   if(sqlite3_exec(pSql, cSql, 0, 0, &pError) !=SQLITE_OK)
    {
      printf("(%s)\r\n", pError);
       return-1;
    }

   //查找所有项,并显示
   sprintf(cSql, "select * from userInfo");
   if(sqlite3_exec(pSql, cSql, 0, 0,&pError) != SQLITE_OK)
    {
      printf("(%s)\r\n", pError);
    }
    else
    {
     //获取选择的项目
      sqlite3_get_table(pSql, cSql,&ppTableData, &nRow,&nColumn, &pError);
      printf("nRow  = %d, nColumn = %d\r\n", nRow, nColumn);
      pos = nColumn;
      for(i = 0;i < nRow;i++)
      {
         for(j = 0;j < nColumn;j++)
         {
            printf("i %d, j %d, value = %s\r\n", i, j,ppTableData[pos++]);
         }
      }
      //释放空间
      sqlite3_free_table(ppTableData);
    }

   //查找cUserName = admin,cUserPwd = password的项
   sprintf(cSql, "select * from userInfo wherecUserName='admin' and cUserPwd='password'");
   if(sqlite3_exec(pSql, cSql, 0, 0,&pError) != SQLITE_OK)
    {
      printf("(%s)\r\n", pError);
    }
    else
    {
      sqlite3_get_table(pSql, cSql,&ppTableData, &nRow,&nColumn, &pError);
      if(nRow <= 0)
      {
         printf("no find cUserName='admin' andcUserPwd='password'\r\n");
      }
      else
      {
         printf("find success\r\n");
         pos = nColumn;
         for(i = 0;i < nRow;i++)
         {
            for(j = 0;j < nColumn;j++)
            {
               printf("i %d, j %d, value = %s\r\n", i, j,ppTableData[pos++]);
            }
         }
      }
      sqlite3_free_table(ppTableData);
    }

   //关闭数据库
   sqlite3_close(pSql);

    return0;
}
0 0
原创粉丝点击