创建多个光标

2024-05-03 05:20:57 发布

您现在位置:Python中文网/ 问答频道 /正文

做下列事情有速记吗?你知道吗

    # need 3 cursors for reading and writing
    self.cursor1 = self.conn.cursor()
    self.cursor1.execute("SET NAMES utf8")
    self.cursor1.execute('SET CHARACTER SET utf8;')
    self.cursor1.execute('SET character_set_connection=utf8;')

    self.cursor2 = self.conn.cursor()
    self.cursor2.execute("SET NAMES utf8")
    self.cursor2.execute('SET CHARACTER SET utf8;')
    self.cursor2.execute('SET character_set_connection=utf8;')

    self.cursor3 = self.conn.cursor()
    self.cursor3.execute("SET NAMES utf8")
    self.cursor3.execute('SET CHARACTER SET utf8;')
    self.cursor3.execute('SET character_set_connection=utf8;')

基本上,我需要三个光标在脚本中读写,我希望能够做到如下:

cursor_n = self.cursor1.clone()

如果有这样的事情存在的话。你知道吗


Tags: selfexecutenamesutf8connectionconn事情cursor
1条回答
网友
1楼 · 发布于 2024-05-03 05:20:57

这样更好吗?你知道吗

self.cursor = []
for n in range(3):
    self.cursor.append(self.conn.cursor())
    self.cursor[n].execute("SET NAMES utf8")
    self.cursor[n].execute('SET CHARACTER SET utf8;')
    self.cursor[n].execute('SET character_set_connection=utf8;')

或者(我假设cursor()是一个类):

class my_cursor(self.conn.cursor):
    def __init__(self):
        self.execute("SET NAMES utf8")
        self.execute('SET CHARACTER SET utf8;')
        self.execute('SET character_set_connection=utf8;')

self.cursor1 = my_cursor()
self.cursor2 = my_cursor()
self.cursor3 = my_cursor()

相关问题 更多 >