Page 1 of 1
Standalone settings for my app with a database
Posted: Tue Jul 14, 2015 12:27 am
by jalz
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
Re: Standalone settings for my app with a database
Posted: Tue Jul 14, 2015 5:32 am
by bangkok
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
Re: Standalone settings for my app with a database
Posted: Tue Jul 14, 2015 12:10 pm
by Klaus
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")
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
Re: Standalone settings for my app with a database
Posted: Tue Jul 14, 2015 6:29 pm
by AxWald
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!
Re: Standalone settings for my app with a database
Posted: Fri Jul 17, 2015 12:58 am
by jalz
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