简体   繁体   中英

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? 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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