我有遊標與查詢語句如下:如何用Python檢查SQLite中是否存在行?
cursor.execute("select rowid from components where name = ?", (name,))
我要檢查組件的存在:名稱,並返回到Python變量。 我該怎麼做?
我有遊標與查詢語句如下:如何用Python檢查SQLite中是否存在行?
cursor.execute("select rowid from components where name = ?", (name,))
我要檢查組件的存在:名稱,並返回到Python變量。 我該怎麼做?
由於name
s爲獨一無二的,我真的很贊成使用fetchone
或使用SELECT count(*)
在我使用fetchall
的初始建議的亞歷克斯·馬爾泰利的方法,你(的OP的)方法。
fetchall
將結果(通常是多行數據)包裝在列表中。由於name
是唯一的,因此fetchall
返回列表中只有一個元組的列表(例如[(rowid,),]
或空列表[]
。如果您想知道rowid
,則使用fetchall
需要您在列表和元組中挖掘以獲取到 的rowid
。
使用fetchone
在這種情況下更好,因爲你得到的只是一排,(rowid,)
或None
。爲了獲得在rowid
(前提是有一個),你只需要摘掉元組的第一個元素
如果你不關心特定rowid
,你只是想知道有一個命中, 然後你可以使用亞歷克斯Martelli的建議,SELECT count(*)
,這將返回(1,)
或(0,)
。
下面是一些示例代碼:
首先一些鍋爐板代碼設置一個玩具SQLite表:
import sqlite3
connection = sqlite3.connect(':memory:')
cursor=connection.cursor()
cursor.execute('create table components (rowid int,name varchar(50))')
cursor.execute('insert into components values(?,?)', (1,'foo',))
使用fetchall
:
for name in ('bar','foo'):
cursor.execute("SELECT rowid FROM components WHERE name = ?", (name,))
data=cursor.fetchall()
if len(data)==0:
print('There is no component named %s'%name)
else:
print('Component %s found with rowids %s'%(name,','.join(map(str, next(zip(*data))))))
收率:
There is no component named bar
Component foo found with rowids 1
使用fetchone
:
for name in ('bar','foo'):
cursor.execute("SELECT rowid FROM components WHERE name = ?", (name,))
data=cursor.fetchone()
if data is None:
print('There is no component named %s'%name)
else:
print('Component %s found with rowid %s'%(name,data[0]))
收率:
There is no component named bar
Component foo found with rowid 1
使用SELECT count(*)
:
for name in ('bar','foo'):
cursor.execute("SELECT count(*) FROM components WHERE name = ?", (name,))
data=cursor.fetchone()[0]
if data==0:
print('There is no component named %s'%name)
else:
print('Component %s found in %s row(s)'%(name,data))
收率:
There is no component named bar
Component foo found in 1 row(s)
嗨unutbu,你可以更清晰的Python語句後,分配給數據?我是Python – 2010-03-14 19:26:05
的新手,但是,'create table components'中的'name'字段具有UNIQUE屬性,這也很重要。感謝分享代碼。 – 2010-03-15 10:22:10
這是否也適用於python3? – 2018-01-08 03:19:58
我找到了答案。
exist = cursor.fetchone()
if exist is None:
... # does not exist
else:
... # exists
我得到這個首先做'cursor.execute工作(「SELECT * FROM MYTABLE」)' – nobism 2017-09-22 04:04:31
由於兩個現有的回答(你自己和@ unutbu的)所指出的,關鍵是你需要做一些排序取的,在執行SELECT
,後檢查是否已經有任何結果到選擇與否(無論你是通過單個獲取並檢查沒有,或檢查所有並檢查一個空列表,這是一個邊際區別 - 考慮到你提到了約束條件,它們基本上是等價的方法)。
對於一個非常直接的答案,你可以select count(*) from components where name = ?
,而不是選擇rowid
,如果你只關心的是給定的名稱值是否存在(而不是關心它是什麼行ID,如果有的話;-)。執行此選擇,以及獲取的結果,爲您提供了0
如果該值不存在,1
如果它的存在(沒有其他的結果是可能給你繞UNIQUE
約束上name
列;-)的評論中提及。
喜亞歷克斯,其實,我要檢查它是否存在,並得到rowid的爲好。所以,這就是爲什麼我把rowid放在select語句中的原因。你的解決方案工作得很好,但如果它存在,我必須做另一次選擇才能得到結果。您能否舉一個例子來檢查一下我可以做的一些代碼,以便進行一些比較? (取一個與檢查無VS取回所有與檢查空)對不起,我是新來的Python和sqlite3的 – 2010-03-14 19:24:40
@fx,如果你需要的rowid,然後在你的自我的答案代碼是好的,基本上等同於unutbu的(用測試'if data:'檢查非空列表) - 不需要進行任何深度比較。 – 2010-03-14 19:56:07
爲了使它更短...:
row = cursor.fetchone()
if row:
print "row is present"
else:
print "row is not present"
你想檢查它是否是空或者列在架構存在的話? – AndiDog 2010-03-13 21:49:43
不,列名是UNIQUE,所以我想這樣選擇這個名字的組件,如果它存在與否。對不起,我錯過了一些細節 – 2010-03-13 21:55:49
如果你找到了解決方案,你應該選擇一個答案來標記這個問題爲答案。最好別人的回答指出你是正確的。 – 2010-03-14 11:51:57