db = db_pathif db.is_file(): print("DB already exists")else: createDb(db)
On Android, Context.GetFilesDir is returned.
On Android, Context.GetFilesDir is returned.
Changed in version 1.11.0: On Android, this function previously returned /sdcard/<app_name>. This folder became read-only by default in Android API 26 and the user_data_dir has therefore been moved to a writeable location.
if platform == 'android':
from android.storage import app_storage_path
datadir = app_storage_path()
db_path = Path('user/database.db')
How do I refer to user_data_dir?
from kivy.app import App
from kivy.uix.label import Label
from android.storage import app_storage_path
from os.path import abspath
from os import getcwd
class MyApp(App):
def build(self):
text = App.get_running_app().user_data_dir + '\n'
text += self.user_data_dir + '\n'
text += app_storage_path() + '\n'
text += getcwd() + '\n'
text += abspath('.') + '\n'
text += abspath('~') + '\n'
text += abspath(__file__) +'\n'
return Label(text=text)
if __name__ == '__main__':
MyApp().run()
class RecipApp(App): def build(self): user_dir = App.get_running_app().user_data_dir print('THE DIRECTORY IS: ',user_dir) dbFile = user_dir+'/recipes.db'
RecipApp/
|-RecipApp/
| |-db/
| | |-query.py
| | |-__init__.py
| |-app.py
| |-__init__.py
|-main.py
class RecipApp(App): def build(self): user_dir = App.get_running_app().user_data_dir
dbFile = user_dir+'/recipes.db'
def fetchIngredients(ingInput): conn = sqlite3.connect(dbFile)
Here is what I would suggest:
class RecipApp(App):
@property
def dbfile(self):
return self.user__data_dir+'/recipes.db'
# in the App class, self is App.get_running_app()
def fetchIngredients(ingInput):
app = App.get_running_app()
conn = sqlite3.connect(app.dbfile)
As an FYI you might also want to look at the pathlib library in the python standard library for constructing and manipulating file manes and paths.
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/39677ec4-fa9e-41ee-92a3-b39ebed316e5o%40googlegroups.com.
AttributeError: 'NoneType' object has no attribute 'dbFile'
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-...@googlegroups.com.
As in the example.
Looks like an error somewhere, attach some code.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/d2b6bdd8-372f-43a1-b67c-c9d7693e9b63o%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/d2b6bdd8-372f-43a1-b67c-c9d7693e9b63o%40googlegroups.com.
from kivy.utils import platformfrom pathlib import Pathimport os.path
if platform == 'android': from android.storage import app_storage_path
user_data_dir = app_storage_path() dbFile = user_data_dir+'/recipes.db'else: dbFile = Path('user/recipes.db')
def dbInit(): if os.path.isfile(dbFile): return dbFile else: conn = sqlite3.connect(dbFile)
# a bunch of sqlite operations
return dbFile
class RecipApp(App): manager = ObjectProperty() def build(self): dbFile = dbInit()