psycopg2 vs MySQLdb 反斜杠转义行为
psycopg2 vs MySQLdb backslash escaping behaviour
import MySQLdb
import psycopg2
# CREATE DATABASE mytest CHARACTER SET utf8;
db=MySQLdb.connect(host="localhost",user="anton",
passwd="password",db="mytest")
cur = db.cursor()
cur.execute("drop table if exists mytable")
cur.execute("create table mytable (textcol text)")
cur.execute("insert into mytable values (' some \\ escaped stuff')")
cur.execute("select * from mytable")
print(cur.fetchall())
# CREATE DATABASE mytest ENCODING 'UTF8';
db=psycopg2.connect(host="localhost",user="anton",
password="password",dbname="mytest")
cur = db.cursor()
cur.execute("drop table if exists mytable")
cur.execute("create table mytable (textcol text)")
cur.execute("insert into mytable values (' some \\ escaped stuff')")
cur.execute("select * from mytable")
print(cur.fetchall())
输出:
((' some \ escaped stuff',),)
[(' some \\ escaped stuff',)]
基本上,我想要的只是能够使用相同的插入 sql(我无法修改)并且能够为 text
列使用两个数据库驱动程序都通过向连接添加一些参数来实现。我也找不到如何控制这种行为,所以卡住了。
其实两者都有可能。
db=MySQLdb.connect(host="localhost",user="anton",
passwd="password",db="mytest", sql_mode="NO_BACKSLASH_ESCAPES")
会告诉mysql
to treat backslashes like the standard, and so like postgresql
has been by default since 9.1。像这样设置 sql_mode
可能不是你想要的,所以像 sql_mode="TRADITIONAL,NO_BACKSLASH_ESCAPES"
这样的东西(注意它们之间没有 space,你会得到 space 的错误)会给你一个严格的 sql_mode
和 SQL 标准的转义行为。
也可以采用其他方式 - 您可以让 postgresql
在默认配置中以类似于 mysql
的方式运行(在 Ubuntu 20.04 上):
db=psycopg2.connect(host="localhost",user="anton",
password="password",dbname="mytest", options='-c standard_conforming_strings=off')
本质上 puts postgresql
back in pre-9.1 mode 关于反斜杠转义。
import MySQLdb
import psycopg2
# CREATE DATABASE mytest CHARACTER SET utf8;
db=MySQLdb.connect(host="localhost",user="anton",
passwd="password",db="mytest")
cur = db.cursor()
cur.execute("drop table if exists mytable")
cur.execute("create table mytable (textcol text)")
cur.execute("insert into mytable values (' some \\ escaped stuff')")
cur.execute("select * from mytable")
print(cur.fetchall())
# CREATE DATABASE mytest ENCODING 'UTF8';
db=psycopg2.connect(host="localhost",user="anton",
password="password",dbname="mytest")
cur = db.cursor()
cur.execute("drop table if exists mytable")
cur.execute("create table mytable (textcol text)")
cur.execute("insert into mytable values (' some \\ escaped stuff')")
cur.execute("select * from mytable")
print(cur.fetchall())
输出:
((' some \ escaped stuff',),)
[(' some \\ escaped stuff',)]
基本上,我想要的只是能够使用相同的插入 sql(我无法修改)并且能够为 text
列使用两个数据库驱动程序都通过向连接添加一些参数来实现。我也找不到如何控制这种行为,所以卡住了。
其实两者都有可能。
db=MySQLdb.connect(host="localhost",user="anton",
passwd="password",db="mytest", sql_mode="NO_BACKSLASH_ESCAPES")
会告诉mysql
to treat backslashes like the standard, and so like postgresql
has been by default since 9.1。像这样设置 sql_mode
可能不是你想要的,所以像 sql_mode="TRADITIONAL,NO_BACKSLASH_ESCAPES"
这样的东西(注意它们之间没有 space,你会得到 space 的错误)会给你一个严格的 sql_mode
和 SQL 标准的转义行为。
也可以采用其他方式 - 您可以让 postgresql
在默认配置中以类似于 mysql
的方式运行(在 Ubuntu 20.04 上):
db=psycopg2.connect(host="localhost",user="anton",
password="password",dbname="mytest", options='-c standard_conforming_strings=off')
本质上 puts postgresql
back in pre-9.1 mode 关于反斜杠转义。