当前位置:网站首页>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)
结果

边栏推荐
猜你喜欢

深入分析若依数据权限@datascope (注解+AOP+动态sql拼接) 【循序渐进,附分析过程】

docker部署完mysql无法连接

Shared memory + inotify mechanism to achieve multi-process low-latency data sharing

Quick Start to Drools Rule Engine (1)

多用户商城多商户B2B2C拼团砍价秒杀支持小程序H5+APP全开源

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

2022杭电多校六 1006-Maex (树形DP)

The 25 best free games on mobile in 2020

超简单的白鹭egret项目添加图片详细教程

typescript66-分析partial的实现
随机推荐
【FAQ】CCAPI兼容EOS相机列表(2022年8月 更新)
HelloWorld
txt文件英语单词词频统计
文本样式这一篇文章就够了
After docker is deployed, mysql cannot connect
Tips for formatting code indentation
铠侠携手Aerospike提升数据库应用性能
ev加密视频转换成MP4格式,亲测可用
typescript64-映射类型
VSCode编写OpenCV
AH8669-AC380/VAC220V转降5V12V24V500MA内电源芯片IC方案
【内推】新相微电子
格式化代码缩进的小技巧
文件内音频的时长统计并生成csv文件
(四)旋转物体检测数据roLabelImg转DOTA格式
Redis的使用
Some basic method records of commonly used languages in LeetCode
JS控制只能输入数字并且最多允许小数点两位
淘宝客APP带自营商城本地生活CPS外卖优惠电影票话费更新渠道跟单生活特权V3
(2022杭电多校六)1012-Loop(单调栈+思维)