利用数据库一次插入一万条数据,该如何实现?
在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;
}
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;
}
...全文
相关推荐
详解C#把DataTable中数据一次插入数据库的方法
本篇文章主要介绍了详解C#把DataTable中数据一次插入数据库的方法,具有一定的参考价值,有兴趣的可以了解一下。
用java程序怎么实现200ms往数据库中插入10000条数据
用java程序怎么实现200ms往数据库中插入10000条数据
NBA球员数据管理系统数据库课程设计
NBA球员数据管理系统数据库课程设计
mybatis 批量将list数据插入到数据库的实现
主要介绍了mybatis 批量将list数据插入到数据库的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
利用datagridview控件实现批量输入数据并存入数据库
利用datagridview控件实现批量输入数据并存入数据库


工具平台和程序库
