Data loss using Sqlite with Serious Python and Flet in Android Studio #2193
-
QuestionI made a very simple example to test Sqlite with Flet and Serious Python in Android Studio. When running the Android emulator, it turns out that my app starts normally. Supposedly it supports error-free data insertion into the sqlite file. However, after closing my application and opening it again, all registered data disappeared. Code sampleimport flet as ft
import sqlite3
class Book():
def __init__(self, id:int=None, title:str=None) -> None:
self.id = id
self.title = title
def main(page:ft.Page):
conexao = sqlite3.connect("assets/agenda.db", check_same_thread=False)
cursor = conexao.cursor()
book_table = """CREATE TABLE IF NOT EXISTS book(
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL
)"""
cursor.execute(book_table)
conexao.commit()
def select_books()->list[Book]:
book_list:list[Book] = []
sql = "SELECT * FROM book"
res = cursor.execute(sql)
for el in res:
book = Book(id=el[0], title=el[1])
book_list.append(book)
return book_list
def insert_book(book:Book):
params = (book.title,)
sql = """INSERT INTO book(title)
VALUES(?)"""
cursor.execute(sql, params)
conexao.commit()
def book_title_loading():
dt_book_title.rows.clear()
for el in select_books():
dt_book_title.rows.append(
ft.DataRow(
cells=[
ft.DataCell(ft.Text(el.id)),
ft.DataCell(ft.Text(el.title)),
],
)
)
def register_book_title_clicked(e):
book = Book(title=ttf_book_title.value)
insert_book(book=book)
book_title_loading()
ttf_book_title.value = ""
page.update()
ttf_book_title = ft.TextField(label="Book title", hint_text="Enter title", autofocus=True)
btn_register_book_title = ft.ElevatedButton(text="Register title", on_click=register_book_title_clicked)
dt_book_title = ft.DataTable(
columns=[
ft.DataColumn(ft.Text("Id")),
ft.DataColumn(ft.Text("Book title"))
],
rows=None
)
col_books = ft.Column(
visible=True,
scroll=ft.ScrollMode.ALWAYS,
controls=[dt_book_title]
)
cnt_col_jogos = ft.Container(
content= col_books,
# alignment=ft.alignment.top_center,
expand=True,
)
book_title_loading()
page.add(ft.Text(value="Hello Sqlite3"))
page.add(ttf_book_title)
page.add(btn_register_book_title)
page.add(cnt_col_jogos)
ft.app(target=main, assets_dir="assets") Error messageAfter registering data in Sqlite, I close the application. When opening again, the data no longer exists and this is the problem, that is, where did the data go?
Using the Nexus 6 Android emulator (API 34), when opening the application for the second time, I EXPECTED to see the previously registered data. However, the data that was registered disappears, that is, it does not remain and was simply lost. This behavior is not repeated when run through a web page or desktop. ------------------------------------------------------
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
you can use android studio to create python flet project? will it show the available method when you ctrl + space like flutter did? |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
#2189