Python連接操作My sql數據庫

#coding=utf-8
#連接數據庫測試
import  pymysql

def OpenMysql(host,user,password,db):
    db = pymysql.connect(host=host, user=user, password=password, db=db,
                         cursorclass=pymysql.cursors.DictCursor)
    return db
def Select_All(db):
    cur = db.cursor()
    sql_dict = {}
    # 查詢操作
    sql = "select * from jz2_low"
    try:
        cur.execute(sql)
        result = cur.fetchall()
        return result
    except Exception as e:
        print("查詢全部數據錯誤", e)
    finally:
        db.close()
def RunSqlShell(db,shell):
    cur = db.cursor()
    sql_dict = {}
    # 查詢操作
    sql = shell
    try:
        cur.execute(sql)
        result = cur.fetchall()
        return result
    except Exception as e:
        print("RunShellError:", e)
    finally:
        db.close()

在想調用的地方引入使用

import OpenMysql,Select_All、
 db = OpenMysql("192.168.10.229", "root", "123456", "perforamance")
 result = Select_All(db)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章