标签归档:dbhandler

RSS feed of dbhandler

基于MySQLdb的SAE_PYTHON环境MYSQL小工具


Python代码 


  1. #coding=utf-8

  2. import sae.const

  3. import MySQLdb

  4.  

  5. #获取数据库连接

  6. def get_conn():

  7.   conn=MySQLdb.connect(host=sae.const.MYSQL_HOST,user=sae.const.MYSQL_USER,passwd=sae.const.MYSQL_PASS,db=sae.const.MYSQL_DB,port=int(sae.const.MYSQL_PORT),charset='utf8')

  8.   return conn

  9.  

  10. #执行SQL查询,结果以list(dict...)的形式返回

  11. def query_for_list(query,*param):

  12.   try:

  13.     conn=get_conn()

  14.     try:

  15.       cur=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)

  16.       cur.execute(query, param)

  17.       result = cur.fetchall()

  18.       return result

  19.     finally:

  20.       cur.close()

  21.       conn.close()

  22.    ...

继续阅读