【问题标题】:How to match a string with a value in a database如何将字符串与数据库中的值匹配
【发布时间】:2019-03-14 20:18:57
【问题描述】:

我认为价值是正确的词。如果不是,请编辑。

内容:

问题 1(字符串未永久保留在数据库中)

问题 2(问题 1 的想法)

我正在创建一个程序,该程序使用 Python3 中的 Sqlite3 将字符串添加到数据库中的表中。我正在使用一个要求您输入密码的功能。稍后,如果输入的字符串等于数据库中的某个值,则想调用该函数。(如果它不等于数据库中的任何值,则将其插入密码表中。)

问题一:

问题是当我停止运行程序并再次运行它时,字符串不会留在数据库中,这导致它允许我重新输入以前的密码。我想要程序做的是让字符串在停止后留在数据库中。
这是上面段落的程序:(SCROLL DOWN for问题 1 )

的想法
import sqlite3
import hashlib

db  = sqlite3.connect( "users.db" )
cur = db.cursor()
cur.execute( "CREATE TABLE IF NOT EXISTS passwords( pwd TEXT, UNIQUE( pwd ))" )

def password():
  pwd = input( "password: " )
  sha = hashlib.sha256( pwd.encode( 'utf-8' )).hexdigest()
  cur.execute( "INSERT INTO passwords VALUES( ? )", ( sha, ))

while True:
  try:
    password()
    #break
  except KeyboardInterrupt:
    print( "aborted" )
    break
  except sqlite3.IntegrityError:
    print( "cannot reuse that password" )
db.commit()
db.close()

================================================ ======================== 问题 2:(问题 1 的想法)

这是一个更新的版本。我在这里所做的是尝试将字符串添加到数据库的表中,如果它与任何字符串匹配或不匹配。我在这里遇到的错误是 pwd 不是第 13 行的变量,尽管我确实将它作为一个变量并将其设置为全局变量。如果想帮助解决这个问题,我想知道为什么 pwd 不是变量以及如何使其成为变量。

import sqlite3
import hashlib

db  = sqlite3.connect( "used_passwords.db" )
cur = db.cursor()
cur.execute( "CREATE TABLE IF NOT EXISTS passwords( pwd TEXT, UNIQUE( pwd ))" )

def password():
  global pwd
  pwd = input( "password: " ) #turn this into a global variable
  sha = hashlib.sha256( pwd.encode( 'utf-8' )).hexdigest()
  cur.execute( "INSERT INTO passwords VALUES( ? )", ( sha, ))

while True:
  #take pwd from password and put it here
  sha = hashlib.sha256( pwd.encode( 'utf-8' )).hexdigest()
  try:
    password()
    #break
  except KeyboardInterrupt:
    print( "aborted" )
    break
  except sqlite3.IntegrityError:
    print( "cannot reuse that password" )
    cur.execute( "INSERT INTO passwords VALUES( ? )", ( sha, ))
db.commit()
db.close()

【问题讨论】:

    标签: python database python-3.x sqlite passwords


    【解决方案1】:

    对于问题 1,将您的 db.commit() 移动到循环中,或者将您的 try-except 移动到 else 中,或者直接移动到 password() 函数中。

    try:
      password()
    except KeyboardInterrupt:
      print( "aborted" )
      break
    except sqlite3.IntegrityError:
      print( "cannot reuse that password" )
    else:
      db.commit()
    

    def password():
      pwd = input( "password: " )
      sha = hashlib.sha256( pwd.encode( 'utf-8' )).hexdigest()
      cur.execute( "INSERT INTO passwords VALUES( ? )", ( sha, ))
      db.commit()
    

    在插入成功后单独提交插入,否则您可能会在未处理的错误中丢失所有插入。除了未提交的插入之外,我没有看到您的密码“不留在数据库中”的任何其他原因。

    至于问题2:当程序进入循环时,password()还没有被调用,因此当你尝试使用它时pwd还不存在。

    while True:
      #take pwd from password and put it here
      sha = hashlib.sha256( pwd.encode( 'utf-8' )).hexdigest()  # <-- pwd is undefined here ...
    try:
        password()  # ... because it needs this to be executed at least once
    

    为什么还要在循环中第二次使用hashlib.sha256?你已经在password 中做到了;您可以从循环中删除该行并立即摆脱NameError。此外,循环的 except 块中的第二个 INSERT 没有意义。如果 INSERT 违反了 UNIQUE 约束并引发了IntegrityError,您会再次尝试同样的 INSERT 吗?这将引发相同的错误,这一次未处理,将使您的程序崩溃。

    坚持你的第一种方法,它会好很多。不要使用全局变量,除非你真的真的 REALLY必须这样做。

    【讨论】:

      猜你喜欢
      • 2012-11-25
      • 2013-06-08
      • 2021-04-28
      • 2023-02-14
      • 1970-01-01
      • 2011-07-22
      • 2014-10-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多