Linux下,sqlite简单实例,照猫画虎

编译方法:
1、下载sqlite库:sqlite-autoconf-3070500(本文所使用的)
2、将以下代码保存到sqlite目录下main.c
3、编译:gcc -o main main.c -L.libs -lsqlite3

#include "stdlib.h"
#include "stdio.h"

#include "sqlite3.h"

int main()
{
       char cSql[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 exists userInfo"
              "("
              "cUserName varchar(32) not null primary key ,"//用户名 关键字 不能为空
              "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('no describe') "//用户描述信息
              ")");

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

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

       //插入一项 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)rn", 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)rn", pError);
              return -1;
       }

     //查找所有项,并显示
       sprintf(cSql, "select * from userInfo");
       if(sqlite3_exec(pSql, cSql, 0, 0, &pError) != SQLITE_OK)
       {
              printf("(%s)rn", pError);
       }
       else
       {
            //获取选择的项目
              sqlite3_get_table(pSql, cSql, &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 * from userInfo where cUserName='admin' and cUserPwd='password'");
       if(sqlite3_exec(pSql, cSql, 0, 0, &pError) != SQLITE_OK)
       {
              printf("(%s)rn", pError);
       }
       else
       {
              sqlite3_get_table(pSql, cSql, &ppTableData, &nRow, &nColumn, &pError);
              if(nRow <= 0)
              {
                     printf("no find cUserName='admin' and cUserPwd='password'rn");
              }
              else
              {
                     printf("find successrn");
                     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);
       }

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

       return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值