Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- server = SSHTunnelForwarder(
- ('172.17.9.125', 22),
- ssh_password="123456",
- ssh_username="root",
- remote_bind_address=('127.0.0.1', 3306))
- server.start()
- database = pymysql.connect(host='127.0.0.1',
- port=3306,
- user='root',
- passwd='root')
- try:
- dbsql = "CREATE DATABASE TestDB" # Create database
- except Exception as e:
- print("Error: ", e)
- database.cursor().execute(dbsql)
- global db, cursor
- db = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='root', db='TestDB')
- cursor = db.cursor()
- print("Connected to MySQL database")
- f = open("testdb.sql") # Execute .sql file, creating data tables
- full_sql = f.read()
- sql_commands = full_sql.split(';')[:-1]
- try:
- for sql_command in sql_commands:
- if sql_command is not None:
- cursor.execute(sql_command)
- else:
- print("Null")
- print("Created database")
- except Exception as e:
- print("Error: ", e)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement