使用 python mysql.connector 转义字符串 [英] Escaping strings with python mysql.connector

查看:83
本文介绍了使用 python mysql.connector 转义字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 python 和 mysql.connector 将一堆字符串插入 mysql.我当前的代码如下所示:

I am trying to insert a bunch of strings into mysql using python and mysql.connector. My current code looks something like this:

db = mysql.connector.Connect('config blah blah')
cursor = db.cursor()
data = (somestring1, somestring2)
sql = "INSERT INTO mytable (col1, col2) VALUES ('%s', '%s')"
cursor.execute(sql, data)

我应该如何转义我的字符串?我可以尝试用 python 来做,但我知道这不是正确的方式.

How should I go about escaping my strings? I could try doing it in python but I know this isn't the right way.

注意:我意识到 mysql.connector 仍在开发中.

Note: I realise mysql.connector is still in development.

更新:

第 4 行应为:

sql = "INSERT INTO mytable (col1, col2) VALUES (%s, %s)"

推荐答案

由于 mysql.connector 是 DB API v2.0 兼容,您不需要自己转义数据,它会自动为您转义.

Since mysql.connector is DB API v2.0 compliant, you do not need to escape the data yourself, it does it automatically for you.

这篇关于使用 python mysql.connector 转义字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆