最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python pymysql操作MySQL詳細(xì)

 更新時(shí)間:2021年09月26日 15:13:20   作者:tigeriaf  
pymysql是Python3.x中操作MySQL數(shù)據(jù)庫(kù)的模塊,其兼容于MySQLdb,使用方法也與MySQLdb幾乎相同,但是性能不如MySQLdb,但是由于其安裝使用方便、對(duì)中文兼容性也更好等優(yōu)點(diǎn),被廣泛使用??梢允褂胮ip install pymysql進(jìn)行安裝。

1、使用

1.1 簡(jiǎn)單使用

import pymysql

# 創(chuàng)建連接
con = pymysql.connect(
                        host='localhost',
                        port=3306,
                        user='root',
                        password='123456',
                        database='test',
                        charset='utf8'
)
# 創(chuàng)建游標(biāo)
cursor = con.cursor()

# 執(zhí)行新增SQL,返回受影響行數(shù)
row1 = cursor.execute("insert into user (username, password) values ('username3','password3')")
print(row1)

# 執(zhí)行更新SQL,返回受影響行數(shù)
row2 = cursor.execute("update user set password = '123456' where id > 2;")
# 執(zhí)行查詢(xún)SQL
res = cursor.execute("SELECT * FROM user;")
result = cursor.fetchall()
for info in result:
    print(info[0], info[1])
# 提交,不然無(wú)法保存新增或者更新的數(shù)據(jù)
con.commit()
# 關(guān)閉游標(biāo)
cursor.close()
# 關(guān)閉連接
con.close()

注意:數(shù)據(jù)庫(kù)表中存在中文時(shí),創(chuàng)建連接需要指定charset='utf8',否則中文顯示會(huì)亂碼。 其中cursor.fetchall()是獲取所有結(jié)果集,如果只有一條結(jié)果集,可以使用cursor.fetchone() 。

1.2 封裝工具類(lèi)

為了使用方便,可以直接封裝成工具類(lèi):

import pymysql


class MysqlHelper:
    def __init__(self, config):
        self.host = config["host"]
        self.port = config["port"]
        self.user = config["user"]
        self.password = config["password"]
        self.db = config["db"]
        self.charset = config["charset"]
        self.con = None
        self.cursor = None

    def create_con(self):
        """
        創(chuàng)建連接
        """
        try:
            self.con = pymysql.connect(host=self.host, port=self.port, user=self.user, password=self.password,
                                       database=self.db, charset='utf8')
            self.cursor = self.con.cursor()
            return True
        except Exception as e:
            print(e)
            return False

    #
    def close_con(self):
        """
        關(guān)閉鏈接
        """
        if self.cursor:
            self.cursor.close()
        if self.con:
            self.con.close()

    # sql執(zhí)行
    def execute_sql(self, sql):
        """
        執(zhí)行插入/更新/刪除語(yǔ)句
        """
        try:
            self.create_con()
            print(sql)
            self.cursor.execute(sql)
            self.con.commit()
        except Exception as e:
            print(e)
        finally:
            self.close_con()

    def select(self, sql, *args):
        """
        執(zhí)行查詢(xún)語(yǔ)句
        """
        try:
            self.create_con()
            print(sql)
            self.cursor.execute(sql, args)
            res = self.cursor.fetchall()
            return res
        except Exception as e:
            print(e)
            return False
        finally:
            self.close_con()

使用工具類(lèi):

config = {
    "host": 'localhost',
    "port": 3306,
    "user": 'root',
    "password": '123456',
    "db": 'test',
    "charset": 'utf8'
}
db = MysqlHelper(config)
db.execute_sql("insert into user (username, password) values ('username4','password4')")
db.select("SELECT * FROM user;")

到此這篇關(guān)于Python pymysql操作MySQL詳細(xì)的文章就介紹到這了,更多相關(guān)Python pymysql操作MySQL內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

蒲城县| 当雄县| 凤翔县| 福鼎市| 龙陵县| 武定县| 灵武市| 沧源| 娱乐| 英超| 丹巴县| 荣昌县| 佛冈县| 永定县| 敖汉旗| 迁安市| 华宁县| 沾益县| 阜阳市| 松原市| 大化| 共和县| 永州市| 中超| 新泰市| 荣昌县| 辛集市| 卓尼县| 洛阳市| 武清区| 长泰县| 山阳县| 吉首市| 策勒县| 连城县| 钦州市| 平利县| 娱乐| 辽阳市| 沂源县| 东安县|