1
我有这个基本的SQL文件:python3 - psycopg2 - 执行sql文件
CREATE TABLE `app_users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(200) DEFAULT NULL,
`password` varchar(200) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
,并希望通过Python执行它:
connect_str = "dbname='dbname' user='user' host='192.168.1.101' password='password'"
conn = psycopg2.connect(connect_str)
cursor = conn.cursor()
fd = open('file.sql', 'r')
sqlFile = fd.read()
fd.close()
sqlCommands = sqlFile.split(';')
for command in sqlCommands:
print(command)
if command.strip() != '':
cursor.execute(command)
当我通过 “python3 app.py” 执行此它连接,但我只是得到:
psycopg2.ProgrammingError: syntax error at or near "`"
LINE 1: CREATE TABLE `app_users` (
但我不知道为什么..有人可以帮我解决这个问题?
感谢和问候
我建议寻找到您所使用的引用类型。这与'不同',每种编程语言都有不同的方式来解释它们,解析和转义。 –
我通常使用\来将引用的字符转义为在python中执行sql。像这样的东西CREATE TABLE \'app_users \'(... – Alam
我用'替换'和\'。都导致检查相同的错误.. –