David542
David542

Reputation: 110113

Creating multiple cursors

Is there a shorthand for doing the following?

    # 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;')

Basically, I need three cursors for reading and writing in a script, and I'd like to be able to do something like:

cursor_n = self.cursor1.clone()

If something like that exists.

Upvotes: 0

Views: 156

Answers (1)

jcoppens
jcoppens

Reputation: 5440

Is this better?

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;')

or (I suppose cursor() is a class):

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

Upvotes: 1

Related Questions