SQLite Database not Opening on Android

New to Moai? Get started here with our best tips and tutorials.

SQLite Database not Opening on Android

Postby sgeos » Mon Jul 09, 2012 2:55 am

My LuaSQL sample runs on OSX and iOS. FWIW, this sample should run on a vanilla installation of Moai. When I try to open an SQL database on the Android host, I get the following error:
unable to open database file

Do I need to do anything special to use SQLite databases on Android?

EDIT: It looks like the short answer is yes. What does a person need to do to get a SQLite database to work on Android with Moai? Should there be some sort of an initialization call that moves the database to the correct location if the DB exists and the script is running on Android?
Any original code posted by me is released via the CC0 Public Domain Dedication. It is in the public domain. Do whatever you want with it.
User avatar
sgeos
 
Posts: 241
Joined: Sat Apr 28, 2012 4:42 am
Location: Married in Japan.

Re: SQLite Database not Opening on Android

Postby sgeos » Thu Jul 12, 2012 3:14 pm

In case anyone else runs into the same problem, this is what I wound up using.
Code: Select all
  1. require "luasql"

  2.  

  3. -- Database File Parameters

  4. dbname = "mydatabase.db"

  5. dbpath = "./"

  6.  

  7. -- Special Handling for Android

  8. if "Android" == MOAIEnvironment.osBrand then

  9.   local systemdbpath = MOAIEnvironment.documentDirectory:gsub("/files", "/databases/")

  10.   if not MOAIFileSystem.checkFileExists(systemdbpath .. dbname) then

  11.     MOAIFileSystem.copy(dbpath .. dbname, systemdbpath .. dbname)

  12.   end

  13.   dbpath = systemdbpath

  14. end

  15.  

  16. -- Create Environment and Connect to DB

  17. environment = assert ( luasql.sqlite3 () )

  18. connection  = assert ( environment:connect ( dbpath .. dbname ) )

  19.  

  20. -- Queries Here

Any original code posted by me is released via the CC0 Public Domain Dedication. It is in the public domain. Do whatever you want with it.
User avatar
sgeos
 
Posts: 241
Joined: Sat Apr 28, 2012 4:42 am
Location: Married in Japan.

Re: SQLite Database not Opening on Android

Postby ibisum » Fri Jul 13, 2012 12:47 am

Great, thanks for sorting that out - exactly this problem was causing me a bit of trouble earlier this week on Android and now I know why.
;
--
ibisum@gmail.com
Got a MOAI snippet? Please consider adding it to http://moaisnippets.info
User avatar
ibisum
 
Posts: 1026
Joined: Mon Oct 17, 2011 1:11 am
Location: Vienna, Austria


Return to New Users

Who is online

Users browsing this forum: No registered users and 1 guest

x