Python:如何检查sql数据库中是否已经存在一个条目?

2024-09-28 20:55:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python新手,甚至是sql新手,但我整个上午都在努力让它工作,我似乎都搞不懂。我有一个名为POSTS(id)的表设置。我试图检查id列中是否已经有一个数字,但是postid是什么并不重要,因为即使postid设置为数据库中没有的数字,数据也不会返回none。

import sqlite3 as lite
import sys

con = lite.connect('post.db')
postid = '52642'

cur = con.cursor()
cur.execute("select id from POSTS where id=?", (postid,))
data = cur.fetchall()
if data is None:
        print ('not found')
else:
        print ('found')

Tags: 数据importid数据库sqldata数字lite