星期二, 3月 22, 2016

Windows Azure的mysql與python

因為Windows Azure註冊出了問題,目前我還沒測試。
網路上查到的
Windows Azure的mysql連結參數:

db = MySQLdb.connect(host= "windows azure url goes here",user="uname",passwd="pwd",db="dbname")

不過 Pythone所提供的mysql連結參數:
db = MySQLdb.connect(host= "host","uname","pwd","dbname")


這語法得再查證,不過除了連結資料庫不同外,Windows Azure的mysql還得做以下兩件事情
1.防火牆上開啟3306
2.針對你的資料庫設定一個連線帳號,例如
 GRANT ALL ON SOMEDATABASE.* TO USERNAME@% IDENTIFIED BY 'SOMEPASSWORD'

python新增與查詢的參考語法
import MySQLdb

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

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

# Select qSQL with id=4.
cursor.execute("SELECT qSQL FROM TBLTEST WHERE id = 4")

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

qSQL = results[0]

cursor.execute(qSQL)

# Fetch all the rows in a list of lists.
qSQLresults = cursor.fetchall()
for row in qSQLresults:
    id = row[0]
    city = row[1]

    #SQL query to INSERT a record into the table FACTRESTTBL.
    cursor.execute('''INSERT into FACTRESTTBL (id, city)
                  values (%s, %s)''',
                  (id, city))

    # Commit your changes in the database
    db.commit()

# disconnect from server
db.close()


MySQL與python可參考:
http://www.tutorialspoint.com/python/python_database_access.htm

1 則留言:

  1. 老師,我是上你C語言的學生
    我下載python後想在win10介面操作,網路上說要安裝distribute,但卻發生下面的問題,請問如何解決
    register_loader_type(importlib_bootstrap.SourceFileLoader, DefaultProvider)
    AttributeError: module 'importlib._bootstrap' has no attribute 'SourceFileLoader'

    回覆刪除