问个python多线程操作sqlite的问题

shiweifu 2010-05-04 07:11:24

class db_manger(Thread):
"""用来操作数据库和维护表"""
def __init__(self, dbname):
self.dbname = dbname

Thread.__init__(self, name="database manger")

def run(self):
try:
self.conn = sqlite3.connect(sqlpath)
self.conn.text_factory = str
except:
printl("数据库连接错误")
sys.exit() )

s = "insert into stuinfo(stuid,clasid,fetid,extenid,name,sex,phone,mail) values(1,4,'testdate',0,'float',1,'','')"
c = self.conn.cursor()
try:
c.execute(s)
time.sleep(1)
except:
sys.exit("提交出错")
printl("执行")



这段代码连接数据库和提交语句,应该是在一个线程中阿。。为啥我执行的时候还是会说不在一个线程中?求教
...全文
694 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
angel_su 2010-05-05
  • 打赏
  • 举报
回复
SQLite Frequently Asked Questions

(6) Is SQLite threadsafe?

Threads are evil. Avoid them.

SQLite is threadsafe. We make this concession since many users choose to ignore the advice given in the previous paragraph. But in order to be thread-safe, SQLite must be compiled with the SQLITE_THREADSAFE preprocessor macro set to 1. Both the Windows and Linux precompiled binaries in the distribution are compiled this way. If you are unsure if the SQLite library you are linking against is compiled to be threadsafe you can call the sqlite3_threadsafe() interface to find out.

Prior to version 3.3.1, an sqlite3 structure could only be used in the same thread that called sqlite3_open() to create it. You could not open a database in one thread then pass the handle off to another thread for it to use. This was due to limitations (bugs?) in many common threading implementations such as on RedHat9. Specifically, an fcntl() lock created by one thread cannot be removed or modified by a different thread on the troublesome systems. And since SQLite uses fcntl() locks heavily for concurrency control, serious problems arose if you start moving database connections across threads.

The restriction on moving database connections across threads was relaxed somewhat in version 3.3.1. With that and subsequent versions, it is safe to move a connection handle across threads as long as the connection is not holding any fcntl() locks. You can safely assume that no locks are being held if no transaction is pending and all statements have been finalized.

Under Unix, you should not carry an open SQLite database across a fork() system call into the child process. Problems will result if you do.
angel_su 2010-05-05
  • 打赏
  • 举报
回复
sqlite没那么不堪吧,试了lz的代码起了10线程能跑的,唯一不同的地方是线程最后有commit/close,lz的没看是不是作了,如果在主线程里才提交关闭可能会报错吧...
shiweifu 2010-05-05
  • 打赏
  • 举报
回复
upupupup
shiweifu 2010-05-05
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 xiaokang007 的回复:]

SQLite的局限性:SQLITE本来就是单机版使用的数据库解决方案,不能用于网络,就是说他的并发支持很弱.当如果需要并发支持,请采用大型网络数据库.


有推荐使用 python bsddb 的,这里有点参考:http://tech.ddvip.com/2009-06/1245203960124157.html
[/Quote]

这个好像只支持k-v的方式,而且v必须是个字符串。。所以局限性比较大,还是学习了。
shiweifu 2010-05-05
  • 打赏
  • 举报
回复
调用代码:

db = db_manger(sqlpath)
db.setDaemon(True)
db.start()
db.join()


请教。。我是在一个线程中操作的呀……
xiaokang007 2010-05-05
  • 打赏
  • 举报
回复
SQLite的局限性:SQLITE本来就是单机版使用的数据库解决方案,不能用于网络,就是说他的并发支持很弱.当如果需要并发支持,请采用大型网络数据库.


有推荐使用 python bsddb 的,这里有点参考:http://tech.ddvip.com/2009-06/1245203960124157.html
thy38 2010-05-05
  • 打赏
  • 举报
回复
看起来是没什么问题,出错信息是什么?
shiweifu 2010-05-05
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 angel_su 的回复:]

SQLite Frequently Asked Questions

(6) Is SQLite threadsafe?

Threads are evil. Avoid them.

SQLite is threadsafe. We make this concession since many users choose to ignore the advice g……
[/Quote]

学习了,已经解决,是因为没有commit,疏忽了
thy38 2010-05-04
  • 打赏
  • 举报
回复
光看类可看不出来,要看上下文,也许你是在多个线程中实例化了它。sqlite不支持跨线程操作数据库

37,721

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

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