MySQLdb 在已转义的字符串前面添加字符 b - Python
MySQLdb adding character b infront of strings that have been escaped - Python
我正在尝试编写一个简单的 Python 脚本,使用 MySQLdb (mysqlclient) 程序包将电影标题批量添加到本地数据库中。我正在阅读 TSV 文件中的标题。但是当使用 MySQLdb::escape_string()
对输入进行清理时,我在字符串之前得到了字符 b
。我相信这意味着 SQL 将其解释为位值,但是当我执行查询时出现以下错误:
You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use
near 'b'Bowery to Bagdad',1955)' at line 1"
有问题的 select 语句:
INSERT INTO movies (imdb_id, title, release_year) VALUES ('tt0044388',b'Bowery to Bagdad',1955)
def TSV_to_SQL(file_to_open):
from MySQLdb import _mysql
db=_mysql.connect(host='localhost', user='root', passwd='', db='tutorialdb', charset='utf8')
q = """SELECT * FROM user_id"""
# MySQLdb.escape_string()
# db.query(q)
# results = db.use_result()
# print(results.fetch_row(maxrows=0, how=1))
print("starting?")
with open(file_to_open, encoding="utf8") as file:
tsv = csv.reader(file, delimiter="\t")
count = 0
for line in tsv:
if count == 10:
break
# print(MySQLdb.escape_string(line[1]))
statement = "INSERT INTO movies (imdb_id, title, release_year) VALUES ('{imdb_id}',{title},{year})\n".format(
imdb_id=line[0], title=MySQLdb.escape_string(line[1]), year=line[2])
# db.query(statement)
print(statement)
count = count + 1
我知道一个简单的解决方案是从字符串的开头删除字符 b
,但我想知道是否有更合适的方法,或者我是否遗漏了文档中的内容。
字符串前面的'b'表示该字符串是二进制编码的,而不是文字字符串。
如果你使用 .encode() 你将能够得到你想要的。
How to convert 'binary string' to normal string in Python3?
更常见的做法是让连接器自动执行转义,方法是在 SQL 语句中插入占位符并将值序列(通常是 tuple
)作为第二个参数传递给 cursor.execute
.
conn = MySQLdb.connect(host='localhost', user='root', passwd='', db='tutorialdb', charset='utf8')
cursor = conn.cursor()
statement = """INSERT INTO movies (imdb_id, title, release_year) VALUES (%s, %s, %s)"""
cursor.execute(statement, (line[0], line[1], line[2]))
conn.commit()
生成的代码更具可移植性——除了连接之外,它将与所有 DB-API 连接器*一起工作。 _mysql.connect
和 escape_string
之类的低级函数在 Python 代码中是不常见的(当然,如果你愿意,你完全可以自由地编写这样的代码)。
* 一些连接包可能使用不同的占位符而不是 %s
,但 %s
似乎是 MySQL 连接包的首选占位符。
我正在尝试编写一个简单的 Python 脚本,使用 MySQLdb (mysqlclient) 程序包将电影标题批量添加到本地数据库中。我正在阅读 TSV 文件中的标题。但是当使用 MySQLdb::escape_string()
对输入进行清理时,我在字符串之前得到了字符 b
。我相信这意味着 SQL 将其解释为位值,但是当我执行查询时出现以下错误:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'b'Bowery to Bagdad',1955)' at line 1"
有问题的 select 语句:
INSERT INTO movies (imdb_id, title, release_year) VALUES ('tt0044388',b'Bowery to Bagdad',1955)
def TSV_to_SQL(file_to_open):
from MySQLdb import _mysql
db=_mysql.connect(host='localhost', user='root', passwd='', db='tutorialdb', charset='utf8')
q = """SELECT * FROM user_id"""
# MySQLdb.escape_string()
# db.query(q)
# results = db.use_result()
# print(results.fetch_row(maxrows=0, how=1))
print("starting?")
with open(file_to_open, encoding="utf8") as file:
tsv = csv.reader(file, delimiter="\t")
count = 0
for line in tsv:
if count == 10:
break
# print(MySQLdb.escape_string(line[1]))
statement = "INSERT INTO movies (imdb_id, title, release_year) VALUES ('{imdb_id}',{title},{year})\n".format(
imdb_id=line[0], title=MySQLdb.escape_string(line[1]), year=line[2])
# db.query(statement)
print(statement)
count = count + 1
我知道一个简单的解决方案是从字符串的开头删除字符 b
,但我想知道是否有更合适的方法,或者我是否遗漏了文档中的内容。
字符串前面的'b'表示该字符串是二进制编码的,而不是文字字符串。 如果你使用 .encode() 你将能够得到你想要的。 How to convert 'binary string' to normal string in Python3?
更常见的做法是让连接器自动执行转义,方法是在 SQL 语句中插入占位符并将值序列(通常是 tuple
)作为第二个参数传递给 cursor.execute
.
conn = MySQLdb.connect(host='localhost', user='root', passwd='', db='tutorialdb', charset='utf8')
cursor = conn.cursor()
statement = """INSERT INTO movies (imdb_id, title, release_year) VALUES (%s, %s, %s)"""
cursor.execute(statement, (line[0], line[1], line[2]))
conn.commit()
生成的代码更具可移植性——除了连接之外,它将与所有 DB-API 连接器*一起工作。 _mysql.connect
和 escape_string
之类的低级函数在 Python 代码中是不常见的(当然,如果你愿意,你完全可以自由地编写这样的代码)。
* 一些连接包可能使用不同的占位符而不是 %s
,但 %s
似乎是 MySQL 连接包的首选占位符。