Python and MySQL
Jul 5, 2021
A easy example to use MySQL and Python.
import pymysql.cursors
db_host = ‘server’
db_user = ‘user…’
db_pass = ‘pass…’
db_name = ‘mydb’
conn = pymysql.connect (host=db_host, user=db_user, passwd=db_pass)
cursor = conn.cursor ()
cursor.execute (“DROP DATABASE IF EXISTS mydb”)
cursor.execute (“CREATE DATABASE mydb”)
conn = pymysql.connect (host=db_host, user=db_user, passwd=db_pass, db=db_name)
cursor = conn.cursor ()
cursor.execute (“CREATE TABLE hello (id INT AUTO_INCREMENT PRIMARY KEY, yo float, tu float, el float)”)
sql = “INSERT INTO hello (yo, tu, el) VALUES (%s, %s, %s)”
val = [
(
‘1’, ‘1’, ‘1’)
]
cursor.executemany (sql, val)
conn.commit ()
print (“Created Structure”)