使用SQLite更新数据库

时间:2013-12-11 15:07:35

标签: python sqlite

我试图通过让用户在该记录中选择他们想要编辑的内容来编辑我的数据库中的记录。

我收到以下错误

UnboundLocalError: local variable 'Type' referenced before assignment

我的代码如下:

import sqlite3
def update_data(values):
    with sqlite3.connect("mrsimms.db")as db:
        cursor = db.cursor()
        sql = "Update Stock set Type=?,RetailPrice=?, NumberInStock=? where Name=?"
        cursor.execute(sql,values)
        db.commit()

def update():
  Update = "y"
  while Update == "y":
      Name = input("Please enter the name of the product you wish to update: ")
      Type_change=input('would you like to edit the type?(y/n) ')
      if Type_change == 'y':
          Type=input("Please enter the updated type: ")

      RetailPrice_change=input('would you like to edit the retail price?(y/n) ')
      if RetailPrice_change=='y':
          RetailPrice = input("Please enter the updated retail price: ")

      NumberInStock_change=input('would you like to edit the number in stock?(y/n) ')
      if NumberInStock_change=='y':
          NumberInStock = input("Please enter the updated number in stock: ")

      data = (Type,RetailPrice,NumberInStock)       
      update_data(data)
      Update = input("Do you wish to update another lesson? (y/n) ")
      print()
update()
谢谢你:)

1 个答案:

答案 0 :(得分:0)

你有这段代码。

if Type_change == 'y':
    Type=input("Please enter the updated type: ")

以后没有elseType未分配。

所以这里

data = (Type,RetailPrice,NumberInStock)       

Type如果if条件不正确,则不存在。

else之外添加Type子句或初始化if