【问题标题】:update with csv file using python使用 python 更新 csv 文件
【发布时间】:2022-01-16 19:21:17
【问题描述】:

我必须使用 CSV 文件更新数据库。考虑数据库表如下所示:

CSV 文件数据如下所示:

您可以看到 CSV 文件数据修改了一些数据并添加了一些新记录,我应该做的是仅更新已修改的数据或添加的一些新记录。

在 Table2 中,col2 的第一条记录被修改。我只需要更新 col2 的第一条记录(即 AA),而不是 col2 的全部记录。

我可以通过硬编码来做到这一点,但我不想通过硬编码来做到这一点,因为我需要使用 2000 个表来做到这一点。

谁能建议我实现目标的步骤。

这是我的代码 sn-p..

df = pd.read_csv('F:\\filename.csv', sep=",", header=0, dtype=str)

sql_query2 = engine.execute('''
                               SELECT
                               *
                               FROM ttcmcs023111temp
                               ''')

df2 = pd.DataFrame(sql_query2)
df.update(df2)

【问题讨论】:

  • 仅供参考,一些基本的格式和语法在发布问题时会有很长的路要走。请确保你使用基本的东西,例如你的句子开头的单词大写是最多的。
  • 到目前为止你写了什么代码?贴出代码sn-p。
  • 好的..你能给我任何关于我的问题的建议
  • do not post images of code, data, error messages, etc.,将信息添加为文本(在代码围栏等内)。
  • 编辑您的问题并包含代码。

标签: python sql sql-server file-handling


【解决方案1】:

由于我没有与您类似的数据,因此我使用了自己的数据库。 我的书籍表的架构如下:

+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int(11)     | NO   | PRI | NULL    |       |
| name   | varchar(30) | NO   |     | NULL    |       |
| author | char(30)    | NO   |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+

表格如下所示:

+----+--------------------+------------------+
| id | name               | author           |
+----+--------------------+------------------+
|  1 | Origin             | Dan Brown        |
|  2 | River God          | Wilbur Smith     |
|  3 | Chromosome 6       | Robin Cook       |
|  4 | Where Eagles Dare  | Alistair Maclean |
|  5 | The Seventh Scroll | Dan Brown        |  ### Added wrong entry to prove 
+----+--------------------+------------------+  ### my point  

所以,我的方法是使用 python 从 CSV 创建一个与 books 表具有相同架构的新临时表。 我使用的代码如下:

sql_query = sqlalchemy.text("CREATE TABLE temp (id int primary key, name varchar(30) not null, author varchar(30) not null)")
result = db_connection.execute(sql_query)
csv_df.to_sql('temp', con = db_connection, index = False, if_exists = 'append')

这样创建了一个表:

+----+--------------------+------------------+
| id | name               | author           |
+----+--------------------+------------------+
|  1 | Origin             | Dan Brown        |
|  2 | River God          | Wilbur Smith     |
|  3 | Chromosome 6       | Robin Cook       |
|  4 | Where Eagles Dare  | Alistair Maclean |
|  5 | The Seventh Scroll | Wilbur Smith     |
+----+--------------------+------------------+

现在,您只需在 MySQL 中使用 update 使用 INNER JOIN 来更新您想要在原始表中更新的值。 (在我的例子中,'书')。

你可以这样做:

statement = '''update books b
inner join temp t
on t.id = b.id
set b.name = t.name,
b.author = t.author;
'''
db_connection.execute(statement)

此查询将从我使用 CSV 创建的表 temp 中更新表 books 中的值。

您可以在更新值后销毁temp 表。

【讨论】:

  • 感谢@VishalA,同样的事情我需要同时处理多张桌子,比如大约 2000 张桌子,如果你有任何解决方案或线索,请分享给我。
  • 如果每个表都有类似的架构,您可以在 for 循环中执行操作并在每次迭代结束时销毁 temp
  • statement = '''update books b inner join temp t on t.id = b.id set b.name = t.name, b.author = t.author; ''',在这里我觉得将其放入循环中很复杂,因为每个表都有不同的列名
  • 创建列名列表并将这些值作为变量传递给 SQL 查询。你可以在这里得到一些想法:stackoverflow.com/questions/902408/…
  • 照你说的让我试试
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-19
相关资源
最近更新 更多