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

边栏推荐
- 微信小程序仿input组件、虚拟键盘
- In-depth analysis if according to data authority @datascope (annotation + AOP + dynamic sql splicing) [step by step, with analysis process]
- Source code analysis of Nacos configuration service (full)
- vscode笔记
- 指针常量与常量指针 巧记
- Nacos集群搭建
- 数据库多表关联插入数据
- Freeswitch操作基本配置
- Error correction notes for the book Image Processing, Analysis and Machine Vision
- 利用将网页项目部署到阿里云上(ngnix)
猜你喜欢
随机推荐
在STM32中使用printf函数
Technical Analysis Mode (7) Playing the Gap
自营商城提高用户留存小技巧,商城对接小游戏分享
lingo入门——河北省第三届研究生建模竞赛B题
Redis
长度以及颜色单位基本概念
LaTeX笔记
图像处理、分析与机器视觉一书纠错笔记
DevExpress中针对指定列进行百分比转换
超简单的白鹭egret项目添加图片详细教程
利用将网页项目部署到阿里云上(ngnix)
开源中国活动合作说明书
基于快速行进平方法的水面无人船路径规划
多用户商城多商户B2B2C拼团砍价秒杀支持小程序H5+APP全开源
NB-IOT智能云家具项目系列实站
1、Citrix XenDesktop 2203之AD域系统安装(一)
Shared memory + inotify mechanism to achieve multi-process low-latency data sharing
document.querySelector()方法
指针常量与常量指针 巧记
ndk编译so库









