.execute(“ INSERT给出OperationalError:无法打开数据库文件

时间:2019-04-06 10:51:20

标签: python sql sqlite

因此,我正在创建密码管理器,由于某种原因,每当我使用类时,使用SQL时都无法向数据库中输入任何数据。 (注意:使用tkinter时,我仍然可以使用SQL将代码输入到数据库中)

我已经以与其余代码相同的方式连接到数据库

class dataentry():

 def __init__(self,master):

      conn = sqlite3.connect('passworddatabase.db')
      cursor = conn.cursor()
      cursor.execute('CREATE TABLE IF NOT EXISTS passwords(username STRING,password STRING,portal_name STRING)') #creates tables to be used, incase it does not exist

      self.username=StringVar() #creates tkinter variable, and validates contents of what will be entered
      self.password=StringVar() #""
      self.portal_name=StringVar() #""

      self.master=master
      self.master.geometry('400x200+100+200') #geometry
      self.master.title('Password Manager') #window title

      self.label2=Label(self.master,text='Add New Details', fg='white', bg='black').grid(row=0,column=0)
      self.label2=Label(self.master,text='Username',fg='black').grid(row=3,column=0)
      self.label2=Label(self.master,text='Password',fg='black').grid(row=4,column=0)
      self.label2=Label(self.master,text='Application Name',fg='black').grid(row=5,column=0)

      self.username=Entry(self.master,textvariable=self.username).grid(row=3,column=1)
      self.password=Entry(self.master,textvariable=self.password,show="*").grid(row=4,column=1) #hides password enteries
      self.portal_name=Entry(self.master,textvariable=self.portal_name).grid(row=5,column=1)
      self.button4=Button(self.master,text="Add Details",fg='white', bg='black', command=self.dynamic_data_entry).grid(row=10,column=1) #add's field enteries to database
      self.button5=Button(self.master,text="Return to Home Page",fg='white', bg='black',command=self.exit).grid(row=12,column=1) #exit button

上面的代码是给下面的子例程(负责添加数据)一些上下文

 def dynamic_data_entry(self):
      global dynamic_data_entry #access within the entire program
    #this is what adds the data to the database
      global conn, cursor
      conn = sqlite3.connect('passworddatabase.db')
      c = conn.cursor()
      username1 = self.username #assigning variables
      password1 = self.password #""
      portal_name1 = self.portal_name #
      c.execute("INSERT INTO passwords(username, password, portal_name) VALUES (?, ?, ?)",(username1, password1, portal_name1)) #enters data into the table
      conn.commit()
      self.writetodatabase() 

 def writetodatabase(self):
      for i in range(1):
           time.sleep(1)
      c.close()
      conn.close()     

我希望将数据添加到数据库表“密码”中,而不是出现错误,值得注意的两个是数据库是“只读”

  

更新:回溯不再报告只读数据库问题,而是报告以下内容:

Traceback (most recent call last):
  File "C:\Users\owner\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1702, in __call__
    return self.func(*args)
  File "C:\Users\owner\Documents\A-Levels\Subjects\Comp.science\nea\code\loginnew.py", line 80, in dynamic_data_entry
    cursor.execute("INSERT INTO passwords(username, password, portal_name) VALUES (?, ?, ?)",(username1, password1, portal_name1)) #enters data into the table
sqlite3.OperationalError: unable to open database file

0 个答案:

没有答案