Board logo

標題: 確認MySQL Using Connector/Python連線成功 [打印本頁]

作者: heavenweaver    時間: 2014-3-6 01:17     標題: 確認MySQL Using Connector/Python連線成功

有關 Connecting to MySQL 5.6 Using Connector/Python可以參考MySQL Connector/Python Developer Guide,在Chapter 5 Connector/Python Coding Examples有:
5.1 Connecting to MySQL Using Connector/Python
5.2 Creating Tables Using Connector/Python
5.3 Inserting Data Using Connector/Python
5.4 Querying Data Using Connector/Python
就5.1 Connecting to MySQL Using Connector/Python部份,說明文件雖有提供範例,但我還是依照自己的需要作些修改,或可供後續的人參考,連線測試範例程式如下:
  1. import mysql.connector
  2. from mysql.connector import errorcode

  3. try:
  4.   cnx = mysql.connector.connect(user='root', password='http://forum.twbts.com',
  5.                               host='127.0.0.1',
  6.                               database='world')
  7. except mysql.connector.Error as err:
  8.   if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
  9.     print("Something is wrong with your user name or password")
  10.   elif err.errno == errorcode.ER_BAD_DB_ERROR:
  11.     print("Database does not exists")
  12.   else:
  13.     print(err)
  14.     exit(1)
  15. else:   
  16.   print("Congratulation! Your Connector/Python Installation is Verified.")  
  17.   cnx.close()
複製代碼





歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)