สำหรับใน Python ผมขอแนะนำ PyMySQL http://www.pymysql.org/ ครับ และมีคนมา fork ต่อกลายเป็น CyMySQL ที่จริงทาง MySQL ได้ออกมอดูลสำหรับ Python มาครับ แต่การใช้งานที่เรียกว่า ยุ่งยากเกินไป นี่ครับ mysql-connector-python
การเชื่อมต่อกับฐานข้อมูล MySQL โดยใช้ PyMySQL
หลังจากที่ผู้อ่านได้ทำการติดตั้ง PyMySQL แล้ว https://github.com/PyMySQL/PyMySQL/archive/master.zip วิธีการติดตั้งมอดูล Modules ใน Python 3 แล้วนี่คือโค้ดตัวอย่างการใช้งาน PyMySQLimport pymysql
conn = pymysql.connect(host='127.0.0.1', unix_socket='/tmp/mysql.sock', user='root', passwd=None, db='mysql')
# conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd=None, db='mysql')
cur = conn.cursor()
cur.execute("SELECT Host,User FROM user")
# print cur.description
# r = cur.fetchall()
# print r
# ...or...
for r in cur:
print r
cur.close()
conn.close()
จากตัวอย่าง จะเห็นได้ว่าใช้งานแค่ใช้ภาษา SQL ในการจัดการฐานข้อมูลครับ ตัวแปรที่เชื่อมต่อฐานข้อมูล.execute("คำสั่ง SQL")
ลองนำไปประยุกต์ใช้กันครับ
ขอบคุณครับ

0 ความคิดเห็น:
แสดงความคิดเห็น
แสดงความคิดเห็นได้ครับ :)