Page 1 of 1

sqlite on android

Posted: Wed Jan 07, 2015 5:01 am
by grimaldiface
Hi all,

recently finished developing my first iphone app, and now I'm adapting it for android. everything works perfectly in the iphone version. The app uses a sqlite db that is copied over in the copy files to specialFolderPath("engine"). When I try to connect to the db on android, I get an error I was unable to connect to the database. "Database Error: Unable to open the database file".

I verified that the file is properly copying by setting the defaultfolder to specialFolderPath("engine") and putting the files. The database is there I just can't connect to it.
I made sure to check "sqlite" in the standalone settings!

Not sure what else to do. Thoughts?

Re: sqlite on android

Posted: Wed Jan 07, 2015 9:40 am
by Mark
Hi,

What is the exact path to the database? Often, having characters like ()-/: and probably a few others cause such problems on Android. Another possibility is that it wants to be able to make changes to the database but lacks write permission. Copying to the documents folder might help in that case.

Kind regards,

Mark

Re: sqlite on android

Posted: Wed Jan 07, 2015 1:38 pm
by Klaus
Hi grimaldiface,

I may repeat myself, but the SEARCH function here in the forum isn't really THAT bad! 8)
Check this: http://forums.livecode.com/viewtopic.ph ... 16#p114411


Best

Klaus

Re: sqlite on android

Posted: Wed Jan 07, 2015 5:36 pm
by grimaldiface
Mark wrote:Hi,

What is the exact path to the database? Often, having characters like ()-/: and probably a few others cause such problems on Android. Another possibility is that it wants to be able to make changes to the database but lacks write permission. Copying to the documents folder might help in that case.

Kind regards,

Mark
Right now I am using the following code to connect to the db. Only trying to connect at this point, not write changes.

The code I use in my working iPhone version is this...

Code: Select all

 if environment() is "mobile" then
      set the defaultfolder to specialFolderPath("engine")
 else
      set the defaultfolder to the desktop folder
end if 
put revOpenDatabase("sqlite", "strategyDB.sqlite") into tDatabaseID
In another thread, setting the default folder was poo poo'd, so I've also tried:

Code: Select all

if environment() is "mobile" then
      put specialfolderpath("engine") & "/strategyDB.sqlite" into tDBFile
      put revopendatabase("sqlite",tDBFile) into tDatabaseID
end if
Still no luck. Not sure why this works on iOS but not android....

Re: sqlite on android

Posted: Wed Jan 07, 2015 5:39 pm
by Klaus
grimaldiface wrote:Not sure why this works on iOS but not android....
It definitively shouldn't work on iOS either!
Why not take a look at the link I provided? 8)

Re: sqlite on android

Posted: Wed Jan 07, 2015 8:32 pm
by grimaldiface
Klaus wrote:
grimaldiface wrote:Not sure why this works on iOS but not android....
It definitively shouldn't work on iOS either!
Why not take a look at the link I provided? 8)

Klaus--

Thanks. I did read the thread you provided prior to posting this one. You're right, the search isn't THAT bad (but it's still pretty bad!).

I guess I'm not seeing the answer to my problem in that thread--care to help me out? What I don't understand is that you mention I need write privileges for the engine folder? I don't understand why, if I'm not writing anything to the folder, just reading. Maybe I there's something about accessing a database that I don't understand.....

Edit: I'll try copying to documents folder later this evening. But I'm still interested to learn why this isn't working...

Re: sqlite on android

Posted: Thu Jan 08, 2015 12:58 pm
by Klaus
Hi Grimadliface,

see it this way:
The ENGINE folder is TABOO, noone is allowed to write here!

I'm not sure about the actual technical background, but I think opening a database prepares
the db file to be read AND written to and this is prohibited in any case!

Therefore you need to copy it to the users DOCUMENTS folder where we have full access!


Best

Klaus

Re: sqlite on android

Posted: Thu Jan 08, 2015 9:51 pm
by grimaldiface
Klaus wrote:Hi Grimadliface,

see it this way:
The ENGINE folder is TABOO, noone is allowed to write here!

I'm not sure about the actual technical background, but I think opening a database prepares
the db file to be read AND written to and this is prohibited in any case!

Therefore you need to copy it to the users DOCUMENTS folder where we have full access!

Best

Klaus
Sure enough, moving the db to the DOCUMENTS folder solved the problem. There's definitely some difference between iOS and Android here, because I was definitely able to access the db from the ENGINE on iOS. Nevertheless, seems the best course is to just avoid working from the ENGINE altogether. Thanks!

Re: sqlite on android

Posted: Wed Dec 02, 2015 4:17 am
by seanmiller
Howdy,

Belatedly, I can confirm this is indeed a key difference between iOS and Android. I built an app using SQLite. The database resides in the engine on iOS and works fine. But it didn't work on Android. When I moved the SQLite database to documents, it worked immediately. Thanks for solving this problem!

Sean

Re: sqlite on android

Posted: Wed Dec 02, 2015 5:36 am
by quailcreek
Here's how I handle this.

Code: Select all

 if the environment is not "mobile" then exit preOpenStack
   put specialFolderPath("documents") & “/MyDataBase.sqlite" into tDatabaseFile
   
   if there is not a file tDatabaseFile then
      put url("binfile:" & specialFolderPath("engine") & "/MyDataBase.sqlite") into url("binfile:" & specialFolderPath("documents") & "/MyDataBase.sqlite")
   else
      --      answer "There is a file MyDataBase.sqlite"
   end if