1 去官网下载:http://www3.sqlite.org/download.html
2 创建数据库和表 :http://www3.sqlite.org/cli.html ,linux 和 windows 环境是不一样的
3 示例代码http://www3.sqlite.org/quickstart.html
一个简单的 C++ 操作类Csqlite.h
#pragma once
#include "sqlite3.h"
class Csqlite
{
public:
Csqlite(void);
virtual ~Csqlite(void);
friend int CallBack(void *lpParameter,int argc,char **argv,char **azColName);
int sqliteOpen(string strFile);
bool ExecuteQuery(string strQuery);
bool ExecuteCmd(string strCmd);
bool GetQueryData(vector<string> &vecColName,vector<vector<string> > &vecData);
bool GetQueryData(vector<string> &vecColName,vector<string> &vecData);
void Close();
private:
sqlite3 *m_pDB;
vector<vector<string> > m_vecData;
vector<string> m_vecCol;
};
Csqlite.cpp
#include "StdAfx.h"
#include "sqlite.h"
m_pDB = NULL;
}
Close();
int Csqlite::sqliteOpen(string strFile)
return sqlITE_OK ==sqlite3_open(strFile.c_str(),&m_pDB);
int CallBack(void *lpParameter,char **azColName)
Csqlite *psqlite = (Csqlite *)lpParameter;
if (NULL == psqlite)
return 0;
vector<string> vecData;
for (int i = 0;i < argc;i++)
{
if (argv[i])
vecData.push_back(argv[i]);
}
else
vecData.push_back(" ");
if (vecData.size() > 0)
psqlite->m_vecData.push_back(vecData);
set<string> setCol;
setCol.insert(psqlite->m_vecCol.begin(),psqlite->m_vecCol.end());
for (int j = 0;j < argc;j++)
if (azColName[j]
&& setCol.find(azColName[j]) == setCol.end()
)
psqlite->m_vecCol.push_back(azColName[j]);
bool Csqlite::ExecuteQuery(string strQuery)
if (NULL == m_pDB)
return false;
m_vecData.clear();
m_vecCol.clear();
char *zErrMsg = 0;
int iRet = sqlite3_exec(m_pDB,strQuery.c_str(),CallBack,this,&zErrMsg);
if( iRet != sqlITE_OK ){
sqlite3_free(zErrMsg);
return iRet == sqlITE_OK;
bool Csqlite::ExecuteCmd(string strCmd)
sqlite3_free(zErrMsg);
bool Csqlite::GetQueryData(vector<string> &vecColName,vector<vector<string> > &vecData)
vector<int> vecIndex;
for (int i = 0;i < (int)vecColName.size();i++)
for (int j = 0;j < (int)m_vecCol.size();j++)
if (_stricmp(vecColName.at(i).c_str(),m_vecCol.at(j).c_str()) == 0)
vecIndex.push_back(j);
break;
for (int i = 0; i < (int)m_vecData.size();i++)
vector<string> vecTemp;
vector<string> vecItem = m_vecData[i];
for (int j = 0;j < (int)vecIndex.size();j++)
int iIndx = vecIndex[j];
if (iIndx < (int)vecItem.size())
vecTemp.push_back(vecItem[iIndx]);
if (vecTemp.size() == vecIndex.size())
vecData.push_back(vecTemp);
return true;
// 索引
if (m_iCurIndex >= (int)m_vecData.size())
m_iCurIndex = 0;
if (_stricmp(vecColName.at(i).c_str(),arial; font-size:14px; line-height:21px"> }
vecData.clear();
vector<string> vecItem = m_vecData[m_iCurIndex];
{
vecData.push_back(vecItem[iIndx]);
m_iCurIndex++;
return true;
void Csqlite::Close()
if (NULL != m_pDB)
sqlite3_close(m_pDB);
m_pDB = NULL;
}
原文链接:https://www.f2er.com/sqlite/199802.html