MySQLdb - 检查行是否存在 Python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31692339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQLdb - Check if row exists Python
提问by spenf10
I am trying to check if a row exist with the same Name my database with python and can't quite get it here is what I am trying: (I know the connection is wokring)
我正在尝试使用 python 检查是否存在与我的数据库同名的行,但在这里无法完全得到它,这就是我正在尝试的:(我知道连接是 wokring)
try:
cursor.execute("SELECT Name, COUNT(*) FROM Item_Info WHERE Name = %s GROUP BY Name"), (item_name)
catch:
print "it does not exist"
Can someone help me out here
有人可以帮我吗
Thanks
谢谢
采纳答案by Digvijayad
First of all you have a wrong syntax in your code. Python doesn't have a
try...catch
block. It hastry...except
block which is used like this:try: #something here except: #something here
MySQL does not return an error when you use
SELECT
command. However there are two different ways you can find out if it returned something or not.
首先,您的代码中有错误的语法。Python 没有
try...catch
块。它有try...except
这样使用的块:try: #something here except: #something here
使用
SELECT
命令时,MySQL 不会返回错误。但是,您可以通过两种不同的方式确定它是否返回了某些东西。
PYTHON 2.7
蟒蛇 2.7
cursor.execute(
"SELECT Name, COUNT(*) FROM Item_Info WHERE Name = %s GROUP BY Name",
(item_name,)
)
# gets the number of rows affected by the command executed
row_count = cursor.rowcount
print "number of affected rows: {}".format(row_count)
if row_count == 0:
print "It Does Not Exist"
PYTHON 3+
蟒蛇 3+
cursor.execute(
"SELECT Name, COUNT(*) FROM Item_Info WHERE Name = %s GROUP BY Name",
(item_name,)
)
# gets the number of rows affected by the command executed
row_count = cursor.rowcount
print ("number of affected rows: {}".format(row_count))
if row_count == 0:
print ("It Does Not Exist")
Another way to do this would be to fetch the statement and check if it is empty:
另一种方法是获取语句并检查它是否为空:
#execute statement same as above
msg = cursor.fetchone()
# check if it is empty and print error
if not msg:
print 'It does not exist'
This is my first answer, so I don't know how to style the code in the answer properly, it also seems messy because of that. Sorry for that.
这是我的第一个答案,所以我不知道如何正确设置答案中的代码样式,因此看起来也很混乱。对不起。
Also i use Python 3 and pymysql, so there may be some syntax error but I have tried to write the code according to python 2.7 from what I could remember about it.
此外,我使用 Python 3 和 pymysql,因此可能存在一些语法错误,但我已尝试根据我所记得的 Python 2.7 编写代码。
EDIT(5/1/2020)
编辑(5/1/2020)
Thanks to @Arishta for pointing out that the first method will require you to fetch all rows before using row_count. i.e adding cursor.fetchall()
before the row_count = cursor.rowcount
感谢@Arishta 指出第一种方法将要求您在使用 row_count 之前获取所有行。即cursor.fetchall()
在之前添加row_count = cursor.rowcount
cursor.execute(
"SELECT Name, COUNT(*) FROM Item_Info WHERE Name = %s GROUP BY Name",
(item_name,)
)
# Add THIS LINE
results = cursor.fetchall()
# gets the number of rows affected by the command executed
row_count = cursor.rowcount
print ("number of affected rows: {}".format(row_count))
if row_count == 0:
print ("It Does Not Exist")
Use the cursor.fetchone()
if you only care if the record exists or not.
使用cursor.fetchone()
,如果你仅当记录存在或不关心。
回答by Laxman Gupta
cursor.execute("SELECT * FROM userinfo WHERE User_Name=%s",(userid,))
data="error" #initially just assign the value
for i in cursor:
data=i #if cursor has no data then loop will not run and value of data will be 'error'
if data=="error":
print("User Does not exist")
else:
print("User exist")