c++ 将xml数据写入sqlite数据库

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插入结果

相关推荐
0+1111 小时前
MySQL --基本查询
数据库·mysql
inferno2 小时前
十九、性能优化
数据库·sql·mysql
底层玩家老张4 小时前
绿盟和金仓共建七个月的复盘:适配记录、两个场景、交付与人
数据库·嵌入式数据库·日志检索·产品适配·主备读写分离
Mojitocean4 小时前
主流数据库对比与业务场景选型指南
数据库
武子康5 小时前
GameEval第一轮先测什么:正常对照、已知故障与未知探索
大数据·数据库·人工智能
代码不停5 小时前
Linux基础知识
java·linux·数据库
stormsha8 小时前
MySQL 连不上:2003 / 1045 / 1040 三张面孔与一个排查顺序
java·数据库·mysql
撸码猿8 小时前
老系统只读出口:MySQL 秒级时间戳当水位,3,375 次变更漏 817 行
数据库·mysql·数据一致性·数据同步·增量更新
衡石科技9 小时前
SQL 由模型写出来之后:行级安全在问数链路上的四个注入点
数据库·sql·安全·数据分析
真上帝的左手10 小时前
10. 软件设计&架构-架构师之路-102.MySQL秒级扩容
数据库·mysql·架构·系统架构·架构设计·高可扩展性