返回列表 上一主題 發帖

確認MySQL Using Connector/Python連線成功

確認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()
複製代碼
很抱歉,我是小學生,不能下載檔案,是個小屁孩!

        靜思自在 : 修行要繫緣修心,藉事練心,隨處養心。
返回列表 上一主題