Can't get MySQL source query to work using Python mysqldb module

前端 未结 5 1951
情深已故
情深已故 2021-01-02 20:20

I have the following lines of code:

sql = \"source C:\\\\My Dropbox\\\\workspace\\\\projects\\\\hosted_inv\\\\create_site_db.sql\"
cursor.execute (sql)
         


        
5条回答
  •  无人及你
    2021-01-02 20:56

    The source command is one of the built-in commands recognized only by the mysql command-line client. It is not supported as a statement you can execute via any API.

    Some people think you can simply split an SQL script file on the ";" statement terminator and call execute() on each line you get. But there are numerous exception cases:

    • Statements that are built-in commands like CONNECT, SOURCE, CHARSET, WARNINGS, QUIT, etc.
    • Note that built-in commands don't need to terminate in ; for example DELIMITER.
    • Statements that contain ; but not as a terminator, like CREATE TRIGGER.
    • Statements that contain ; inside string literals or comments or even quoted identifiers.
    • Comments lines.

    To load an SQL script programmatically, you'd have to duplicate a fair amount of the functionality of the mysql client. So it's best if you just fork a process to actually execute that client program with the script as input.

    See also:

    • Loading .sql files from within PHP
    • is it possible to call a sql script from a stored procedure in another sql script?
    • composing multiple mysql scripts
    • Running Database scripts in C#

提交回复
热议问题