Python 程序中的数据库锁?
程序中有一个数据库锁,如何修复 插入产品位工作正常,只是类别位告诉我数据库已锁定且不工作:Python 程序中的数据库锁?,python,sql,database,sqlite,locking,Python,Sql,Database,Sqlite,Locking,程序中有一个数据库锁,如何修复 插入产品位工作正常,只是类别位告诉我数据库已锁定且不工作: import sqlite3 def insert_product(product): with sqlite3.connect("main.db") as db: cursor = db.cursor() sql = "insert into Products (CatID,Name,Qty,Price) values (?, ?, ?, ?)"
import sqlite3
def insert_product(product):
with sqlite3.connect("main.db") as db:
cursor = db.cursor()
sql = "insert into Products (CatID,Name,Qty,Price) values (?, ?, ?, ?)"
cursor.execute(sql, product)
db.commit()
print("The product {0} has been inserted successfully" .format(Name))
cursor = db.execute("SELECT ProductID, CatId, Name, Qty, Price from Products")
for row in cursor:
print ("ID = ", row[0])
print ("CatID = ", row[1])
print ("NAME = ", row[2])
print ("Qty = ", row[3])
print ("Price = ", row[4], "\n")
def insert_category(category):
with sqlite3.connect("main.db") as db:
cursor = db.cursor()
sql = "insert into categories (Name) values (?)"
cursor.execute(sql, category)
db.commit()
print("The category {0} has been inserted successfully" .format(Name))
cursor = db.execute("SELECT CatID,Name from categories")
for row in cursor:
print ("CatID = ", row[0])
print ("Name = ", row[1], "\n")
if __name__ == "__main__":
response = input("Do you want to insert a category or a product (c/p) ?")
if response == "p":
ID= input("enter the CatID of the product:>> ")
Name = input("Enter the name of the Product you want to insert: >>")
Qty = input("Enter the quantity of the stock available: >>")
Price = input("Enter the price of the product: >>")
product = (ID,Name,Qty,Price)
insert_product(product)
print ("Product inserted successfully")
else:
Name = input("Enter the name of the Category you want to insert: >>")
category = (Name,)
insert_category(category)
db.commit()
print ("The new category {0} has been inserted successfully" .format(Name))
我遇到的问题是:
cursor.execute(sql, category)
sqlite3.OperationalError: database is locked
您好,Saqib,只有在您先保存insert product,然后尝试插入类别时才会发生这种情况吗?或者当您尝试先插入类别时也会发生这种情况?Category的CatID应该是自动生成的,并保存在db?@ChetanRanpariya中。ID是自动生成的,但现在在插入产品时也会发生。它只是不允许我使用这个代码在数据库表中插入任何内容,它说数据库被锁定,以停止程序的运行。之后,请尝试在打印后关闭连接:db.close()@PeterRing如果您可以共享一些代码,我不知道您的确切意思。谢谢在“for row in cursor”中打印所有内容后:“使用命令“db.close()”关闭与db的连接Hi Saqib,仅当您先保存插入产品,然后尝试插入类别时,才会发生这种情况吗?或者当您尝试先插入类别时也会发生这种情况?Category的CatID应该是自动生成的,并保存在db?@ChetanRanpariya中。ID是自动生成的,但现在在插入产品时也会发生。它只是不允许我使用这个代码在数据库表中插入任何内容,它说数据库被锁定,以停止程序的运行。之后,请尝试在打印后关闭连接:db.close()@PeterRing如果您可以共享一些代码,我不知道您的确切意思。谢谢在“for row in cursor”中打印完所有内容后:“使用命令“db.close()”关闭与数据库的连接”