- 安裝 easy_install > sudo wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py -O - | python
- 安裝 Mysql Connector/Python > sudo easy_install mysql-connector-python
範例程式
import mysql.connector
from mysql.connector import errorcode
try:
cnx = mysql.connector.connect(user='userid', password='******',
host='127.0.0.1',
database='dbname', charset='utf8')
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exists")
else:
print(err)
else:
cursor = cnx.cursor()
query = ("SELECT * FROM tablename ORDER BY id DESC LIMIT 10;")
cursor.execute(query)
for i in cursor:
print i
cursor.close()
cnx.close()
延伸閱讀:MySQL Connector/Python 官網
#