sqlite3.InterfaceError和查询错误

时间:2017-09-24 15:46:49

标签: python sqlite

我在python中使用SQLITE3,我有几个错误。

第一个:

  

sqlite3.ProgrammingError:提供的绑定数量不正确。当前语句使用1,并且提供了2个。

第二个: 这个问题并不是一个错误,但是它不起作用。

username= input("Username: ").lower()
password= input("Password: ")


#Get username and see if it exists
c.execute('SELECT id FROM accounts WHERE username=?', (username))


print("test")
#Insert values into the account table in the database.
c.execute("INSERT INTO accounts VALUES(NULL,?,?)", (username,password))

conn.commit()

#Grab ID
ids = c.execute("SELECT id FROM accounts WHERE username=?", (username,))

#Insert defaults into playlists
c.execute("INSERT INTO playlists VALUES(NULL,?,'NULL')", (ids))

#Save the changes.
conn.commit()

它只是说"用户名已经存在!"唯一的帐户是

用户名:s 密码:s

我已经在StackOverflow上四处寻找,但找不到任何有助于我的事业的事情,因此我发布了这个原因。 编辑:

   def loggedIn(userid):
        def viewPlaylist(userid):
            print(userid + 1)
            c.execute("SELECT 1 FROM playlists where userid=?", (userid))

            if(c.fetchone): 
                print(c.fetchone)
            else:
                printError("ERROR: No Data found, with that UserID\nCreating Data...")
                c.execute("INSERT INTO playlists VALUES(NULL,?,NULL)", (userid))
                printError("Finished!\nYou may now edit your playlist.")
                loggedIn(userid)

1 个答案:

答案 0 :(得分:0)

这在我身边:

def logged_in(user_name, password):
    # Here you should add conn and cursor, or paste in parameter
    c.execute("SELECT id FROM accounts where username=? and password=?", (user_name, password)))
    user = c.fetchone()
    if user:
        print('logged in')
    else:
        print('user not found')

或者以你的方式,你应该这样做:

loggedIn(c.execute("SELECT id FROM accounts where username=?",(username)).fetchone())