I'm creating a python script to delete, create and import a database using python
sql file location : /home/myname/Downloads/db/my_db_file.sql
python file location : /var/www/dev-django/my_python_file.py
my file is as follows:
from MySQLdb import _mysql
host = '127.0.0.1'
user = 'test1'
passwd = '123123'
file_location = '/home/myname/Downloads/db/my_db_file.sql'
db_name = 'my_db'
db=_mysql.connect(host,user,passwd)
db.query("""DROP DATABASE """ + db_name + """;""")
print('db deleted')
db.query("""CREATE DATABASE """ + db_name + """;""")
print('db created again')
db.query("""use """ + db_name + """;""")
print('using the db')
db.query("""SET autocommit=0 ; """)
db.query(""" source """ + file_location + """ ; """)
db.query(""" COMMIT ; """)
print('done')
on running the file, i get the following error:
(venv) [root@XXXX some_folder]# python create_db_for_testing.py
db deleted
db created again
using the db
Traceback (most recent call last):
File "create_db_for_testing.py", line 18, in <module>
db.query(""" source """ + file_location + """ ; """)
_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'source /home/myname/Downloads/db/my_db_file.sql ; COMMIT' at line 1")