python整理二十——连接MySQ和MSSQL

来源:互联网 发布:cad手机画图软件 编辑:程序博客网 时间:2024/05/29 09:36

关于连接数据库,mysql和mssql的库不一样,连接参数名也不一样,需要注意!

 

  1.     host = ''
  2.     user = ''
  3.     pwd = ''
  4.     dbname = ''
  5.     conn_my, conn_ms = NoneNone
  6.     import MySQLdb
  7.     conn_my = MySQLdb.connect(host=host,
  8.         user=user,passwd=pwd, db=dbname, connect_timeout=5)
  9.     import pymssql
  10.     conn_ms = pymssql.connect(host=host,
  11.         user=user,password=pwd, database=dbname)
  12.     if conn_my:
  13.         print 'conn mysql:', conn_my.ping(),
  14.         cursor = conn_my.cursor()
  15.         cursor.execute('select LENGTH("sql")')
  16.         data = cursor.fetchall()
  17.         print data
  18.     if conn_ms:
  19.         print 'conn mssql:', conn_ms.ping(),
  20.         cursor = conn_ms.cursor()
  21.         cursor.execute('select LENGTH("sql")')
  22.         data = cursor.fetchall()
  23.         print data
原创粉丝点击