我正在尝试将数据添加到 sqlite3 数据库,以便动态地从 RecycleView 中读取、添加和删除它们,但我无法在将数据上传到数据库后自动加载它们。
这是我的代码的一部分,我需要修改它才能工作。非常感谢。
producto=[]
def cargarproductosbase():
global producto
con = sqlite3.connect("base.db")
cur = con.cursor()
cur.execute("select nombre from producto")
print("se cargaron los productos")
rows = cur.fetchall()
for row in rows:
for i in row:
producto.append(i)
print(producto)
con.commit()
con.close()
print ("cargue estos datos")
print (producto)
class SelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior,
RecycleBoxLayout):
''' Adds selection and focus behaviour to the view. '''
class SelectableLabel(RecycleDataViewBehavior, Label):
''' Add selection support to the Label '''
index = None
selected = BooleanProperty(False)
selectable = BooleanProperty(True)
cols=2
def refresh_view_attrs(self, rv, index, data):
''' Catch and handle the view changes '''
self.index = index
return super(SelectableLabel, self).refresh_view_attrs(
rv, index, data)
def on_touch_down(self, touch):
''' Add selection on touch down '''
if super(SelectableLabel, self).on_touch_down(touch):
return True
if self.collide_point(*touch.pos) and self.selectable:
return self.parent.select_with_touch(self.index, touch)
def apply_selection(self, rv, index, is_selected):
''' Respond to the selection of items in the view. '''
self.selected = is_selected
if is_selected:
aceptar=AceptarDonacion()
aceptar.open()
print("selection changed to {0}".format(rv.data[index]))
else:
print("selection removed for {0}".format(rv.data[index]))
class ExampleRV(RecycleView):
global producto
cargarproductosbase()
def __init__(self, **kwargs):
global producto
super(ExampleRV, self).__init__(**kwargs)
Clock.schedule_interval(self.reloading, 1)
def reloading(self, nothing):
global producto
self.data = [{'text': str(x)} for x in producto]
print (producto)
在类 ExampleRV
的构造函数中实例化类 cargarproductosbase()
。
片段
class ExampleRV(RecycleView):
def __init__(self, **kwargs):
global producto
super(ExampleRV, self).__init__(**kwargs)
cargarproductosbase()
self.data = [{'text': str(x)} for x in producto]
print(producto)
输出
我是一名优秀的程序员,十分优秀!