Open HappyChews opened 3 years ago
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
When using csvsql to insert csv files to MySQL database, the REPLACE prefix generates incorrect SQL and fails with error.
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:
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.