1

I have created an app in livecode that uses an sqlite database. The app connects to the database base using the following code:

on preopenstack 
--Used to connect to the database when application 
--first open and open the menu stack

   set the itemDelimiter to " "
   put item 2 of  the name of the current stack into stackname
   put  the length of stackname into namelength
   put char 2 to namelength-1 stackname into stackname

   if stackname= "FoodPoisoningInvestigator" then

      -- Open a connection to the database and store 
      --the database id into global variable so 
      --other handlers can access it
      put the effective filename of this stack into tFolderPath
      set the itemDelimiter to slash
      delete last item of tFolderPath

      put tFolderPath & "/mysql2.sqlite" into gDatabasePath
      put revOpenDatabase("sqlite",gDatabasePath) into gDatabaseID

   end if 

end preopenstack

In the file setting of the standalone setting dialog I have selected the mysql2.sqlite which is added to the folder when creating windows standalone but not for android. The app installs on the mobile but it does not connect to the database even when I manually add it to the folder before installing.

What am I doing wrong?

Sheils
  • 323
  • 2
  • 22

1 Answers1

0

Perhaps, yor app doesn't have permission to access the database in the apk package or gDatabasePath doesn't contain what you expect or the path doesn't exist. Note that the .apk package is a zip file, which can't be accessed directly and thus virtual paths are used. Try

put specialFolderPath("engine") & "/mysql2.sqlite" into gDatabasePath

or install the database into

specialFolderPath("documents")

and use

put specialFolderPath("documents") & "/mysql2.sqlite" into gDatabasePath
Mark
  • 2,380
  • 11
  • 29
  • 49
  • Mark, I tried `put specialFolderPath("documents") & "/mysql2.sqlite" into gDatabasePath put revOpenDatabase("sqlite",gDatabasePath) into gDatabaseID` and also create a field on a card call test and used `put gDatabasePath into fld "test"` to display the location of the db. I get /data/data/com.sheilsapp.FPI13/files/mysql2.sqlite. I am know getting revdberr in the fields that require data from the database. – Sheils Nov 04 '15 at 20:07
  • Mark, you explained that .apk package is a zip file. How do I find out if the sqlite file has been added to the package – Sheils Nov 04 '15 at 20:24
  • You can unzip the file and see if the sqlite file is there or you can use syntax like `there is a file gDatabasePath`. – Mark Nov 05 '15 at 13:09
  • I am still struggling. I have unzipped and the sqlite file is in the asset folder. The largest folder is lib so I guess that it is housing the app. Any suggestions based on this info? – Sheils Nov 22 '15 at 05:52
  • using `the effective filename of this stack ` reveals that the app is stored in /data/app/com.sheilsapp.FPI13. I have tried /data/app/com.sheilsapp.FPI13/files/mysql2.sqlite as the database path with no luck – Sheils Nov 22 '15 at 06:02
  • Mark I came across some tutorial in on this link http://blog.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/ but it is old and I will have to change the name of all the primary key which means rewritting the codes that referred to them.Is that the only possible way? – Sheils Nov 22 '15 at 06:16
  • You don't have to change primary keys for the database to be found and the tutorial is for Java and not for LiveCode. – Mark Nov 22 '15 at 10:01
  • I am also unclear about how to use the SQLiteOpenHelper in android. Is there a livecode version of the code – Sheils Nov 22 '15 at 12:23
  • OK I have given up trying to ship the database with the app. Instead I am running an sql code to create the database on first use. That works fine when the database is created in specialFolderPath("documents") – Sheils Nov 23 '15 at 19:07
  • Creating the database by script would have the same effect as saving the database file in a custom property and putting it into the correct location using the `put url` command. Your current method is also possible (obviously), it is just slightly more complicated. – Mark Nov 24 '15 at 09:43
  • Mark you are right it is more complicated. I would rather ship the file with the app and save it to the location. So how do I save the database file in a custom property. So far I have not found any example of how to save the file in a custom property – Sheils Nov 25 '15 at 08:55