利用数据库一次插入一万条数据,该如何实现?

jaeson83 2013-04-12 12:05:00
在SQLite数据库中,如何一次性插入10000条记录?我现在的做法是利用网上的方法:
INSERT INTO tableName(col1,col2,col3)
SELECT 3,4,5
UNION ALL
SELECT 6,7,8
UNION ALL
.........
但是实际测试时发现,写了500个SELECT后,插入操作失败,这种方法是否存在限制?性能如何?
有没有其它的方法?
我的代码是这么写的
-----------------------------------------------------------------------------------
// 创建表
bool CSignalResMgr::CreateSignalResTable()
{
int nRet = 0;

tostringstream tos;
tos<<_T("create table ")<<pSignalResTableName
<<_T("(ResID varchar primary key not null,\
ResName varchar,\
ResType unsigned char,\
OutputType unsigned char, \
Model varchar,\
ExtraData varchar,\
Time varchar) ");

nRet = m_db.execDML(/*pSql*/tos.str().c_str(), -1, nRet);

return 0==nRet?true:false;
}

// 向表中插入数据
bool CSignalResMgr::InsertSignalResToDB_N(const int &nBeginIndex, const int &nItemCount)
{
CString strCount;
strCount.Format(_T("---- 插入%d条数据 开始 ----"), nItemCount);
WriteRunInfo(strCount);

int nRet = 0;

SYSTEMTIME tm;
tostringstream tos;
tos<<_T("insert into ")<<pSignalResTableName;
for(int nItemIndex=nBeginIndex; nItemIndex<(nBeginIndex+nItemCount); nItemIndex++)
{
tos<<_T(" select ")<<_T("'")<<nItemIndex<<_T("','")<<_T("SignalResource")<<nItemIndex
<<_T("', ")<<3<<_T(",")<<4<<_T(",'vwas format',''")<<_T(",'");
GetSystemTime(&tm);
tos<<tm.wYear<<_T("-")<<tm.wMonth<<_T("-")<<tm.wDay<<_T(" ")<<tm.wHour<<_T(":")
<<tm.wMinute<<_T(":")<<tm.wSecond<<_T(".")<<tm.wMilliseconds<<_T("'");
if(nItemIndex != nBeginIndex+nItemCount-1)
{
tos<<_T("union all");
}
}

m_db.execDML(tos.str().c_str(), -1, nRet);

if(0 == nRet)
{
strCount.Format(_T("---- 插入%d条数据 成功 ----"), nItemCount);
WriteRunInfo(strCount);
}
else
{
strCount.Format(_T("---- 插入%d条数据 失败 ----"), nItemCount);
WriteRunInfo(strCount);
}

return 0==nRet?true:false;
}

//利用SQLite数据库写数据
int CppSQLite3DB::execDML(const wchar_t* szSQL, std::size_t nLen, int &nRet)
{
try
{
checkDB();

wchar_t* szError=0;

int nRetTemp = sqlite3_exec16(m_pDB, szSQL, -1, 0, NULL, &szError);
sqlite3_free(szError);
nRet = nRetTemp;

if (SQLITE_OK == nRetTemp)
{
return sqlite3_changes(m_pDB);
}
}
catch(...)
{}

return 0;
}
...全文
1117 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
nice_cxf 2013-04-19
  • 打赏
  • 举报
回复
估计是sql的字符串太长了
prohibit 2013-04-19
  • 打赏
  • 举报
回复
我只知道postgresQL有个copy操作,不知道这个有没
jaeson83 2013-04-19
  • 打赏
  • 举报
回复
结帖了,采用事务处理,可以很方便地处理10000条数据

24,860

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧