我正在使用__rowid__sqlite 表默認的CRUD 操作。我的任何表中都沒有單獨的 ID 列。
我的創建、讀取和洗掉操作都完成了。
我正在按客戶的姓名搜索資料庫。
表

UPDATE查詢customers表
cursor.execute("""
SELECT * FROM customers
WHERE name = ?""", (name_variable.get(),))
cursor.execute("""
UPDATE customers SET
'contact' = ?,
'mail' = ?,
'address' = ?
WHERE name = ?
""",
(
contact_variable.get(),
mail_variable.get(),
address_variable.get(),
name_variable.get()
)
)
我的問題是更新services&charges表。
我想要的是,如果用戶更改John's資訊,那么我如何UPDATE只John's使用__rowid__. 我不明白如何執行該查詢。
(我在 Ubuntu 20.04 上使用 sqlite3 版本 3.31.1)。
uj5u.com熱心網友回復:
根據您顯示的架構,客戶、服務和費用之間沒有關系,因此更新客戶對其他表沒有影響。因此,您可能想要建立關系以及您所說的含義
那么我如何使用rowid僅將 John 的資料更新到這兩個表中
答案是 rowid 列,因為沒有關系,除了唯一標識相應表中的行之外,不做任何事情。
所以首先你需要定義需要的關系
- 兩個表(服務和費用)中的每一列都為有孩子的父母(客戶)提供服務(服務將是客戶的孩子,費用將是客戶的孩子)又名兩個一(客戶)到多(服務)和收費)關系,或
- 如果您需要多對多關系,請使用映射參考表。
通常,映射/參考/關聯/鏈接/關聯子項與父項的最有效方法是通過將其別名為列名(例如 id INTEGER PRIMARY KEY)來利用始終存在(但通常隱藏)的 rowid。
因此,您可能希望表格定義類似于:-
CREATE TABLE IF NOT EXISTS customers (
customer_id INTEGER PRIMARY KEY,
name TEXT,
contact TEXT,
mail TEXT,
address TEXT
);
- 該CUSTOMER_ID是ROWID列的別名
然后 :-
CREATE TABLE IF NOT EXISTS services (
serviceid INTEGER PRIMARY KEY,
service TEXT, subservice TEXT,
customer_id INTEGER REFERENCES customers(customer_id) ON DELETE CASCADE ON UPDATE CASCADE
);
- 所述的service_id列是ROWID列的別名
- 在CUSTOMER_ID列參考父,即客戶向誰服務屬于。
- REFERENCES 關鍵字以及表和相關列定義了一個約束,該約束表示 customer_id 列必須是存在于 customer_id 列中的值 customers 表(即外鍵約束(規則))。
then :- - ON DELETE CASCADE 表示如果洗掉父級,則父級的所有子級都將從父級中洗掉。- ON UPDATE 是類似的,但會級聯對 customer_id 表中 customer_id 列的任何更改。
CREATE TABLE IF NOT EXISTS charges (
initialcharges REAL,
taxes REAL,
discount REAL,
advance REAL,
total REAL,
customer_id INTEGER REFERENCES customers(customer_id) ON DELETE CASCADE ON UPDATE CASCADE
);
- 類似于服務
現在假設您然后插入一些 (2) 客戶(注意,對于演示,特定的 customer_id 值是指定的,而不是允許它們自動生成)使用:-
INSERT OR IGNORE INTO customers VALUES
(10,'John','something','something','something')
,(20,'Jane','something','something','something')
;
然后使用:-
SELECT *,rowid FROM customers;
然后 :-

- 請注意,rowid 顯示為 customer_id(1),因為它現在將 customer_id 作為別名,并且它與 customer_id 列的值完全匹配。
現在我們使用以下方法在服務表中添加一些行:-
INSERT INTO services (service,subservice,customer_id) VALUES
('Exterior','something',10)
,('Interior','something',10)
,('Interior','something',20)
;
- 請注意 customer_id 如何是來自 customer 表的 customer_id 列的值,以及您如何將每個服務行與一個客戶相關聯。
使用: -
SELECT *,rowid FROM services;
結果是 :-

- 再次 rowid 匹配它的別名但是兩列都只是服務表中行的唯一識別符號它對服務行與其父客戶之間的關系沒有意義(因此為什么 rowid 對你想要的沒有用)。
- the important row, relationship wise, is the customer_id row which specifies the parent.
Similarly for the charges table :-
INSERT INTO charges (initialcharges,taxes,discount,advance,total,customer_id) VALUES
(10.50,0.50,1.5,0,11.50,10),
(105.00,05.00,1.5,0,115,20)
;
SELECT *,rowid FROM charges;

Now say you used-
SELECT customers.*,customers.rowid AS custid,' - ' AS ' ', services.*,services.rowid AS sid,' - ' AS ' ',charges.*,charges.rowid AS cid
FROM customers
JOIN services ON services.customer_id = customers.customer_id
JOIN charges ON charges.customer_id = customers.customer_id
;
then you get :-

If the name of John were changed to Fred using :-
UPDATE customers SET name = 'Fred' WHERE name = 'John';
Then as the John (now Fred) is accessed from the specific row it's change will be seen without any special processing in future queries e.g.
SELECT customers.*,customers.rowid AS custid,' - ' AS ' ', services.*,services.rowid AS sid,' - ' AS ' ',charges.*,charges.rowid AS cid
FROM customers
JOIN services ON services.customer_id = customers.customer_id
JOIN charges ON charges.customer_id = customers.customer_id
;
now results in :-

However, say the id for Jane were changed to 10000 using:-
UPDATE customers SET customer_id = 10000 WHERE customer_id = 20;
Then using the same query results in:-

i.e the ne value (10000) has automatically been applied to the children (not that you would likely change the customer_id often).
NOTE if you updated a child's column (if it did not violate the FK constraint) then that change IS NOT propagated to the parent. The parent would be switched.
洗掉的作業方式類似,洗掉父項,子項將被洗掉。洗掉一個孩子,只有那個孩子是孩子。
因此,對于上述內容,您需要做的就是更新您需要更新的任何內容。
- 請注意,上述內容可能會也可能不會實際反映您想要的內容,而是對原理的演示。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/401092.html
標籤:sqlite 加入 sql更新 python-3.8 行号
