Yesterday with Shell script to create a database, involving Java calls, compare toss, instead of using Python to create a database directly, it is more convenient, good, directly on the code, the relevant comments also added
# _*_encoding:utf-8_*_
Import MySQLdb
Db_host = ' '
Db_user = ' '
DB_PW = ' '
db_name = ' VDT '
def cre_db (host, user, PW, name):
Try
# Database connection
db = MySQLdb.connect (host, user, PW, charset= ' UTF8 ')
# Create cursors through connection and data communication
cursor = Db.cursor ()
# Execute SQL statement
Cursor.execute (' Show databases ')
rows = Cursor.fetchall ()
For row in rows:
TMP = "%2s"% row
# Determine if the database exists
If name = = tmp:
Cursor.execute (' drop database if exists ' + name)
Cursor.execute (' Create database if not exists ' + name)
# Commit to Database execution
Db.commit ()
Except Mysqldb.error, E:
Print "Mysql Error%d:%s"% (E.args[0], e.args[1])
Finally
# Close the database connection
Db.close ()
cre_db (Db_host, Db_user, DB_PW)
Python CREATE Database