【问题标题】:sqlite3.InterfaceError and query errorsqlite3.InterfaceError 和查询错误
【发布时间】:2017-09-24 15:46:48
【问题描述】:

我在 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)

【问题讨论】:

  • 你可以使用'SELECT 1 FROM accounts WHERE username={}'.format(username)
  • 不想工作,sqlite3.OperationalError: no such column: d Thinks the username is a column :P
  • 您在哪里使用此代码c.execute("SELECT * FROM playlists where userid=?", [userid]) 进行查询?我在代码 sn-p 中没有看到它
  • 什么意思?
  • 我在您粘贴的代码中看不到 SELECT * FROM playlists where userid。代码和数据库中的userid 类型是什么?

标签: python sqlite


【解决方案1】:

这是我这边的:

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())

【讨论】:

  • 谢谢 :) 但是注册有什么问题?
  • 什么意思?
  • 我没有看到任何带有文字 Account with that username already exists! 的消息你能在你的问题中添加更多代码吗?
  • 错误是 sqlite3.ProgrammingError: 提供的绑定数量不正确。当前语句使用 1,提供了 2 个。
  • 更新了帖子,除了注册中的那个没有别的代码了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多