下载sqllite3 的c++库(我忘记我的是哪个了,反正就那几个文件)
1 初始化
CppSqlite * m_sqlite;
sqlite3 * GetDBPointer() {
return /* m_dictDb */ m_sqlite->GetSqlitePtr();
}
UnicodeString m_path = GetModuleName(0);
UnicodeString fileName = ExtractFileName(m_path);
int pos = m_path.Pos(fileName);
m_path.Delete(pos, fileName.Length());
AnsiString dbPath = m_path + "config.db";
if (OpenDialog1->Execute(this->Handle)) {
dbPath = OpenDialog1->FileName;
}
else
return;
m_sqlite->Open(dbPath.c_str());
2 赋值给cxtreelist
char * sqlStr =
"select distinct SYS_TYPE,TABLE_TYPE,TABLE_NAME, TABLE_DESC, NO from FIELDS order by SYS_TYPE asc,TABLE_TYPE asc, TABLE_NAME asc";
cxTreeListTables->BeginUpdate();
if (ManageForm->strHttppath == "") {
sqlite3_exec(GetDBPointer(), sqlStr,
SqliteCallBack, NULL, NULL);
}
int SqliteCallBack(void *data, int columnCount,
char **columnValues, char **columnNames) {
if (m_SysType != UnicodeString(columnValues[0])) {
TcxTreeListNode * typeNode = m_me->cxTreeListTables->Add();
typeNode->Texts[1] = columnValues[0];
typeNode->Values[3] = false;
typeNode->ImageIndex = 2;
m_SysType = columnValues[0];
if (m_currentNodeA)
m_currentNodeA->Expand(true);
m_currentNodeA = typeNode;
}
if (m_tableType != UnicodeString(columnValues[1])) {
TcxTreeListNode * node = m_currentNodeA->AddChild();
node->Texts[1] = columnValues[1];
node->Values[3] = false;
node->ImageIndex = 3;
m_tableType = columnValues[1];
if (m_currentNode)
m_currentNode->Expand(true);
m_currentNode = node;
}
TcxTreeListNode * nodeA = m_currentNode->AddChild();
nodeA->Texts[1] = columnValues[2];
nodeA->Texts[2] = columnValues[3];
nodeA->Values[3] = false;
nodeA->Texts[4] = columnValues[4];
nodeA->ImageIndex = 15;
return 0;
}
3.如何 读取sqllite3 blob数据
char * buf, *output;
int len;
m_sqlite->ReadBlob(AnsiString(sqlStr).c_str(), buf, len);
if (len == 0)
return "";
m_aes->InvCipher(buf, len, output);//这是我自己的解密函数 AES
UnicodeString rst = output;
delete[]buf;
delete[]output;
return rst;
4.执行SQL语句
int __fastcall Execsql(UnicodeString updateStr) {
int err;
err = ManageForm->m_sqlite->ExecNonQuery(AnsiString(updateStr).c_str());
return err;
}
5 写combox
AnsiString sqlStr = "select ID, DESCRIPTION from session";
TcxComboBoxProperties * prop = (TcxComboBoxProperties*)
this->cxTreeListColumn_kind->Properties;
m_sqlite->ExecSql(sqlStr.c_str());
while (!ManageForm->m_sqlite->IsEOF()) {
prop->Items->Add(UnicodeString
(m_sqlite->GetFieldByIndex(0).c_str() ));
props->Items->Add(UnicodeString
(m_sqlite->GetFieldByIndex(0).c_str() ));
m_sqlite->MoveNext();
}