簡體   English   中英

運行 SQL 查詢 UPDATE 語句以循環遍歷結果集中的每一行並更新列的 Python 腳本

[英]Python Script to run SQL query UPDATE statement to loop through each row in result set and update columns

紐比正在做我的第一個項目。 對不起這個解釋。

我有 2 個表:t1:具有單行(唯一項目 ID)和 3 個狀態字段的主表,s1、s2、s3

t2:具有重復 project_id 的列表表,其中包含 3 個狀態字段 s1、s2、s3(以及此處不相關的其他數據)。 s1-3 字段中的值為 true(1) 或 false(0)

表 1:項目 ID、狀態 1、狀態 2、狀態 3

表 2:記錄 ID、項目 ID、名稱、狀態 1、狀態 2、狀態 3

我正在使用 Python 運行 mysql 查詢。

我想遍歷 t1 中具有唯一記錄 project_id 的行,然后使用每個 t1.project_id 查詢 t2 加入 t1.projectid 並從 t2 的每個項目/狀態字段的最后狀態條目更新 t1 狀態字段。

到目前為止,我已經完成了此操作,但無法將 id 傳遞給查詢 2。再次,對不起,我真的是在嘗試通過這個嘗試學習一些東西。

import mysql.connector as msql
from mysql.connector import Error

Host = "192.168.0.10"
Port = 3306
User = ""       
Password = ""           
database = "projectdata"

conn = msql.connect(host=Host,port=Port, user=User, password=Password, database=database)
cursor1 = conn.cursor() 
cursor2 = conn.cursor()

sql = ("select id from table1 t1") cursor1.execute(sql) 
t1 = cursor1.fetchall()

for row in t1:

sql2 = ("SELECT recordid,project_id, s1, s2, s3
FROM table2 t2
WHERE t2.project_id = 't1.project_id'
ORDER by id, recordid DESC LIMIT 1")

cursor2.execute(sql) t2 = cursor2.fetchall()

我還沒有進入更新 t1 部分,因為我還沒有弄清楚如何通過 t1 進行循環並查詢 t2 以獲取更新 t1 的結果。

t1 before update
|project_id |s1 |s2 |s3 |
|-----------|---|---|---|
|1003       |   |   |   |
|1005       |   |   |   |
|1001       |   |   |   |
|1002       |   |   |   |

t2 example 
|recordid   |project_id |s1         |s2         |s3         |
|-----------|-----------|-----------|-----------|-----------|
|1          |1001       |           |0          |1          |
|2          |1002       |1          |           |0          |
|3          |1003       |           |1          |           |
|4          |1001       |0          |           |           |
|5          |1002       |0          |0          |           |
|6          |1005       |           |1          |           |
|7          |1003       |1          |           |1          |

t1 期望結果的示例。 實時狀態字段是該特定 project_id 的最后狀態更改,按 t2 的 recordid 排序

t1 after update
|project_id |s1         |s2         |s3         |
|-----------|-----------|-----------|-----------|
|1003       |1          |1          |1          |
|1005       |null       |1          |null       |
|1001       |0          |0          |1          |
|1002       |0          |0          |null       |

在此先感謝,對不起,新手。 我可以使用 vb 和 msAccess 查詢來做到這一點,但需要學習比使用 msAccess 更好的方法。

如果我理解正確,您想動態提供 id。 要實現這一點,您需要使用占位符。

sql2 = ("SELECT recordid,project_id, s1, s2, s3
FROM table2 t2
WHERE t2.project_id = %s and s1 = '0' or '1'
ORDER by id, recordid DESC LIMIT 1")

您將零件設置為 %s 占位符。 然后,您需要按順序使用變量構建一個列表。 在您的情況下,它似乎只是 id。

 placeholder = [row[1]]

然后你給游標和占位符列表。

 cursor1.execute(sql,placeholder)

希望能幫助到你!

--------------循環查詢1的結果------------

我不能發表評論,所以我需要這樣做。 如果計划或可能進行結構更改,最好使用 pandas,就像你說的那樣,因為數據幀更像是 dicts,你不需要使用 index.

r1 = cursor1.fetchall()
for row in r1:
  sql2 = ("SELECT recordid,project_id, s1, s2, s3
              FROM table2 t2
              WHERE t2.project_id = %s
              ORDER by id, recordid DESC LIMIT 1")
   id = [row[0]]
   cursor2.execute(sql2,id)
   cursor2.commit 
   update = ("UPDATE t1
              set s1 = %s, s2 = %s, s3 = %s 
              where project_id = %s")
   line = cursor2.fetchone()
   upt_data = [line[2],line[3],line[4],line[1],]
   cursor3.execute(update,upt_data)
   cursor3.commit

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM