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

Python保存dict字典類型數(shù)據(jù)到Mysql并自動(dòng)創(chuàng)建表與列

 更新時(shí)間:2022年02月11日 17:22:39   作者:呆萌的代Ma  
這篇文章主要介紹了Python保存dict字典類型數(shù)據(jù)到Mysql并自動(dòng)創(chuàng)建表與列,字典是另一種可變?nèi)萜髂P?,且可存?chǔ)任意類型對(duì)象,想了解更多內(nèi)容的小伙伴可以和小編一起進(jìn)入下面文章學(xué)習(xí)更多內(nèi)容,希望對(duì)你有所幫助

字典是另一種可變?nèi)萜髂P?,且可存?chǔ)任意類型對(duì)象,主要是工具類,

接下來使用pymysql來創(chuàng)建表與SQL

下面來看看示例代碼:

import pymysql


class UseMysql(object):
? ? def __init__(self, user, passwd, db, host="127.0.0.1", port=3306):
? ? ? ? self.db = db
? ? ? ? self.conn = pymysql.connect(
? ? ? ? ? ? host=host, user=user, passwd=passwd, db=db, port=port, charset='utf8') ?# 鏈接數(shù)據(jù)庫
? ? ? ? self.cursor = self.conn.cursor()

? ? def table_exists(self, table_name) -> bool:
? ? ? ? """判斷表是否存在
? ? ? ? :param table_name: 表名
? ? ? ? :return: 存在返回True,不存在返回False
? ? ? ? """
? ? ? ? sql = "show tables;"
? ? ? ? self.cursor.execute(sql)
? ? ? ? tables = self.cursor.fetchall()
? ? ? ? for _t in tables:
? ? ? ? ? ? if table_name == _t[0]:
? ? ? ? ? ? ? ? return True
? ? ? ? return False

? ? def create_table(self, data: dict, table_name):
? ? ? ? """創(chuàng)建表"""
? ? ? ? # 構(gòu)造數(shù)據(jù)庫
? ? ? ? sql_key_str = ''
? ? ? ? columnStyle = ' text' ?# 數(shù)據(jù)庫字段類型
? ? ? ? for key in data.keys():
? ? ? ? ? ? sql_key_str = sql_key_str + ' ' + key + columnStyle + ','
? ? ? ? self.cursor.execute("CREATE TABLE %s (%s)" % (table_name, sql_key_str[:-1]))
? ? ? ? # 添加自增ID
? ? ? ? self.cursor.execute("""ALTER TABLE `{}` \
? ? ? ? ? ? ? ? ? ? ADD COLUMN `id` INT NOT NULL AUTO_INCREMENT FIRST, \
? ? ? ? ? ? ? ? ? ? ADD PRIMARY KEY (`id`);"""
? ? ? ? ? ? ? ? ? ? ? ? ? ? .format(table_name))
? ? ? ? # 添加創(chuàng)建時(shí)間
? ? ? ? self.cursor.execute(
? ? ? ? ? ? """ALTER TABLE {} ADD join_time timestamp NULL DEFAULT current_timestamp();""".format(table_name))

? ? def write_dict(self, data: dict, table_name):
? ? ? ? """
? ? ? ? 寫入mysql,如果沒有表,創(chuàng)建表
? ? ? ? :param data: 字典類型
? ? ? ? :param table_name: 表名
? ? ? ? :return:
? ? ? ? """
? ? ? ? if not self.table_exists(table_name):
? ? ? ? ? ? self.create_table(data, table_name)
? ? ? ? sql_key = '' ?# 數(shù)據(jù)庫行字段
? ? ? ? sql_value = '' ?# 數(shù)據(jù)庫值
? ? ? ? for key in data.keys(): ?# 生成insert插入語句
? ? ? ? ? ? sql_value = (sql_value + '"' + pymysql.escape_string(data[key]) + '"' + ',')
? ? ? ? ? ? sql_key = sql_key + ' ' + key + ','

? ? ? ? self.cursor.execute(
? ? ? ? ? ? "INSERT INTO %s (%s) VALUES (%s)" % (table_name, sql_key[:-1], sql_value[:-1]))
? ? ? ? self.conn.commit() ?# 提交當(dāng)前事務(wù)


if __name__ == '__main__':
? ? mysql = UseMysql('用戶名', '密碼', '數(shù)據(jù)庫名')
? ? my_data1 = {"col1": "a", "col2": "b", "col3": "c", }
? ? mysql.write_dict(my_data1, table_name="mytable")
? ? my_data2 = {"col1": "a2", "col2": "b2"}
? ? mysql.write_dict(my_data2, table_name="mytable")

到此這篇關(guān)于Python保存dict字典類型數(shù)據(jù)到Mysql并自動(dòng)創(chuàng)建表與列的文章就介紹到這了,更多相關(guān)Python保存dict字典類型數(shù)據(jù)到Mysql并自動(dòng)創(chuàng)建表與列內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

平昌县| 沿河| 梅河口市| 吴桥县| 长宁区| 海门市| 阿城市| 黄龙县| 泽普县| 安徽省| 保亭| 英吉沙县| 灌阳县| 泗洪县| 乐昌市| 镇坪县| 襄樊市| 尼玛县| 拉萨市| 英超| 常熟市| 鄂州市| 那坡县| 房山区| 宜兰市| 通河县| 兴和县| 澳门| 正阳县| 镇赉县| 刚察县| 内江市| 安义县| 多伦县| 遵义县| 罗江县| 广灵县| 桂平市| 仲巴县| 大连市| 安阳市|