Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MySQL needs REPLACE and not INSERT OR REPLACE (though it does use INSERT IGNORE) #44

Open
HappyChews opened this issue May 10, 2021 · 1 comment
Labels

Comments

@HappyChews
Copy link

When using csvsql to insert csv files to MySQL database, the REPLACE prefix generates incorrect SQL and fails with error.

csvsql --version
csvsql 1.0.5
python3 --version
Python 3.7.3
python --version
Python 2.7.16

Tested on Debian WSL.

Test file attached but can be anything.
test.txt

Commands tried:
csvsql --db mysql+mysqlconnector://$dbUser:$dbPass@$dbString:$dbPort/$dbSchema --tables test --unique-constraint Id --create-if-not-exist --prefix REPLACE --insert test.csv

csvsql --db mysql+mysqlconnector://$dbUser:$dbPass@$dbString:$dbPort/$dbSchema --tables test --unique-constraint Id --create-if-not-exist --prefix IGNORE --insert test.csv

When using IGNORE prefix the command finishes without any error, but with REPLACE it throws an ProgrammingError:

ProgrammingError: (mysql.connector.errors.ProgrammingError) 1064 (42000): 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 'REPLACE INTO test (Name, Id, Width) VALUES ('Charles', '1', '1')' at line 1
[SQL: INSERT REPLACE INTO test (Name, Id, Width) VALUES (%(Name)s, %(Id)s, %(Width)s)]

As you can see on the bottom line, I have tried to highlight the error. It should have been only "REPLACE INTO"
MySQL Documentation states that the correct syntax for REPLACE INTO does not have INSERT in front.
https://dev.mysql.com/doc/refman/8.0/en/replace.html

As the syntax for IGNORE actually is "INSERT IGNORE INTO", this prefix does not throw an error.

@jpmckinney jpmckinney added the bug label Jun 11, 2021
@jpmckinney jpmckinney transferred this issue from wireservice/csvkit Oct 17, 2023
@jpmckinney jpmckinney changed the title csvsql mysql REPLACE INTO syntax error MySQL uses REPLACE and not INSERT OR REPLACE (though it does use INSERT IGNORE) Oct 17, 2023
@jpmckinney
Copy link
Member

I looked into this and it's not terribly obvious how to do it. We use Table from SQLAlchemy, which has an insert method but not a replace method.

The only solution I could find is to switch to using Sessions, but that's quite different from our approach. https://stackoverflow.com/questions/708762/sqlalchemy-insert-or-replace-equivalent

@jpmckinney jpmckinney changed the title MySQL uses REPLACE and not INSERT OR REPLACE (though it does use INSERT IGNORE) MySQL needs REPLACE and not INSERT OR REPLACE (though it does use INSERT IGNORE) Oct 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants