PyMYSQL使用

由于Python3用PyMySQL取代MySQLdb, 为了兼容, 我们开发的运维管理平台也使用PySQL模块操作数据。

#coding: utf8

import pymysql.cursors

class MySQLD_CONN(object):
    # Connect to the database
    def get_mysql_connection(self, host='localhost', user='planx', passwd='planx', port=3306):
        conn=None
        try:
            conn = pymysql.connect(host, user, passwd, port=port,
                                   charset='utf8', cursorclass=pymysql.cursors.DictCursor)
        except Exception, e:
            print(e)
        return conn

    def change_table_data(self, conn, db_name, sql):
        # execute insert, update, delete SQL
        _status = "OK"
        if not conn:
            print("lost database connection!")
            return None

        try:
            with conn.cursor() as cursor:
                conn.select_db(db_name)
                cursor.execute(sql)
                conn.commit()
        except Exception,e:
            print(e)
            _status = "ERROR"
        finally:
            cursor.close()
        return _status
    
    def get_table_data(self, conn, db_name, sql):
        result=None
        if not conn:
            print("lost database connection")
            return result

        try:
            with conn.cursor() as cursor:
                conn.select_db(db_name)
                cursor.execute(sql)
                conn.commit()
                result = cursor.fetchall()
        except Exception,e:
            print(e) 
        finally:
            cursor.close()
        return result


mysql_conn=MySQL_CONN()

 

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇