【发布时间】:2014-07-11 19:36:40
【问题描述】:
我一直在尝试使用 pythons mysql.connector 将一个大字符串插入 MySQL 数据库。我遇到的问题是,在使用准备好的语句时,长字符串在某些时候会被截断。我目前正在使用 MySQL.com 上提供的 MySQL Connector/Python。我使用以下代码确实重复了我遇到的问题。
db = mysql.connector.connect(**creditials)
cursor = db.cursor()
value = []
for x in range(0, 2000):
value.append(str(x+1))
value = " ".join(value)
cursor.execute("""
CREATE TABLE IF NOT EXISTS test (
pid VARCHAR(50),
name VARCHAR(120),
data LONGTEXT,
PRIMARY KEY(pid)
)
""")
db.commit()
#this works as expected
print("Test 1")
cursor.execute("REPLACE INTO test (pid, name, data) VALUES ('try 1', 'Description', '{0}')".format(value))
db.commit()
cursor.close()
#this does not work
print("Test 2")
cursor = db.cursor(prepared=True)
cursor.execute("""REPLACE INTO test (pid, name, data) VALUE (?, ?, ?)""", ('try 2', 'Description2', value))
db.commit()
cursor.close()
测试 1 按预期工作,并存储了直到 2000 的所有数字,但测试 2 在数字 65 之后立即被切断。我宁愿使用准备好的语句而不是尝试自己清理传入的字符串。任何帮助表示赞赏。
额外信息:
计算机:Windows 7 64 位
Python:在 python 3.4 和 3.3 上都试过
MYSQL:5.6.17(WAMP 自带)
库:MySQL 连接器/Python
【问题讨论】:
-
如果是mysql请移除sql标签
-
你使用的是哪个 Python MySQL 库?
-
我正在使用 mysql.com 上提供的 MySQL 连接器/Python
标签: python mysql prepared-statement