简体   繁体   English

可以只使用一个游标对数据库执行所有操作吗?

[英]Is it okay to use only one cursor to execute all operations with database?

I have a program, which must work with database (and it can work in few threads). 我有一个必须与数据库一起使用的程序(它可以在几个线程中工作)。 I wrote a special class, which starts from: 我写了一个特殊的类,从以下内容开始:

class DataBase:
    def __init__(self):
         self.connection = sqlite3.connect("some_db.db", check_same_thread=False)
         self.cursor = self.connection.cursor()

Is it okay to use such class? 可以使用此类课程吗? Or I should go another way and use few cursors? 还是我应该另辟way径并使用几个游标? My program works fine now and I didn't notice any problems, but I'm not sure can it lead to big problems in future 我的程序现在可以正常运行了,我没有发现任何问题,但是我不确定将来是否会导致大问题

I have sqlite3 working in classes and I don't need it to be self-ed. 我有sqlite3在课堂上工作,我不需要它是自学的。 Ive got it like... 我喜欢...

connection = sqlite3.connect("some_db.db")
c = connection.cursor()

Yeah I think its fine, until you need multiple databases where all you need to do is name you connection and cursors something different like... 是的,在您需要多个数据库之前,您需要做的就是为您的连接命名并使用类似游标的游标...

connection2 = sqlite3.connect("another_db.db")
c2 = connection2.cursor()

You can have them running at the same time too. 您也可以同时运行它们。 Just remember to commit and close when you don't need them anymore. 只要记住当不再需要它们时提交并关闭即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM