2016-08-14 28 views
2

我是python,kivy和sqlite的新手。但我必須做這個艱鉅的任務。 :-(任何形式的幫助將不勝感激在此先感謝Python + kivy + SQLite:如何一起使用它們

的任務是:!顯示kivy屏幕在Android上從.db文件中的數據

我從http://zetcode.com/db/sqlitepythontutorial/國產數據庫文件

在這裏,我再次發佈的代碼。

#!/usr/bin/python 
# -*- coding: utf-8 -*- 

import sqlite3 as lite 
import sys 

con = lite.connect('test.db') 

with con: 

    cur = con.cursor()  
    cur.execute("CREATE TABLE Cars(Id INT, Name TEXT, Price INT)") 
    cur.execute("INSERT INTO Cars VALUES(1,'Audi',52642)") 
    cur.execute("INSERT INTO Cars VALUES(2,'Mercedes',57127)") 
    cur.execute("INSERT INTO Cars VALUES(3,'Skoda',9000)") 
    cur.execute("INSERT INTO Cars VALUES(4,'Volvo',29000)") 
    cur.execute("INSERT INTO Cars VALUES(5,'Bentley',350000)") 
    cur.execute("INSERT INTO Cars VALUES(6,'Citroen',21000)") 
    cur.execute("INSERT INTO Cars VALUES(7,'Hummer',41400)") 
    cur.execute("INSERT INTO Cars VALUES(8,'Volkswagen',21600)") 

則數據庫會保存到C:\\test.db

然後我用labelbutton作了kivy屏幕。

# -*- coding: utf-8 -*- 

from kivy.app import App 
from kivy.uix.boxlayout import BoxLayout 
from kivy.uix.label import Label 
from kivy.lang import Builder 

import random 

root_widget = Builder.load_string(''' 
BoxLayout: 
    orientation: 'vertical' 
    Label: 
     text: 'Hello' #How to define it? 
     font_size: 30 
    Button: 
     size: root.width/2, 15 
     text: 'next random' 
     font_size: 30 
#  on_release: #How to define it? 
''') 

class TestApp(App): 
    def build(self): 
     return root_widget 

if __name__ == '__main__': 
    TestApp().run() 

我要的是點擊button當每次從上label所示的db file,有random車名

最初的label文字現在是「你好」字樣,應該用一個隨機車名取代。所以每次程序運行時,汽車名稱都會顯示在label上。

但我真的不知道如何編寫代碼。

謝謝你的幫助。

#UPDATE

我寫的代碼,但它不工作。

# -*- coding: utf-8 -*- 

from kivy.app import App 
from kivy.uix.boxlayout import BoxLayout 
from kivy.uix.label import Label 
from kivy.lang import Builder 
from kivy.clock import mainthread 
import sqlite3 

import random 


class MyBoxLayout(BoxLayout): 

    def init(self, **kwargs): 
     super().__init__(**kwargs) 

     @mainthread # execute within next frame 
     def delayed(): 
      self.load_random_car() 
     delayed() 

    def load_random_car(self): 
     conn = sqlite3.connect("C:\\test.db") 
     cur = conn.cursor() 

     ####Length of db file 
     with conn: 
      cur = conn.cursor() 
      cur.execute("SELECT * FROM Cars") 
      rows = cur.fetchall() 
     LengthSQLFile = len(rows) 
     print LengthSQLFile 


     CurrentNo = random.randint(0, LengthSQLFile) 
     CurrentNo_ForSearch = (CurrentNo ,) 
     cur.execute("select * from Cars where rowid = ?" , CurrentNo_ForSearch) 

     CurrentAll = cur.fetchone() 
     CurrentAll = list(CurrentAll) # Change it from tuple to list 
     print CurrentAll 

     Current = CurrentAll[1] 
     self.ids.label.text = Current #"fetch random car data from db and put here" 


root_widget = Builder.load_string(''' 
BoxLayout: 
    orientation: 'vertical' 
    Label: 
     id: label 
     font_size: 30 
    Button: 
     size: root.width/2, 15 
     text: 'next random' 
     font_size: 30 
     on_release: root.load_random_car() 

''') 


class TestApp(App): 
    def build(self): 
#  MyBoxLayout(BoxLayout) 
     return root_widget 

if __name__ == '__main__': 
    TestApp().run() 

#2更新:

現在的代碼是......它顯示錯誤消息:AttributeError: 'BoxLayout' object has no attribute 'load_random_car'

# -*- coding: utf-8 -*- 

from kivy.app import App 
from kivy.uix.boxlayout import BoxLayout 
from kivy.uix.label import Label 
from kivy.lang import Builder 
from kivy.clock import mainthread 
import sqlite3 

import random 

class MyBoxLayout(BoxLayout): 

    def init(self, **kwargs): 
     super().__init__(**kwargs) 

     @mainthread # execute within next frame 
     def delayed(): 
      self.load_random_car() 
     delayed() 

    def load_random_car(self): 
     conn = sqlite3.connect("C:\\test.db") 
     cur = conn.cursor() 

     cur.execute("SELECT * FROM Cars ORDER BY RANDOM() LIMIT 1;") 

     currentAll = cur.fetchone() 
     currentAll = list(currentAll) # Change it from tuple to list 
     print currentAll 

     current = currentAll[1] 
     self.ids.label.text = current #"fetch random car data from db and put here" 


root_widget = Builder.load_string(''' 
BoxLayout: 
    orientation: 'vertical' 
    Label: 
     id: label 
     font_size: 30 
    Button: 
     size: root.width/2, 15 
     text: 'next random' 
     font_size: 30 
     on_release: root.load_random_car() 

''') 


class TestApp(App): 
    def build(self): 
#  MyBoxLayout(BoxLayout) 
     return root_widget 

if __name__ == '__main__': 
    TestApp().run() 
+1

什麼是行不通的?你爲什麼要查詢兩次?一旦你用第一個查詢獲取數據,就用'random.choice(rows)'選擇一個隨機條目。另外,請不要使用大寫字母來啓動變量名 - 它們是爲類名保留的。 – jligeza

+0

@jligeza感謝您的提示。我改變了它。但它仍然顯示錯誤消息:'BoxLayout'對象沒有屬性'load_random_car' – Rita

+0

@jligeza我粘貼了更新。你可以看看嗎?謝謝。 – Rita

回答

2

最簡單的方法將被寫入了一個自定義的init方法盒佈局:

from kivy.uix.boxlayout import BoxLayout 
from kivy.clock import mainthread 

class MyBoxLayout(BoxLayout): 

    def init(self, **kwargs): 
     super().__init__(**kwargs) 

     @mainthread # execute within next frame 
     def delayed(): 
      self.load_random_car() 
     delayed() 

    def load_random_car(self): 
     self.ids.label.text = "fetch random car data from db and put here" 

這是如何更新小部件結構看起來像:

MyBoxLayout: 
    Label: 
     id: label 
    Button: 
     on_release: root.load_random_car() 
+0

非常感謝!但是我的代碼仍然存在問題。你能爲我寫一個框架代碼,告訴我如何加載db文件,如何獲取db數據等等?這對我來說是一個很大的幫助。謝謝。 – Rita

+1

@Rita加載數據庫文件並從數據中提取數據在你已經鏈接的教程中寫得很清楚。你不明白哪部分教程? – jligeza

+0

謝謝。我實際上是在尋找'class'語句來加載和讀取db文件。但我無法在互聯網上找到這樣的說法。 – Rita

相關問題