我在python和mySQL中创建了一个程序,它创建了一个数据库并从文本文件中导入数据并将它们放入4个不同的列中.代码可以工作,但我想更改代码并创建函数.
任何人都可以帮我创建一个创建mySQL数据库的功能吗?这是我目前的代码.提前致谢!
import MySQLdb
# Create connection to the MySQL database - Make sure host, user,
# passwd are consistent with the database you are trying to conect to
def create_database():
db_connection = MySQLdb.connect(host='localhost', user='root', passwd='password')
# Variable that exacutes Database calls with MySQL
cursor = db_connection.cursor()
# Create databse with MYSQL query - databasename
cursor.execute('CREATE DATABASE inb104')
# Select which database to use with MYSQL query - databasename
cursor.execute('USE inb104')
# Create database with MYSQL query - …Run Code Online (Sandbox Code Playgroud)