利用数据库一次插入一万条数据,该如何实现?
在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;
}