我正在尝试编写一个简单的程序来连接 MySQL 并执行一些操作
host = '10.0.106.40'
user = 'ddddd'
port = 3306
passwd = 'DDDDDD'
db = 'bbbbbbb'
''' Creates a MySQL connection and returns the cursor '''
def create_connection():
connection = mysql.connect(host, user, passwd, db, port)
cursor = connection.cursor()
return connection, cursor
''' Close the connection '''
def close_connection(cursor, connection):
cursor.close()
connection.commit()
connection.close()
以上功能是我的骨架。现在,当我尝试这样做时
for user in users:
connection, cursor = create_connection()
...
close_connection(cursor, connection)
我收到这个错误
TypeError: connect() argument 2 must be string, not long
但是,当我这样做时
connection, cursor = create_connection()
for user in users:
...
close_connection(cursor, connection)
代码运行得很好!我不确定,但为什么会这样?我真的很想运行早期版本的代码,因为后者对我来说太慢了。