python 连接mysql 语句_python mysql连接器多语句

本文档展示了使用python的mysql.connector库连接MySQL数据库并执行一系列更新和修改列数据类型的SQL语句。在尝试更改邮政编码数据类型时遇到错误:`mysql.connector.errors.InterfaceError: Use multi=True when executing multiple statements`。尽管其他语句成功执行,但最后一个更改列数据类型的语句在不使用`multi=True`时失败。已经尝试了多种解决方案,包括新建连接、光标、设置`multi=True`等,但问题仍未解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import mysql.connector

connection = mysql.connector.connect(user="REMOVED",

password="REMOVED",

host="REMOVED",

database="REMOVED")

cur = connection.cursor()

# Latitude - remove letter A

cur.execute("UPDATE tau._inm_exportados_test_csv SET latitud = REPLACE (latitud, 'a=','');")

print("Latitude change remove letter A - executed!")

# Longitude - remove letter A

cur.execute("UPDATE tau._inm_exportados_test_csv SET longitud = REPLACE (longitud, 'a=','');")

print("Longitude change remove letter A - executed!")

# Latitude - MODIFY COLUMN

cur.execute("ALTER TABLE tau._inm_exportados_test_csv MODIFY COLUMN latitud DECIMAL(10,6);")

print("Latitude - MODIFY COLUMN - executed!")

# Longitude - MODIFY COLUMN

cur.execute("ALTER TABLE tau._inm_exportados_test_csv MODIFY COLUMN longitud DECIMAL(10,6);")

print("Longitude - MODIFY COLUMN - executed!")

# Post Code data type change

cur.execute("ALTER TABLE tau._inm_exportados_test_csv MODIFY COLUMN codigo_postal varchar(255);)")

print("Post Code data type change to varchar(255) - executed!")

connection.commit()

cur.close()

connection.close()

我试图让这个简单的陈述清单没有成功。更让人困惑的是,前四个语句有效,而最后一个语句甚至在我注释掉其余语句时也不起作用!最后的语句得到以下响应:mysql.connector.errors.InterfaceError: Use multi=True when executing multiple statements

codigo_posal的数据类型是int(11),而latitud和longitud是varchar。在

我尝试过创建新的连接,新的光标,新的连接和光标。我尝试添加multi=“True”并将语句组合到一个操作中。我试着给每一个都添加multi=“True”当前执行()同时作为第二个和第三个参数。我已经在Workbench中运行了该语句,以确保该语句是有效的并且能够正常工作。在

但在这里没有成功。。。在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值