SQLite第五课 应用案例

前端之家收集整理的这篇文章主要介绍了SQLite第五课 应用案例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1 打开数据库文件

sqlite3* m_db = NULL;

int ret = sqlite3_open_v2("test.db",&db,sqlITE_OPEN_READWRITE,NULL);

if (ret != sqlITE_OK)

{

return;

}


2 如果表不存在,创建表

char szCreateUserDatasql[1024] = "create table if not exists tb_user (id INTEGER,\

type INTEGER,255);"> kind INTEGER)";

3 创建唯一的主键ID

char szCreateUserDatasql[1024] = "create table if not exists tb_user (id INTEGER PRIMARY KEY AUTOINCREMENT,255);">4 查询

char szsql[1024] = {0};

sprintf(szsql,"select distinct * from tb_test");

sqlite3_stmt* stmt = NULL;

sqlite3_prepare(m_db,szsql,-1,&stmt,0);

while (sqlite3_step(stmt) == sqlITE_ROW)

const unsigned char* szPOIName = sqlite3_column_text(stmt,255);"> char szName[128] = {0};

if (szPOIName)

{

sprintf(szName,"%s",szPOIName);

}

int kx = sqlite3_column_int(stmt,3);

float x = sqlite3_column_double(stmt,5);

sqlite3_finalize(stmt);

5 获取sql执行失败的错误信息

char* errMsg = NULL;

char* szsql = "select * from address";

nRet = sqlite3_exec(pDB,NULL,&errMsg);

if (nRet != sqlITE_OK)

{

cout<<errMsg<<endl;

sqlite3_free(errMsg);

}

注意:释放errMsg指向的内存

6 关闭数据库

sqlite3_close(db);

原文链接:https://www.f2er.com/sqlite/198263.html

猜你在找的Sqlite相关文章