【发布时间】:2020-12-24 21:12:51
【问题描述】:
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(我无法修改)并能够通过添加一些参数来使用两个 db 驱动程序为 text 列返回相同的内容连接。我都找不到如何控制这种行为,所以卡住了。
【问题讨论】:
-
您现在运行查询时遇到什么样的错误?
-
没有错误,我只需要两个驱动程序的行为一致,这样我就可以有一个代码路径来处理结果。
标签: python mysql postgresql psycopg2 mysql-python