使用Python文件I/O或MySQL查询更新现有数据库并插入新数据库
我拥有或正在使用的内容:使用Python文件I/O或MySQL查询更新现有数据库并插入新数据库,python,mysql,sql,python-2.7,Python,Mysql,Sql,Python 2.7,我拥有或正在使用的内容: 我有两个数据库- 1.在线数据库(表中包含大约37k个条目), 2.我的本地数据库(表包含大约55k个条目)。 我在服务器和本地都使用phpMyAdmin。 在线phpMyAdmin和本地phpMyAdmin版本-5.5.42-cll-MySQL社区服务器(GPL)(两者相同) 我想做什么: 我想更新现有数据,并从本地数据库在联机数据库的表中插入新数据。 注意:我不能删除或替换表,因为它是相互连接的。它给出了外键约束错误和主键约束错误。 我想做什么: 我试图从本地数
我有两个数据库-
1.在线数据库(表中包含大约37k个条目),
2.我的本地数据库(表包含大约55k个条目)。
我在服务器和本地都使用phpMyAdmin。
在线phpMyAdmin和本地phpMyAdmin版本-5.5.42-cll-MySQL社区服务器(GPL)(两者相同)
我想做什么:
我想更新现有数据,并从本地数据库在联机数据库的表中插入新数据。
注意:我不能删除或替换表,因为它是相互连接的。它给出了
外键约束错误
和主键约束错误
。
我想做什么:
我试图从本地数据库导出,并使用phpMyAdmin导入到联机数据库,但收到错误消息:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''CHEA B C13279', 'CHE', 'CHK', 'Reaction kinetics can be studied by', '0', ' ' at line 1
我尝试过导出不同类型的文件格式,如SQL
、CSV
、XML
,以及使用INSERT
、UPDATE
和REPLACE
自定义SQL文件,但失败了。
我想做什么:
以SQL INSERT文件格式导出我的本地和联机数据库,然后使用Python文件I/O和字符串格式比较这两个文件,并创建两个新文件,以
更新
数据库表中的现有数据和插入
新数据数据库表。
到目前为止我的Python代码:
online = "questions(online).sql"
local = "questions(local).sql"
temp1 = "temp/questions(online)"
temp2 = "temp/questions(local)"
newFileName ="questions(insert).sql"
newFileName2 ="questions(update).sql"
open(newFileName, "a") as insert
open(newFileName2, "a") as update
with open(local, "r") as localfile:
with open(online, "r") as onlinefile:
for line in localfile:
for line2 in onlinefile:
if line == line2:
update.write(line)
else:
insert.write(line)
insert.close()
update.close()
运行上述脚本后,我得到的输出为:在更新文件中仅写入1行。
在“插入文件”中写入联机文件的所有条目-1。
注意:
1.我个人在本地计算机上编辑了超过15k个条目,因此在比较两个数据库时,不可能只有一个条目不同。
2.在插入文件中,应写入非重复条目,但它写入了在线数据库中的所有条目,更新文件中的重复条目除外。
3.实际上,数据库的file或
主键
行的唯一性都在前几个字母中。但我不知道如何仅使用两个文件中每行的前几个字母来比较这行。
编辑:
两个SQL文件中的文本类似于此:
在线数据库文件示例
('CHEA B C13279', 'CHE', 'CHK', 'Reaction kinetics can be studied by', 0),
('CHEA B C13281', 'CHE', 'CHK', 'Half - life is independent', 0),
('CHEA B C15590', 'CHE', 'Elo', 'A horizontal plank has a rectangular block', 0),
('PHYPTEST3217', 'PHY', 'PT', 'Two particles of same mass and charge', 0),
('PHYMP34780', 'PHY', 'MP', 'What id the momentum of X-rays', 0),
('MATCUS10678', 'MAT', 'DC', 'If $\\int x^{-3}.{5^{x^\\frac{1}{^2}}}dx{5^{x^\\frac{1}{^2}}} $ then $ k $ is', 0),
('CHEA B C13279', 'CHE', 'CHK', 'Reaction kinetics can be studied by', '0'), #Unchanged content
('CHEA B C13281', 'CHE', 'CHK', 'This is edited and updated', '0'), #Updated content
('CHEA B C15590', 'CHE', 'Elo', 'This is edited and updated', '0'), #Updated content
('PHYPTEST3217', 'PHY', 'PT', 'This is edited and updated', 0), #Updated content
('PHYMP34780', 'PHY', 'MP', 'What id the momentum of X-rays', 0), #New contents
('MATCUS10678', 'MAT', 'DC', 'If $\\int x^{-3}.{5^{x^\\frac{1}{^2}}}dx{5^{x^\\frac{1}{^2}}} $ then $ k $ is', 0), #New contents
('CHEVHV14696', 'CHE', 'TCE', 'Variation of heat of reaction with temperature is given by Kirchhoff''s equation, which is :', '0'), #New contents
('MAT2D35456', 'MAT', '2D', 'The shortest distance between the parabolas $y^{2}=4x$ and $y^{2}=2x-6$ is', '0'), #New contents
('PHYHaT33353', 'PHY', 'TP', 'What is the unit $pV$ in the gas equation $pV = \\ce{RT}?$', '0', '$\\ce{Nm}$', '0'), #New contents
('PHYWEP4745', 'PHY', 'WEP', 'A rubber ball of mass m and radius r is submerged', '0'), #New contents
本地数据库文件示例
('CHEA B C13279', 'CHE', 'CHK', 'Reaction kinetics can be studied by', 0),
('CHEA B C13281', 'CHE', 'CHK', 'Half - life is independent', 0),
('CHEA B C15590', 'CHE', 'Elo', 'A horizontal plank has a rectangular block', 0),
('PHYPTEST3217', 'PHY', 'PT', 'Two particles of same mass and charge', 0),
('PHYMP34780', 'PHY', 'MP', 'What id the momentum of X-rays', 0),
('MATCUS10678', 'MAT', 'DC', 'If $\\int x^{-3}.{5^{x^\\frac{1}{^2}}}dx{5^{x^\\frac{1}{^2}}} $ then $ k $ is', 0),
('CHEA B C13279', 'CHE', 'CHK', 'Reaction kinetics can be studied by', '0'), #Unchanged content
('CHEA B C13281', 'CHE', 'CHK', 'This is edited and updated', '0'), #Updated content
('CHEA B C15590', 'CHE', 'Elo', 'This is edited and updated', '0'), #Updated content
('PHYPTEST3217', 'PHY', 'PT', 'This is edited and updated', 0), #Updated content
('PHYMP34780', 'PHY', 'MP', 'What id the momentum of X-rays', 0), #New contents
('MATCUS10678', 'MAT', 'DC', 'If $\\int x^{-3}.{5^{x^\\frac{1}{^2}}}dx{5^{x^\\frac{1}{^2}}} $ then $ k $ is', 0), #New contents
('CHEVHV14696', 'CHE', 'TCE', 'Variation of heat of reaction with temperature is given by Kirchhoff''s equation, which is :', '0'), #New contents
('MAT2D35456', 'MAT', '2D', 'The shortest distance between the parabolas $y^{2}=4x$ and $y^{2}=2x-6$ is', '0'), #New contents
('PHYHaT33353', 'PHY', 'TP', 'What is the unit $pV$ in the gas equation $pV = \\ce{RT}?$', '0', '$\\ce{Nm}$', '0'), #New contents
('PHYWEP4745', 'PHY', 'WEP', 'A rubber ball of mass m and radius r is submerged', '0'), #New contents
所有这些都可以看作是元组。所以我有两个列表(文件),本地有55263个元组,在线有37548个元组。每个元组有相等数量的元素,即21个元素,它们是字符串、一些特殊字符(如$、^等)和整数
请用
Python
或SQL
query帮助我解决这个问题。我是语言初学者
提前谢谢你 您的解决方案是错误的。你的逻辑也错了。说明: 本地数据库文件:
Line 1: aaa
Line 2: bbb
Line 3: ccc
联机数据库文件:
Line 1: aaa
Line 2: bbb
Line 3: ccc
现在只需在循环内逐行运行代码:
迭代1:
line = aaa
line2 = aaa
**update**.write(line ) => **line == aaa**
迭代2:
line = aaa
line2 = bbb
**insert**.write(line ) => **line == aaa**
迭代3:
line = aaa
line2 = ccc
**insert**.write(line ) => **line == aaa**
发生了什么事?你只写了三次,同一行。一次更新,两次插入
实际情况是,您正在进行37k*55k的比较。每次你把行写入某个文件时
这是错误的做法
可能的解决办法:
mysql -u mysql_username -p new_db_name < /path/to/your/edited/database_local.sql
mysql-u mysql\u username-p new\u db\u name
然后检查所有功能是否正常,重命名数据库,或者强制应用程序使用新的数据库…抱歉,我已经尝试了
REPLACE
和,但没有成功<代码>禁用/删除约束、更新表、恢复约束
我不能这样做,因为数据是实时的,人们正在使用它。我对PL/SQL没有任何概念,但我会检查一下。我仍在使用Python脚本对本地文件进行相应的排序。我应用了你说的逻辑。但仍然有一些复杂的问题。正如我编辑过的问题,在.sql文件中比较两者,我们可以考虑<代码>主键< /代码>作为唯一性。但我必须以这种方式应用逻辑,当它开始比较在线文件和本地文件时,它应该只比较两个文件中的主键。在每行的前几个字母中。如果该行不匹配,则将其写入插入文件
,如果匹配,则将其写入临时文件。完成后,应再次运行新的比较。在此情况下,临时文件应再次与在线文件进行比较,如果行不相似,则应在新的更新文件中写入。多亏了您的逻辑,现在我能够整理并创建更新文件,在这里我只需比较整行。但是我在比较两个具有唯一性的文件时遇到了麻烦。我很困惑,不知道如何比较两个二维数组中的元素(我将这两个文件视为两个多元组列表)。请检查以上我的文件数据示例。我用于对更新文件进行排序的新代码是:集合中的元素(联机):如果元素在本地:继续其他:更新。写入(元素)
如果我使用了替换
,则会出现此错误<