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

前端 未结 5 1952
情深已故
情深已故 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:57

    As others said, you cannot use the command source in MySQLdb Python API

    So, instead of running that, load the file and execute it

    Lets say your .sql file has

    create database test;
    

    Read the content like

    sql=open("test.sql").read()
    

    And then execute it

    cursor.execute(sql);
    

    You will get new database "test"

提交回复
热议问题