2017-07-26 9 views
-2

我用一個名爲product的表做了一個數據庫。當我運行下面的代碼時,出現錯誤 - sqlite3.OperationalError:no such table:product。我已經使用數據庫瀏覽器檢查並確定表存在。有任何想法嗎?代碼和文件都在同一個文件夾中。由於sqlite python數據庫錯誤 - 沒有這樣的表

from tkinter import * 
from tkinter import ttk 
import sqlite3 
import os.path 

class Product: 
    db_name = 'database.db' 

    def __init__(self, wind): 
     self.wind = wind 
     self.wind.title('IT Products') 

     frame = LabelFrame (self.wind, text = 'Add new record') 
     frame.grid (row = 0, column = 1) 

     Label (frame, text = 'Name: ').grid (row = 1, column = 1) 
     self.name = Entry (frame) 
     self.name.grid(row = 1, column = 2) 

     Label (frame, text = 'Price: ').grid (row = 2, column = 1) 
     self.price = Entry (frame) 
     self.price.grid(row = 2, column = 2) 

     ttk.Button (frame, text= 'Add record').grid (row = 3, column =2) 
     self.message = Label (text = '',fg = 'red') 
     self.message.grid (row = 3, column = 0) 

     self.tree = ttk.Treeview (height = 10, colum =2) 
     self.tree.grid(row = 4, column = 0, columnspan = 2) 
     self.tree.heading('#0', text = 'Name', anchor = W) 
     self.tree.heading(2, text = 'Price', anchor = W) 

     ttk.Button (text = 'Delete record').grid (row = 5, column = 0) 
     ttk.Button (text = 'Edit record').grid (row = 5, column = 1) 

     self.viewing_records() 



    def run_query (self, query, parameters =()): # database connection 
     with sqlite3.connect(self.db_name) as conn: 
      cursor = conn.cursor() 
      query_result = cursor.execute (query, parameters) 
      conn.commit() 
     return query_result 


    def viewing_records(self): 
     records = self.tree.get_children() 
     for element in records: 
      self.tree.delete (element) 
     query = 'SELECT * FROM product ORDER BY name DESC' 
     db_rows = self.run_query (query) 
     for row in db_rows: 
      self.tree.insert ('', 0, text = row[1], values = row[2]) 

if __name__ == '__main__': 
    wind = Tk() 
    application = Product (wind) 
    wind.mainloop() 

回答

1

這將創建一個名爲db_path變量,它包含文件database.db

db_path = os.path.join(BASE_DIR, "database.db") 

的路徑,但這裏使用了文字字符串db_path作爲數據庫的名稱來連接,的剛剛創建的變量的內容:

with sqlite3.connect('db_path') as conn: 

要使用變量,去掉引號:

with sqlite3.connect(db_path) as conn: 
+0

忘記採取這些了!但仍然沒有運氣 – maz86

+0

只被添加,以確保它檢查正確的數據庫取出它,但沒有luckBASE_DIR = os.path.dirname(os.path.abspath(__ file__)) db_path = os.path.join(BASE_DIR,「 database.db「) – maz86

+1

@ maz86如果您修改了代碼並仍然出現錯誤(相同錯誤或其他錯誤),請使用您擁有的最新代碼和錯誤更新*問題。 – DeepSpace

3
... 
db_path = os.path.join(BASE_DIR, "database.db") 
with sqlite3.connect('db_path') as conn: 
... 

你正在創建一個名爲'db_path'一個新的數據庫文件。

而不是

with sqlite3.connect('db_path') as conn:

你應該有

with sqlite3.connect(db_path) as conn:

或者換句話說,使用變量db_path而不是文本字符串'db_path'

爲了完整起見,你也可能要使用Product.db_name您已經定義的,而不是再硬編碼'database.db'

... 
db_path = os.path.join(BASE_DIR, self.db_name) 
with sqlite3.connect(db_path) as conn: 
... 
+0

更新了代碼並刪除了該位。仍然得到了同樣的問題:(感謝您的幫助 – maz86

+0

@ maz86密切關注我的答案底部的代碼。您仍然將錯誤的值傳遞給'sqlite3.connect' – DeepSpace

+0

只是嘗試和相同的錯誤BASE_DIR = os .path.dirname(os.path.abspath(__ file__)) db_path = os.path.join(BASE_DIR,self.db_name) with sqlite3.connect(db_path)as conn: – maz86

相關問題