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

边栏推荐
猜你喜欢

After docker is deployed, mysql cannot connect

白鹭egret添加新页面教程,如何添加新页面

LabVIEW中如何实现任意形状的不规则按键
D45_Camera assembly Camera

The 25 best free games on mobile in 2020

typescript67-索引查询类型

MySQL:基础部分
![In-depth analysis if according to data authority @datascope (annotation + AOP + dynamic sql splicing) [step by step, with analysis process]](/img/b5/03f55bb9058c08a48eae368233376c.png)
In-depth analysis if according to data authority @datascope (annotation + AOP + dynamic sql splicing) [step by step, with analysis process]

【MyCat简单介绍】

uniapp打包次数限制怎么办?只需两步就能解决
随机推荐
Writing OpenCV in VSCode
After docker is deployed, mysql cannot connect
技术分析模式(八)双顶和底
格式化代码缩进的小技巧
技术分析模式(十)头肩图案
typescript68-索引查询类型(查询多个)
h5页面回退到微信小程序并携带参数
txt文件英语单词词频统计
NACOS Configuration Center Settings Profile
DevExpress中针对指定列进行百分比转换
概率与期望部分题解
盒子模型大详解
Collision, character controller, Cloth components (cloth), joints in the Unity physics engine
Detailed explanation of the construction process of Nacos cluster
八大排序之快速排序
numpy.random使用文档
lingo入门——河北省第三届研究生建模竞赛B题
(JLK105D)中山爆款LED恒流电源芯片方案
铠侠携手Aerospike提升数据库应用性能
【JVM调优】Xms和Xmx为什么要保持一致