天天看點

python如何建立mysql資料庫,用python建立MySQL資料庫

python如何建立mysql資料庫,用python建立MySQL資料庫

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.