SQLite使用(1)

13 篇文章 0 订阅
1 篇文章 0 订阅

SQLite下载:

http://download.csdn.net/detail/chuanyu/9671135


1.引用头文件

#include "./SQLite/sqlite3.h"


2.定义变量

sqlite3 *database;


3.创建/打开一个本地文件作为数据库存储

int result = sqlite3_open(“文件路径+文件名”, &database);
if (result != SQLITE_OK)
{
database = NULL;
CCLOG("open db fail: %d", result);
return false;
}


4.执行语句

const char *SQL1 = "CREATE TABLE IF NOT EXISTS TableUser (WriteTime integer NOT NULL, room varchar(255), user1 varchar(255), PRIMARY KEY (WriteTime))";
int result = sqlite3_exec(database, SQL1, NULL, NULL, NULL);
if (result != SQLITE_OK)
{
CCLOG("create TableUser fail :%d", result);
return false;
}


5.获取文件数据库中的信息

char **re;
int row, col;
int result = sqlite3_get_table(database, "SELECT * FROM TableUser", &re, &row, &col, NULL);
if (result != SQLITE_OK)
{
sqlite3_free_table(re);
CCLOG("select data failed!");
return false;
}
//
int nDataIndex = col;
for (int i = 0; i < row; i++)
{
tagDBHistoryUser HistoryUserTemp;
ZeroMemory(&HistoryUserTemp, sizeof(HistoryUserTemp));
HistoryUserTemp.dwWriteTime = str2i(re[nDataIndex]);
memcpy(HistoryUserTemp.szServerName, re[nDataIndex + 1], strlen(re[nDataIndex + 1]) + 1);
for (int j = 2; j < col; j++)
{
if (j >= MAX_RECORD_CHAIR + 2)break;
memcpy(HistoryUserTemp.szNick[j - 2], re[nDataIndex + j], strlen(re[nDataIndex + j]) + 1);
}
HistoryUserList.push_back(HistoryUserTemp);


nDataIndex += col;
}
sqlite3_free_table(re);


6.关闭文件数据库

sqlite3_close(database);
database = NULL;



延伸阅读:

http://blog.csdn.net/linchunhua/article/details/7184439





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值