python鏈接mysql數據庫

安裝pythonpython

apt-get install pythonmysql

安裝mysqlLdb:sql

apt-cache search MySQLdb
2022 apt-get install python-mysqldbfetch

實例代碼:utf-8

#!/usr/bin/env python
#coding=utf-8
#!/usr/bin/pythonget

# -*- coding: utf-8 -*-
import MySQLdb as mdb
import sys
con = None
try:
  con = mdb.connect('localhost', 'root', 'firmy', '56demo');
  cur = con.cursor()
  cur.execute("SELECT VERSION()")
  data = cur.fetchone()
  print "Database version : %s " % data
except mdb.Error, e:
  print "Error %d: %s" % (e.args[0],e.args[1])
  sys.exit(1)
finally:
  if con:
  con.close()it

相關文章
相關標籤/搜索