在线急等:如何解决c#多线程写入sqlite的 SQLite error (5): database is locked 错误?

tylrr123 2014-06-02 02:22:44
如题,c#+sqlite数据库。程序使用了多线程调用getlinks 函数,写入数据到sqlite里面,但是程序总是报错:


SQLite error (5): database is locked
SQLite error (5): database is locked
SQLite error (5): database is locked

求解决方法,谢谢!



#region //提取搜索结果中的宝贝链接
private void getlinks(object workmode)
{
...................省略部分代码

//写入数据库
try
{
SQLiteConnection mycon = new SQLiteConnection();
SQLiteConnectionStringBuilder connsb = new SQLiteConnectionStringBuilder();
connsb.DataSource = dbName;
mycon.ConnectionString = connsb.ToString();
mycon.Open();
for (int i = 0; i < itemlinks.Count; i++)
{
SQLiteCommand mycmd = new SQLiteCommand();
switch (workmode.ToString())
{
case "a":
mycmd = new SQLiteCommand(string.Format("insert INTO itemdetails(itemid,checkedtime,itemurl) select '{0}','{1}','{2}' where not exists(select itemid,checkedtime from itemdetails where itemid='{0}'); ", getItemid(itemlinks[i]), checkedtime, itemlinks[i]), mycon);
mycmd.ExecuteNonQuery();
mycmd = new SQLiteCommand(string.Format("update itemdetails set zhrank={0} where itemid={1};", i + 1,getItemid(itemlinks[i])), mycon);
mycmd.ExecuteNonQuery();
break;

case "b":
mycmd = new SQLiteCommand(string.Format("insert INTO itemdetails(itemid,checkedtime,itemurl) select '{0}','{1}','{2}' where not exists(select itemid,checkedtime from itemdetails where itemid='{0}'); ", getItemid(itemlinks[i]), checkedtime, itemlinks[i]), mycon);
mycmd.ExecuteNonQuery();
mycmd = new SQLiteCommand(string.Format("update itemdetails set rqrank={0} where itemid={1};", i + 1, getItemid(itemlinks[i])), mycon);
mycmd.ExecuteNonQuery();
break;

case "c":
mycmd = new SQLiteCommand(string.Format("insert INTO itemdetails(itemid,checkedtime,itemurl) select '{0}','{1}','{2}' where not exists(select itemid,checkedtime from itemdetails where itemid='{0}'); ", getItemid(itemlinks[i]), checkedtime, itemlinks[i]), mycon);
mycmd.ExecuteNonQuery();
mycmd = new SQLiteCommand(string.Format("update itemdetails set xlrank={0} where itemid={1};", i + 1, getItemid(itemlinks[i])), mycon);
mycmd.ExecuteNonQuery();
break;

case "d":
mycmd = new SQLiteCommand(string.Format("insert INTO itemdetails(itemid,checkedtime,itemurl) select '{0}','{1}','{2}' where not exists(select itemid,checkedtime from itemdetails where itemid='{0}'); ", getItemid(itemlinks[i]), checkedtime, itemlinks[i]), mycon);
mycmd.ExecuteNonQuery();
mycmd = new SQLiteCommand(string.Format("update itemdetails set tmrank={0} where itemid={1};", i + 1, getItemid(itemlinks[i])), mycon);
mycmd.ExecuteNonQuery();
break;
}
}

mycon.Close(); //关闭数据库连接
}

catch (Exception e)
{
MessageBox.Show(e.ToString());
}


...全文
635 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
tylrr 2014-06-03
  • 打赏
  • 举报
回复
求指点~~~ 谢谢
tylrr123 2014-06-02
  • 打赏
  • 举报
回复
有人指点下吗? 谢谢

2,209

社区成员

发帖
与我相关
我的任务
社区描述
其他数据库开发 其他数据库
社区管理员
  • 其他数据库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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