sql - python3 - psycopg2 - 执行sql文件

标签 sql postgresql python-3.x psycopg2 execute

我有这个基本的 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` (

但我不知道为什么..有人可以帮我解决这个问题吗?

谢谢和问候

最佳答案

使用正确的 Postgresql 语法:

CREATE TABLE app_users (
    id serial,
    email varchar(200),
    password varchar(200),
    primary key (id)
)

关于sql - python3 - psycopg2 - 执行sql文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45805871/

相关文章:

sql - 按可互换使用的值对分组

php - MYSQL 优化 JOIN 查询中 ON 条件返回多行的情况

mysql - AWS Cognito Identity ID 对 SQL 主键的适用性

java - 批量插入 Postgresql 或 MySQL 时忽略重复约束

java - 如何在Java中将PostgreSQl View 与hibernate连接?

python-3.x - 返回多维数组中N个最大值的索引(可以求一维的解,但不能求多维的)

sql - 验证 SQL Server 中表中的字符串 - CLR 函数或 T-SQL(已更新问题)

sql - 拒绝两个字段中的空值

python - 当没有重复项时,pandas 中索引重复错误

html - 如何调整 Bokeh DataTable 中行的高度和格式