gpt4 book ai didi

python - 创建多个游标

转载 作者:行者123 更新时间:2023-11-29 12:12:41 25 4
gpt4 key购买 nike

是否有执行以下操作的简写?

    # 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()

如果存在类似的东西。

最佳答案

这样更好吗?

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()

关于python - 创建多个游标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30386826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com