1、准备xml的读写库;sqlite数据处理库
2、设计xml文件关系

3、代码实现
(1)xml读取部分代码
void ConfigHandle::_InitTables(TiXmlNode* pParentNode)
{
m_vecTableColumnInfo.clear();
if (pParentNode == NULL) return;
TiXmlNode* pTablesNode = pParentNode->FirstChild("Tables");
if (pTablesNode == NULL) return;
TiXmlNode* pTableNode = pTablesNode->FirstChild("Table");
if (pTableNode == NULL) return;
while (pTableNode != NULL)
{
TableColumn _TableColumn;
TiXmlElement* pItemElement = pTableNode->ToElement();
_TableColumn.strTableName = pItemElement->Attribute("Name");
TiXmlNode* pFieldNode = pItemElement->FirstChild("Field");
while (pFieldNode != NULL)
{
TiXmlElement* pFieldItemElement = pFieldNode->ToElement();
ColumnField _ColumnField;
_ColumnField.strName = pFieldItemElement->Attribute("Name");
int nType = 0;
if (pFieldItemElement->QueryIntAttribute("Type", &nType) != TIXML_SUCCESS)
{
nType = 1;
}
_ColumnField.nType = nType;
_TableColumn.vecColumnField.push_back(_ColumnField);
pFieldNode = pFieldNode->NextSiblingElement("Field");
}
m_vecTableColumnInfo.push_back(_TableColumn);
pTableNode = pTableNode->NextSiblingElement("Table");
}
}
void ConfigHandle::_InitDatas(TiXmlNode* pParentNode)
{
m_vecTableDataInfo.clear();
if (pParentNode == NULL) return;
TiXmlNode* pDatasNode = pParentNode->FirstChild("Datas");
if (pDatasNode == NULL) return;
TiXmlNode* pDataNode = pDatasNode->FirstChild("Data");
if (pDataNode == NULL) return;
while (pDataNode != NULL)
{
TableData _TableData;
TiXmlElement* pItemElement = pDataNode->ToElement();
_TableData.strTableName = pItemElement->Attribute("Name");
TiXmlNode* pItemNode = pItemElement->FirstChild("Item");
while (pItemNode != NULL)
{
TiXmlElement* pItemElement = pItemNode->ToElement();
TableDataItemInfo _TableDataItemInfo;
TiXmlAttribute* attribute = pItemElement->FirstAttribute();
while (attribute)
{
TableDataItem _TableDataItem;
_TableDataItem.strFiledName = attribute->Name();
_TableDataItem.strData = attribute->Value();
_TableDataItemInfo.vecItemData.push_back(_TableDataItem);
attribute = attribute->Next();
}
_TableData.vecTableData.push_back(_TableDataItemInfo);
pItemNode = pItemNode->NextSiblingElement("Item");
}
m_vecTableDataInfo.push_back(_TableData);
pDataNode = pDataNode->NextSiblingElement("Data");
}
}
(2)sqlite数据处理
Kompex::SQLiteDatabase *pDatabase = new Kompex::SQLiteDatabase("test.db", SQLITE_OPEN_READWRITE, 0);
Kompex::SQLiteStatement *pStmt = new Kompex::SQLiteStatement(pDatabase);
pStmt->SqlStatement("DELETE FROM user");
for (auto &_info : m_vecTableData)
{
auto itrTableFind = find_if(m_vecTableColumns.begin(),
m_vecTableColumns.end(), [&](TableColumn &_tableColumn)
{
return _tableColumn.strTableName == _info.strTableName;
});
if (itrTableFind == m_vecTableColumns.end()) continue;
CString strFields;
for (auto &_col : (*itrTableFind).vecColumnField)
{
if (strFields.IsEmpty())
{
strFields = _col.strName;
}
else
{
strFields.Append(L",");
strFields.Append(_col.strName);
}
}
// 行数据
for (auto &_data : _info.vecTableData)
{
CString strTableData;
// 所有列数据
for (auto &_item : _data.vecItemData)
{
CString str1;
int nType = _getType((*itrTableFind).vecColumnField, _item.strFiledName);
switch (nType)
{
case 0: //int
{
str1.Format(L"%d", _wtoi(_item.strData));
break;
}
case 1: //string
{
str1.Format(L"'%s'", _item.strData);
break;
}
}
if (strTableData.IsEmpty())
{
strTableData = str1;
}
else
{
strTableData.Append(L",");
strTableData.Append(str1);
}
}
CString strSQL;
strSQL.Format(L"INSERT INTO %s (%s) VALUES (%s)", _info.strTableName, strFields, strTableData);
string strTmpSQL = CStringA(strSQL);
pStmt->SqlStatement(strTmpSQL.c_str());
}
}
delete pStmt;
delete pDatabase;
4、sqlite插入结果
