使用python创建MySQL数据库

时间:2014-07-07 12:26:53

标签: python mysql

我想用这样的参数执行一个文件:./ file.py server adress arg2 arg3 arg4

file1.py

import MySQLdb

# Open database connection
db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )

# prepare a cursor object using cursor() method
cursor = db.cursor()

# execute SQL query using execute() method.
cursor.execute("SELECT VERSION()")

# Fetch a single row using fetchone() method.
data = cursor.fetchone()

print "Database version : %s " % data

# disconnect from server
db.close()

1 个答案:

答案 0 :(得分:1)

使用内置模块argparse。或optparse如果您遇到Python 2.6或更早版本。