SQLite – open and openAsync methods NOT create folders for created database file

If you run into problems with connecting to the database and you are using open or openAsync with CREATE mode then check if folder location exists for your database file,

following code will generate SQLError

SQLError: ’Error #3125: Unable to open the database file.’, 
details:’Connection closed.’, operation:’open’, detailID:’1001’

import flash.data.SQLConnection;
import flash.data.SQLMode;
import flash.filesystem.File;

var m_oAppStorageDir:File = File.documentsDirectory.resolvePath("myFolder/structure");//my documents or sdcard

var m_oConnection:SQLConnection = new SQLConnection();

try
{
	m_oConnection.open(m_oAppStorageDir.resolvePath("myDb.db"), SQLMode.CREATE);
}
catch (e:Error)
{
	trace(e.getStackTrace());
}

it is because differently then in FileStream class the SQLConnection creates only database file and not entire folder structure. To fix it call createDirectory method of the File instance.

import flash.data.SQLConnection;
import flash.data.SQLMode;
import flash.filesystem.File;

var m_oAppStorageDir:File = File.documentsDirectory.resolvePath("myFolder/structure");//my documents or sdcard
try
{
	m_oAppStorageDir.createDirectory();
}
catch (e:Error)
{
	trace(e.getStackTrace());
}

var m_oConnection:SQLConnection = new SQLConnection();

try
{
	m_oConnection.open(m_oAppStorageDir.resolvePath("myDb.db"), SQLMode.CREATE);
}
catch (e:Error)
{
	trace(e.getStackTrace());
}
This entry was posted in actionscript, air for android, flash, SQLite3 (in AIR) and tagged , , , , , , , , . Bookmark the permalink.