petl-developers / petl

Python Extract Transform and Load Tables of Data
MIT License
1.22k stars 190 forks source link

petl.todb not work DELETE SQL syntax error #631

Closed yqz945 closed 1 year ago

yqz945 commented 1 year ago

Minimal, reproducible code sample, a copy-pastable example if possible

## with sqlalchemy session
       tt = [['foo', 'bar'],
              ['a', 1],
              ['b', 2],
              ['c', 2]]
        petl.todb(
            tt,
            Session(create_engine(config.SQLALCHEMY_DATABASE_URI)),
            tablename='tyedu_fct_ac_homework',
            create=True
        )

## pymysql.connect
      tt = [['foo', 'bar'],
              ['a', 1],
              ['b', 2],
              ['c', 2]]
        petl.todb(
            tt,
            pymysql.connect(**self.params['destination']['conn_params']),
            tablename='tyedu_fct_ac_homework',
            create=True
        )

Problem description

The two got the same error: sqlalchemy.exc.ProgrammingError: (pymysql.err.ProgrammingError) (1064, 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'"tyedu_fct_ac_homework"\' at line 1') [SQL: DELETE FROM "tyedu_fct_ac_homework"]

First I use clickhouse, got the error, then, I change to mysql, the error is still.

Version and installation information

bluechanel commented 1 year ago

N.B., for MySQL the statement SET SQL_MODE=ANSI_QUOTES is required to ensure MySQL uses SQL-92 standard quote characters.

>>> import pymysql
>>> connection = pymysql.connect(password='moonpie', database='thangs')
>>> # tell MySQL to use standard quote character
... connection.cursor().execute('SET SQL_MODE=ANSI_QUOTES')
>>> # load data, assuming table "foobar" already exists in the database
... etl.todb(table, connection, 'foobar')
yqz945 commented 1 year ago

@bluechanel Thank you very much. It's right. I didn't notice the SET statement.