Over a year ago, someone asked this question: Run the .sql files that are used to run in SQL Management Studio in python .
I am writing a script in python that connects to a SQL server and creates and populates a database based on SQL commands in a large (several GB) .sql file.
It seems like SQLCMD requires downloading and installing SQL Server Express. Are there other ways to execute the .sql file from python without requiring everyone using my script to download and install SQL Server? Does pyodbc have this feature?
EDIT:
Here is another similar question: execute * .sql file with python MySQLdb
Here again, the solution is to invoke the utility from the command (in this case mysql.exe) with the file specified as an argument.
It seems to me that there should be a way to do this using one of the Python DB API API libraries, but I did not find it, so I am looking for * .exe, for example SQLCMD or MYSQL, which I can use to run the file from the command line.
PS Please feel free to correct me if I do not look at it correctly. Perhaps the code below is as effective as running from the command line:
for line in open('query.sql','r'): cursor.execute(line)
Neal kruis
source share