Standalone settings for my app with a database

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller

Post Reply
jalz
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 340
Joined: Fri Sep 12, 2008 11:04 pm

Standalone settings for my app with a database

Post by jalz » Tue Jul 14, 2015 12:27 am

Hi Guys,

I have an app which I've developed which has a sqlite database linked to it. The database is in the same folder as my stack script. All work well within the live code environment. I'm experimenting building a standalone using the community edition (7.0.6). The stack compiles correctly (although it complains regarding an icon I haven't chosen), however when I launch the app on my Mac, it says it can't find the database table. I have a fairly straightforward database connection script which I will list below. My question is, should the standalone builder embed (or copy) the database in my application as?

Logically (well to me) I copied over my sqlite db into the same location as the standalone application, but when I launch the standalone, it still says it can't find the various tables to process

This is my code in the card script of the main card, have UI missed a crucial step somewhere in the building process?

Code: Select all

on openCard
   --if gConnID is empty then
   put the effective filename of this stack into filepath
   set the itemdelimiter to slash
   put "mydb.sqlite" into the last item of filepath
   
   put revOpenDatabase("sqlite", filepath,,,,,,) into gConnID
   
   start using stack "CodeLibrary"
   start using stack "ObjectLibrary"
   
   createFontMenu
   
end openCard

Many thanks
Jalz

bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 837
Joined: Fri Aug 15, 2008 7:15 am

Re: Standalone settings for my app with a database

Post by bangkok » Tue Jul 14, 2015 5:32 am

Don't forget... global.

And a small test to check if everything is okay.

on openCard
global gConnID
--if gConnID is empty then
put the effective filename of this stack into filepath
set the itemdelimiter to slash
put "mydb.sqlite" into the last item of filepath

put revOpenDatabase("sqlite", filepath,,,,,,) into gConnID

if gConnID is not a number then
answer error "Problem : "&gConnID
exit to top
end if

start using stack "CodeLibrary"
start using stack "ObjectLibrary"

createFontMenu

end openCard

Klaus
Posts: 11309
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Standalone settings for my app with a database

Post by Klaus » Tue Jul 14, 2015 12:10 pm

Hi Jalz,

use the "Copy files" tab in the standalone builder to add your db file to the app package.
then you will find the db file here -> specialfolderpath("resources")
8)

Hint:
Opening the db file right in the app package will fail if your final user will put the standalone
into the Applications folder because of missing WRITE permissions!

Do like on the mobile platform and copy the db file to the users "Documents" folder or whereever
when the app starts the very first time!


Best

Klaus

AxWald
Posts: 368
Joined: Thu Mar 06, 2014 2:57 pm

Re: Standalone settings for my app with a database

Post by AxWald » Tue Jul 14, 2015 6:29 pm

Hi,

might be that you're gettting slashs in the way - too lazy to check now, I'll rather provide my tested solution. In the openstack handler of the standalone:

Code: Select all

   put getMyLoc(the short name of this stack, true) & "/" into myPath
   Put MyPath & the SPDB of this stack into MyDB
   -- the SPDB contains the DBName!
and from my Lib_Stack:

Code: Select all

function getMyLoc StkName, IsPath
   -- returns the name (IsPath = true) or the path of an open stack StkName
   
   set the itemDel to slash
   put item 1 to -2 of the effective filename of stack StkName into myPath
   put the last item of the effective filename of stack StkName into myName
   if isPath then
      return MyPath
      -- no Slash!
   else
      return MyName
      -- with file.extension
   end if
end getMyLoc
I ever battled with getting the correct names/ paths to find my databases, pref-files, stacks-in-use etc., until one day I took my time to really test this, and to find the final solution.

Be aware that this is probably not usable 1:1 on systems where the StandAlone is in a write-protected place! There you might add a test in the openstack handler if this is true, and modify the path (or the GetMyLoc function) accordingly.

Hope it helps, have fun!
Livecode programming until the cat hits the fan ...

jalz
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 340
Joined: Fri Sep 12, 2008 11:04 pm

Re: Standalone settings for my app with a database

Post by jalz » Fri Jul 17, 2015 12:58 am

Hi Guys

Thanks for the replies. So close to getting it work! I'm using Klaus' approach where I use the copy using the copy files tab in the standalone and then write the file into the documents folder and access it through this folder. The only problem is, the file copied into the documents location seems to be in plain text file (when i get info ) where as it should be sqlite. The name of the file including the extension is correct. When I manual copy the sql file over the the text file, my standalone recognises the tables and seems to execute commands correctly. I'll list the code I have below to see if someone can spot any issues.

Code: Select all


global gDataPath, gConnID

on openCard
   answer "hello"
   FirstCreateFiles
   DatabaseConnect
   
end openCard

on FirstCreateFiles // Create original startup files if the do not exist
   
    put specialFolderPath("resources") & slash into tFileSrc
   
   put specialFolderPath("documents") & slash & "apps" & slash into tFileDest
   
   // check if folder exists
   if there is no folder tFileDest then
      create folder tFileDest
   end if 
   
   //check if file exists
   if there is not a file (FileDest&"theDB.sqlite") then 
      --only copy first time.
      put url ("binfile:"&tFileSrc&"theDB.sqlite") into url ("binfile:"&tFileDest&"theDB.sqlite")
   end if 
   
   put tFileDest & "theDB.sqlite" into gDatabasePath
   
end FirstCreateFiles


command DatabaseConnect // Connect to DB
   
   put revOpenDatabase("sqlite", gDatabasePath,,,,,,) into gConnID
   
   start using stack "CodeLibrary"
   start using stack "ObjectLibrary"
   
   if gConnID is not a number then
      answer error "Problem : " & gConnID
      exit to top
   end if
   
end DatabaseConnect


Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”