当前位置:网站首页>Database table insert data
Database table insert data
2022-08-05 06:56:00 【Left, left to】
题引
During the process of inserting data into the database,If you need to insert numbers into multiple tables,And there are dependencies between multiple tables,Inserting data analysis at this time is too cumbersome
For example I have three tables:
- I want to insert data into these tables,But some relationships are strong associations
- Table structure attached: Idea database
备注: This method is not optimal,The association between multi-table inserts can be temporarily satisfied,Further improvements will be made if necessary
INSERT INTO film_details_table (film_id,film_details,film_photo,film_rating,film_type,film_specification,film_region,film_duration,release_date,updated_time) VALUES
('90020220619192851775',' 30Omnicom Television Productions adopted a baby last year,They deliberately nurtured him to become the most popular documentary soap opera in the world《楚门的世界》中的主人公,The company has achieved great success for this.However, only one person is completely unaware of all this,He is the only protagonist of the show——楚门.Truman has been living in a small town called Taoyuan Island since childhood (It's actually a huge studio),He was a broker for an insurance company in this small town,Truman seems to be living the exact same life as ordinary people,But he doesn't know that there are thousands of cameras facing him every second of his life,每时每刻全世界都在注视着他,Not to mention that everyone around him, including his wife and friends, is《楚门的世界》的演员.Although it feels like everyone seems to be paying attention to him,And everything you do from childhood to adulthood has some unexpected dramatic effects, But none of this made the naive young man take it too seriously.The production team of the program may be due to a momentary negligence,It was because of him that Truman was a child“死”的“父亲”再次露面,“父亲”Was taken away by staff immediately,Until Truman became grief-stricken and began to suspect, they weave a perfect lie for them“父子”相见,in order to achieve their satisfactory results.One was both many years ago《楚门的世界》The loyal audience of the show is the young girl Shi Weiya, who is very sympathetic to Truman,Truman fell in love with her at first sight.She gave Truman some kind hints,He was forcibly removed from the set.Truman, who recalls Sylvia, begins to rediscover his life.Gradually Truman discovered that everyone in the company he worked for started real work after he showed up,On the road near his house, the same people and cars are coming and going every day,What made him even more unbelievable,The wife who claims to be a doctor and goes to the hospital every day is not a doctor.Truman began to doubt the world he lived in,Including his wife、朋友、His father and everyone else were lying to him,A visceral fear arises spontaneously.痛定思痛,He decides to do whatever it takes to escape the seascape, a small town that keeps him silent,To find his true life and the person who truly loves him.然而,Truman underestimated the producer of the soap opera、The power of Christopher in director and executive power.Christopher designed everything to near perfection,近30Over the years, Truman was firmly controlled in the surreal world of the seascape.After several unsuccessful escapes,Truman decided to leave the small town by sea,However, he was disappointed to find that the sea and sky in front of him were also part of this huge studio,At this time, Christopher told Truman about the ins and outs of the matter in the huge control room in the sky,And tell Truman that he is now the most popular star in the world,What he has achieved today is unimaginable for ordinary people,If he wants to stay in Seaview, he can continue his star life,Truman was unmoved,Resolutely go to the road of freedom in the distance . ','https://p0.meituan.net/movie/[email protected]_644h_1e_1c','9.0',NULL,'剧情','美国','103 分钟','','2022-06-19 19:28:51');
INSERT INTO film_info_table (film_id,film_name,updated_time) VALUES
('90020220619192851775','楚门的世界 - The Truman Show','2022-06-19 19:28:51');
INSERT INTO film_type (film_id,film_type) VALUES
('90020220619192851775','科幻');
脚本
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : chairDu
# @Email : [email protected]
# @File : demo.py
# @DataTime : 2022-07-22 05:10:00
# @Description : Insert data into a multi-table parallel relationship
import os
import re
import sys
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
# Set the desktop address as a global variable
global fil_path
fil_path = "G:\\OneDrive\\桌面\\"
# Enter multiple insertssql
sql111 = """ INSERT INTO film_details_table (film_id,film_details,film_photo,film_rating,film_type,film_specification,film_region,film_duration,release_date,updated_time) VALUES ('90020220619192851775',' 30Omnicom Television Productions adopted a baby last year,They deliberately nurtured him to become the most popular documentary soap opera in the world《楚门的世界》中的主人公,The company has achieved great success for this.However, only one person is completely unaware of all this,He is the only protagonist of the show——楚门.Truman has been living in a small town called Taoyuan Island since childhood (It's actually a huge studio),He was a broker for an insurance company in this small town,Truman seems to be living the exact same life as ordinary people,But he doesn't know that there are thousands of cameras facing him every second of his life,每时每刻全世界都在注视着他,Not to mention that everyone around him, including his wife and friends, is《楚门的世界》的演员.Although it feels like everyone seems to be paying attention to him,And everything you do from childhood to adulthood has some unexpected dramatic effects, But none of this made the naive young man take it too seriously.The production team of the program may be due to a momentary negligence,It was because of him that Truman was a child“死”的“父亲”再次露面,“父亲”Was taken away by staff immediately,Until Truman became grief-stricken and began to suspect, they weave a perfect lie for them“父子”相见,in order to achieve their satisfactory results.One was both many years ago《楚门的世界》The loyal audience of the show is the young girl Shi Weiya, who is very sympathetic to Truman,Truman fell in love with her at first sight.She gave Truman some kind hints,He was forcibly removed from the set.Truman, who recalls Sylvia, begins to rediscover his life.Gradually Truman discovered that everyone in the company he worked for started real work after he showed up,On the road near his house, the same people and cars are coming and going every day,What made him even more unbelievable,The wife who claims to be a doctor and goes to the hospital every day is not a doctor.Truman began to doubt the world he lived in,Including his wife、朋友、His father and everyone else were lying to him,A visceral fear arises spontaneously.痛定思痛,He decides to do whatever it takes to escape the seascape, a small town that keeps him silent,To find his true life and the person who truly loves him.然而,Truman underestimated the producer of the soap opera、The power of Christopher in director and executive power.Christopher designed everything to near perfection,近30Over the years, Truman was firmly controlled in the surreal world of the seascape.After several unsuccessful escapes,Truman decided to leave the small town by sea,However, he was disappointed to find that the sea and sky in front of him were also part of this huge studio,At this time, Christopher told Truman about the ins and outs of the matter in the huge control room in the sky,And tell Truman that he is now the most popular star in the world,What he has achieved today is unimaginable for ordinary people,If he wants to stay in Seaview, he can continue his star life,Truman was unmoved,Resolutely go to the road of freedom in the distance . ','https://p0.meituan.net/movie/[email protected]_644h_1e_1c','9.0',NULL,'剧情','美国','103 分钟','','2022-06-19 19:28:51'); INSERT INTO film_info_table (film_id,film_name,updated_time) VALUES ('90020220619192851775','楚门的世界 - The Truman Show','2022-06-19 19:28:51'); INSERT INTO film_type (film_id,film_type) VALUES ('90020220619192851775','科幻'); """
def createInsertSql(_sql):
_path = "{0}insert.sql".format(fil_path)
fil = open(file=_path, mode="w+", encoding="utf-8")
_sql = re.sub("\n|\t", '', _sql).lower() # 去除换行符,制表符,and convert to lowercase
sqlList = re.split(";", _sql)[0:-1] # Split to remove all insertssql生成insert格式
allKey = [] # 所有字段
noRepetitionKey = [] # All unique fields
repetitionKey = [] # All repeating fields
setSql = {
} # 设置变量sql
insertSql = {
}
for sql in sqlList: # Summarize all fields
allKey += re.split(",", re.split("\(|\)", sql)[1])
for i in range(len(allKey) - 1): # Repeated fields are derived separately,不重复字段
for y in range(i + 1, len(allKey)):
if allKey[i] == allKey[y] and allKey[i] not in repetitionKey:
repetitionKey.append(allKey[i])
elif allKey[i] != allKey[y] and allKey[i] not in repetitionKey and allKey[i] not in noRepetitionKey:
noRepetitionKey.append(allKey[i])
elif allKey[i] != allKey[y] and allKey[y] not in repetitionKey and allKey[y] not in noRepetitionKey:
noRepetitionKey.append(allKey[y])
noRepetitionKey = list(set(noRepetitionKey) - set(repetitionKey))
for sql in sqlList: # 得出sql
splitSql = re.split("\(|\)", sql)
tableName = re.sub(r"insert.*into", "", splitSql[0]).strip() # 数据库表名
sqlKey = splitSql[1]
sqlKeyList = re.split(",", sqlKey)
sqlValueList = re.split(",", splitSql[3])
for i in range(len(sqlKeyList)):
if sqlKeyList[i] in repetitionKey:
_setSql = {
sqlKeyList[i]: ("\nset @{0} = {1} ;".format(sqlKeyList[i], sqlValueList[i]))}
setSql.update(_setSql)
sqlKeyList[i] = "@{0}".format(sqlKeyList[i])
insertSelectSql = "\n{0}({1}) \nselect {2} \nfrom {3} limit 1 ;".format(splitSql[0], sqlKey, ','.join(sqlKeyList), tableName) # 生成插入sql\
tableNames = "-- {0}".format(tableName)
__sql = {
tableNames: insertSelectSql}
insertSql.update(__sql)
# 插入数据
noRepetitionCont = "\n\n\n-- Unique fields: {0}".format(','.join(noRepetitionKey))
fil.write(noRepetitionCont)
repetitionCont = "\n-- 重复的字段: {0}\n".format(','.join(repetitionKey))
fil.write(repetitionCont)
for i in setSql.values():
fil.write(i)
print(".", end="")
for i in insertSql.keys():
fil.write("\n\n\n")
fil.write(i)
fil.write(insertSql.get(i))
print(".",end="")
fil.close()
return 1
if __name__ == '__main__':
createInsertSql(sql111)
结果

边栏推荐
猜你喜欢

numpy.random usage documentation

合工大苍穹战队视觉组培训Day9——相机标定

After docker is deployed, mysql cannot connect

开源中国活动合作说明书

Source code analysis of Nacos configuration service (full)

NACOS配置中心设置配置文件

Japan Sanitary Equipment Industry Association: Japan's warm water shower toilet seat shipments reached 100 million sets

自营商城提高用户留存小技巧,商城对接小游戏分享

在STM32中使用printf函数

2022杭电多校六 1007-Shinobu loves trip(同余方程)
随机推荐
盒子模型大详解
图像处理、分析与机器视觉一书纠错笔记
VSCode编写OpenCV
技术分析模式(九)三重顶部和底部
DevExpress中针对指定列进行百分比转换
JS控制只能输入数字并且最多允许小数点两位
指针常量与常量指针 巧记
盒子模型中过度约束问题及其解决办法
ev加密视频转换成MP4格式,亲测可用
文本样式这一篇文章就够了
【JVM调优】Xms和Xmx为什么要保持一致
typescript65-映射类型(keyof)
技术分析模式(十)头肩图案
开源中国活动合作说明书
(2022杭电多校六)1010-Planar graph(最小生成树)
更改小程序原生radio的颜色及大小
亚马逊美国站:马术头盔CPC认证标准要求
日本卫生设备行业协会:日本温水喷淋马桶座出货量达1亿套
单片机期末复习大题
uniapp打包次数限制怎么办?只需两步就能解决