Linux下,sqlite简单实例
生活随笔
收集整理的這篇文章主要介紹了
Linux下,sqlite简单实例
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
#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;
?? //打開數(shù)據(jù)庫
???sqlite3_open("server.db",&pSql);
?? //如果userInfo表不存在,則創(chuàng)建一個。
???sprintf(cSql, "create table if not existsuserInfo"
??????"("
??????"cUserName varchar(32) not null primarykey,"//用戶名 關(guān)鍵字 不能為空
??????"cUserPwd varchar(32) not null,"//用戶密碼 不能為空
??????"nUserPower interger default 1,"//用戶權(quán)限 默認(rèn)為1
??????"cCreateTime varchar(32)default(datetime('now','localtime')),"//創(chuàng)建時(shí)間?默認(rèn)為當(dāng)前本地時(shí)間
??????"cModifyTime varchar(32)default(datetime('now','localtime')),"//最后一次修改時(shí)間
??????"cLoginTime varchar(32)default(datetime('now','localtime')),"//最后一次登錄時(shí)間
??????"cDescribe varchar(256) default('nodescribe')"//用戶描述信息
??????")");
???if(sqlite3_exec(pSql, cSql, 0, 0,&pError) != SQLITE_OK)
?? ?{
??????printf("(%s)\r\n", pError);
?? ?}
??? //刪除一項(xiàng)
???sprintf(cSql, "delete from userInfo where cUserName='%s'","admin");
???if(sqlite3_exec(pSql, cSql, 0, 0, &pError) !=SQLITE_OK)
??? {
??????printf("(%s)\r\n", pError);
??? }
??? //插入一項(xiàng)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);
?? ?}
?? //修改一項(xiàng)
???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;
??? }
?? //查找所有項(xiàng),并顯示
???sprintf(cSql, "select * from userInfo");
???if(sqlite3_exec(pSql, cSql, 0, 0,&pError) != SQLITE_OK)
?? ?{
??????printf("(%s)\r\n", pError);
?? ?}
?? ?else
?? ?{
?????//獲取選擇的項(xiàng)目
??????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的項(xiàng)
???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);
?? ?}
?? //關(guān)閉數(shù)據(jù)庫
???sqlite3_close(pSql);
?? ?return0;
}
#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;
?? //打開數(shù)據(jù)庫
???sqlite3_open("server.db",&pSql);
?? //如果userInfo表不存在,則創(chuàng)建一個。
???sprintf(cSql, "create table if not existsuserInfo"
??????"("
??????"cUserName varchar(32) not null primarykey,"//用戶名 關(guān)鍵字 不能為空
??????"cUserPwd varchar(32) not null,"//用戶密碼 不能為空
??????"nUserPower interger default 1,"//用戶權(quán)限 默認(rèn)為1
??????"cCreateTime varchar(32)default(datetime('now','localtime')),"//創(chuàng)建時(shí)間?默認(rèn)為當(dāng)前本地時(shí)間
??????"cModifyTime varchar(32)default(datetime('now','localtime')),"//最后一次修改時(shí)間
??????"cLoginTime varchar(32)default(datetime('now','localtime')),"//最后一次登錄時(shí)間
??????"cDescribe varchar(256) default('nodescribe')"//用戶描述信息
??????")");
???if(sqlite3_exec(pSql, cSql, 0, 0,&pError) != SQLITE_OK)
?? ?{
??????printf("(%s)\r\n", pError);
?? ?}
??? //刪除一項(xiàng)
???sprintf(cSql, "delete from userInfo where cUserName='%s'","admin");
???if(sqlite3_exec(pSql, cSql, 0, 0, &pError) !=SQLITE_OK)
??? {
??????printf("(%s)\r\n", pError);
??? }
??? //插入一項(xiàng)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);
?? ?}
?? //修改一項(xiàng)
???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;
??? }
?? //查找所有項(xiàng),并顯示
???sprintf(cSql, "select * from userInfo");
???if(sqlite3_exec(pSql, cSql, 0, 0,&pError) != SQLITE_OK)
?? ?{
??????printf("(%s)\r\n", pError);
?? ?}
?? ?else
?? ?{
?????//獲取選擇的項(xiàng)目
??????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的項(xiàng)
???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);
?? ?}
?? //關(guān)閉數(shù)據(jù)庫
???sqlite3_close(pSql);
?? ?return0;
}
總結(jié)
以上是生活随笔為你收集整理的Linux下,sqlite简单实例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 信用卡临时额度可以分期还款吗
- 下一篇: 使用内存文件系统