Python连接数据库,并将python的DataFrame写入数据库,并改写数据库特定字段

Python连接数据库,并将python的DataFrame写入数据库,并改写数据库特定字段

  • 代码
import pymysql
import pandas as pd
from sqlalchemy import create_engine
from urllib.parse import quote_plus as urlquote

data = pd.read_csv(r'D:\ATT&CK子技术描述all-v1-translated.csv', encoding='gbk')
dic = {"技术_英": "technique_en", "子技术_英": "sub_technique_en", "子技术ID": "sub_technique_ID", "技术ID": "technique_ID",
       "战术ID": "tactic_ID", "战术_英": "tactic_en", "描述_英": "description_en", "描述_中": "description_cn"}
data.rename(columns=dic, inplace=True)
print(data.head())


def write_to_sql(data, table, db='nsrep_process'):
    password = 'xxx@xxx123'
    ipaddress = '192.168.1.3'
    port = '3306'
    engine = create_engine(
        f'mysql+pymysql://root:{urlquote(password)}@{ipaddress}:{port}/{db}?charset=utf8')  # 密码中包含特殊字符,如@等,所以密码中有特殊字符需要转码,否则在拼接时会认为是密码和IP地址拼接的@字符,为了避免特殊字符的影响,采用urlquote(特殊字符)解决问题
    try:
        data.to_sql(table, con=engine, if_exists='append', index=False) #append是向末尾追加元素
        # append表示在原有表基础上增加,但该表要有表头
    except Exception as e:
        print(e)


def generate_mysql(sql, db='nsrep_process'):
    conn = pymysql.connect(
        host='192.168.1.3',
        user='root',
        password='xxx@xxx123',
        port=3306,
        charset='utf8',
        db=db)
    cursor = conn.cursor()  # 创建游标

    cursor.execute(sql)  # 执行sql语句
    conn.commit()  # 对数据库修改后必须进行此操作,类似于保存键
    cursor.close()  # 关闭游标
    conn.close()  # 关闭数据库


# 将翻译数据写入数据库
dic = {"技术_英": "technique_en", "子技术_英": "sub_technique_en", "子技术ID": "sub_technique_ID", "技术ID": "technique_ID",
       "战术ID": "tactic_ID", "战术_英": "tactic_en", "描述_英": "description_en", "描述_中": "description_cn"}
sql = 'CREATE TABLE IF NOT EXISTS translation_to_chinese (technique_en VARCHAR(2000),sub_technique_en VARCHAR(2000),sub_technique_ID VARCHAR(2000),technique_ID VARCHAR(2000),tactic_ID VARCHAR(2000),tactic_en VARCHAR(2000),description_en VARCHAR(2000),description_cn VARCHAR(2000))'
# varchar(N)的逻辑意义从MySQL4.1开始,varchar (N)中的N指的是该字段最多能存储多少个字符(characters),不是字节数。
# 不管是一个中英文字符或者数字、或者一个汉字,都当做一个字符。在4.1之前,N表示的是最大存储的字节数(bytes)。
generate_mysql(sql)
write_to_sql(data=data, table='translation_to_chinese')

# 将table1中的name2字段的值设置为table2中的与table1同关联name1的数据的name3的值
"""
UPDATE table1 a
JOIN table2 b ON a.name1 = b.name1
SET b.name2 = a.name3
WHERE
	a.name4 IS NOT NULL
AND a.name5 IS NULL;

"""
# 将战术描述写入tactic表的en_description
sql1 = 'UPDATE translation_to_chinese a JOIN tactic b ON a.tactic_en = b.en_name SET b.cn_description = a.description_cn WHERE a.tactic_ID IS NOT NULL;'
generate_mysql(sql1)
# 将子技术描述写入technique_sub表的cn_description
sql2 = 'UPDATE translation_to_chinese a JOIN technique_sub b ON a.sub_technique_en = b.en_name SET b.cn_description = a.description_cn WHERE a.sub_technique_ID IS NOT NULL;'
generate_mysql(sql2)

# 将不包含子技术的技术描述写入technique表的cn_description
sql3 = 'UPDATE translation_to_chinese a JOIN technique b ON a.technique_en = b.en_name SET b.cn_description = a.description_cn WHERE a.technique_ID IS NOT NULL AND a.sub_technique_ID IS NULL;'
generate_mysql(sql3)

你可能感兴趣的:(MySQL,python技巧,pycharm,python,数据库,开发语言)