Python和SQLite:检查数据库中是否存在某个项目? [英] Python and SQLite: Check if an item exists in a database?

查看:1875
本文介绍了Python和SQLite:检查数据库中是否存在某个项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种方法可以将希望注册的用户的用户名和密码写入数据库。在存储他们提供给数据库的用户名和密码之前,我想检查他们选择的用户名是否已存在于待处理列表或已批准的联系人列表中。

I have a method which writes into a database the username and password of user who wish to register. Before storing the username and password they have provided into the database, i want to check if the username they have chosen already exist in either the "pending" list, or the approved "contacts" list.

以下是我以前使用的代码:

Here is the code I used to do that:

@cherrypy.expose
def writePending(self, username=None, password=None, message=None, signin=None):
    """ Add request of friendship into a database which stores all
        pending friendships.
    """

    page = get_file(staticfolder + "/html/friendingPage.html")

    if username != "" and password != "" and message !="":
        con = lite.connect('static/database/Friendship.db')
        cur = con.cursor()

        with con:      
            cur.execute("CREATE TABLE IF NOT EXISTS pending(user TEXT, pass TEXT, info TEXT)")
            cur.execute("CREATE TABLE IF NOT EXISTS contacts(user TEXT, pass TEXT)")

            "Check to see if the username is already registered"

            cur.execute("Select * from pending where user = ?", (username, ))
            check1=cur.fetchone()
            cur.execute("Select * from contacts where user = ?", (username, ))
            check2=cur.fetchone()

            if check1[0] != None:
                page = page.replace("$Error", "The ID you used is still pending for friendship")
            elif check2[0] != None:
                page = page.replace("$Error", "The ID you used is already added as a contact")
            else:
                cur.execute("CREATE TABLE IF NOT EXISTS pending(user TEXT, pass TEXT, info TEXT)")   
                cur.execute("INSERT INTO pending VALUES(?, ?, ?)", (username, password, message))               
                page = get_file(staticfolder + "/html/thankYouPage.html")

    else:
        page = get_file(staticfolder + "/html/friendingPage.html")
        page = page.replace("$Error", "You Must fill out all fields to proceed")

    return page

但是,我会收到一条消息

However, I would get a message that

Traceback (most recent call last):
  File "/usr/lib/pymodules/python2.7/cherrypy/_cprequest.py", line 606, in respond
    cherrypy.response.body = self.handler()
  File "/usr/lib/pymodules/python2.7/cherrypy/_cpdispatch.py", line 25, in __call__
    return self.callable(*self.args, **self.kwargs)
  File "proj1base.py", line 540, in writePending
    if type(check1[0]) != None:
TypeError: 'NoneType' object is not subscriptable

我想知道我能做些什么来避免这种情况?

I am wondering what I can do to avoid that?

谢谢。

推荐答案

在您的示例中, check1 将是,因此您无法使用 [0] 。您可以这样做:

In your example, check1 will be None, so you can't use [0] on it. You could do something like this:

if check1 is not None:
    (error response)

或者只使用 cur.rowcount 而不是 cur.fetchone()

if cur.rowcount > 0:
    (error response)

这篇关于Python和SQLite:检查数据库中是否存在某个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆