Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. server = SSHTunnelForwarder(
  2. ('172.17.9.125', 22),
  3. ssh_password="123456",
  4. ssh_username="root",
  5. remote_bind_address=('127.0.0.1', 3306))
  6.  
  7. server.start()
  8.  
  9. database = pymysql.connect(host='127.0.0.1',
  10. port=3306,
  11. user='root',
  12. passwd='root')
  13.  
  14. try:
  15. dbsql = "CREATE DATABASE TestDB" # Create database
  16. except Exception as e:
  17. print("Error: ", e)
  18. database.cursor().execute(dbsql)
  19.  
  20. global db, cursor
  21. db = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='root', db='TestDB')
  22.  
  23. cursor = db.cursor()
  24. print("Connected to MySQL database")
  25.  
  26. f = open("testdb.sql") # Execute .sql file, creating data tables
  27. full_sql = f.read()
  28. sql_commands = full_sql.split(';')[:-1]
  29. try:
  30. for sql_command in sql_commands:
  31. if sql_command is not None:
  32. cursor.execute(sql_command)
  33. else:
  34. print("Null")
  35. print("Created database")
  36. except Exception as e:
  37. print("Error: ", e)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement