Python 任意字典写入 MySQL

Catalog

前言

应用场景
爬虫数据写入数据库(假如老板要你写100个爬虫)
优点
通用、直接复制可用
基础补充(阅读此文前需要有以下2个基础)
1、Python 任意字典自动生成 SQL insert 语句
2、Python 连接 MySQL

普通情况

import pymysql
def insert(base_name, table_name, dic):
    # 连接数据库,创建游标
    db = pymysql.connect('localhost', 'root', 'yellow', charset='utf8', db=base_name)
    cursor = db.cursor()
    # 字典生成sql
    ls = list(dic)
    sentence = 'insert %s (' % table_name + ','.join(ls) + ') values (' +\
               ','.join(['%({})r'.format(field) for field in ls]) + ');'
    sentence = sentence % dic
    print(sentence)
    # 执行、提交、关闭
    try:
        cursor.execute(sentence)
        db.commit()
    except Exception as error:
        print('\033[033m%s\033[0m' % error)  # yellow
    cursor.close()
    db.close()
# test
d = {'name': 'ArYe', 'age': 15}
insert('z_test', 'draft', d)

其它情况2:字典values含None

方法1:字典传参

import pymysql
class Mysql:
    # 连接数据库,选择库
    def __init__(self, database, table):
        self.db = pymysql.connect('localhost', 'root', 'yellow', charset='utf8', db=database)
        self.cursor = self.db.cursor()
        self.table = table
    # 执行SQL
    def execute(self, sentence, arg=None):
        try:
            if arg:
                self.cursor.execute(sentence, arg)
            else:
                self.cursor.execute(sentence)
            for i in self.cursor.fetchall():
                print(*i, sep=' | ')
            self.db.commit()
        except Exception as error:
            print('\033[033m%s\033[0m' % error)  # yellow
    # 退出
    def close(self):
        self.cursor.close()
        self.db.close()
    # 将字典写入MySQL
    def insert(self, dic):
        ls = list(dic)
        sentence = 'insert %s(' % self.table + ','.join(ls) + ')values(' +\
                   ','.join(['%({})s'.format(field) for field in ls]) + ');'
        self.execute(sentence, dic)

方法2:直接生成sql,不传参

import pymysql
class Mysql:
    # 连接数据库,选择库
    def __init__(self, database, table):
        self.db = pymysql.connect('localhost', 'root', 'yellow', charset='utf8',
                                  db=database)
        self.cursor = self.db.cursor()
        self.table = table
    # 执行SQL
    def execute(self, sentence):
        try:
            self.cursor.execute(sentence)
            for i in self.cursor.fetchall():
                print(*i, sep=' | ')
            self.db.commit()
        except Exception as error:
            print('\033[033m', error, '\033[0m')  # yellow
    # 退出
    def close(self):
        self.cursor.close()
        self.db.close()
    # 将字典写入MySQL
    def insert(self, dic):
        data_type = {
            type("str"): '"%({})s"',
            type(0): '%({})s',
            type(0.1): '%({})s',
            type(None): 'null'}
        ls = list(dic)
        sentence = 'insert %s (' % self.table + ','.join(ls) + ') values (' +\
                   ','.join([data_type[type(dic[i])].format(i) for i in ls]) + ');'
        sentence = sentence % dic
        print(sentence)
        self.execute(sentence)

练习和测试

  • 建表代码
import pymysql
def sql(sentences):
    db = pymysql.connect('localhost', 'root', 'yellow', charset='utf8')
    cursor = db.cursor()
    # 执行
    for sentence in sentences:
        cursor.execute(sentence)
        for i in cursor.fetchall():
            print(*i, sep=' | ')
        db.commit()
    # 退出
    cursor.close()
    db.close()
cb = 'create database z_test; '
u = 'use z_test; '
bt = '''
    create table draft ( 
    id int(5) not null auto_increment, 
    name char(9), 
    age tinyint(3), 
    weight float(5.2), 
    height float(5.2), 
    birth date, 
    insert_time timestamp, 
    primary key (id) );
'''
# test
sql([cb, u, bt])
# sql(['drop database z_test;'])  # 玩够了就删库吧
  • 写表代码
# 连接MySQL
db = Mysql('z_test', 'draft')
d = {'name': 'ArYe', 'age': 15, 'height': 158.01, 'birth': '1992-04-20'}
db.insert(d)
db.execute('select * from draft')
db.close()

猜你喜欢

转载自blog.csdn.net/Yellow_python/article/details/80818179