用python创建MySQL数据库 [英] Create MySQL database with python
本文介绍了用python创建MySQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用以下参数执行文件:./file.py服务器地址arg2 arg3 arg4
I want to execute a file with parameters like this : ./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()
推荐答案
Use the built-in module argparse
. Or optparse
if you're stuck on Python 2.6 or earlier.
这篇关于用python创建MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文