1

i have this basic sql file:

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;

and want to execute it via 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)

When i execute this via "python3 app.py", it connects, but i just get:

psycopg2.ProgrammingError: syntax error at or near "`"
LINE 1: CREATE TABLE `app_users` (

But i have no idea why.. anybody could help me with this issue?

thanks and greetings

6
  • I suggest to look into the type of quote you are using. This ` is different to this ' and each programming language will have different ways to interpret them, parsing and escaping. Commented Aug 21, 2017 at 21:44
  • i usually use \ to escape the quoted character to executing sql in python. Something like this CREATE TABLE \'app_users\' ( ... Commented Aug 21, 2017 at 21:52
  • i replaced ` with ' and \' . Both result in the examt same error.. Commented Aug 21, 2017 at 21:54
  • i removed them all, results in: psycopg2.ProgrammingError: syntax error at or near "(" ... any help? Commented Aug 21, 2017 at 22:11
  • 3
    Forget about my previous comment. It seems that your question should be "How to convert this MySQL create table script to the PostgreSQL" Commented Aug 21, 2017 at 22:20

1 Answer 1

1

Use the proper Postgresql syntax:

CREATE TABLE app_users (
    id serial,
    email varchar(200),
    password varchar(200),
    primary key (id)
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.