PDA

View Full Version : Attaching a database to your application


tazboy
08-31-2008, 01:40 AM
I'm trying to attach a local pre-made database to my application and then be able to update it and such. Well, the attaching part is fairly simple, but I read that AIR makes the database a read-only file since it is with all your other files. I also read that I need to write some code that will copy that database to another location on the user's drive so that I can write to it.

Here's the site that talks about how to do it.
http://www.adobe.com/cfusion/communityengine/index.cfm?event=showdetails&postid=10483&loc=en_US&productid=4
I tried it a couple different ways but I can't figure it out. I'm not even sure of the best location to copy it to.

Any help or suggestions would be greatly appreciated. Thanks.

tazboy
08-31-2008, 10:06 PM
Well, I apparently figured it out. Here's my final code:

private function openDatabase():void {
conn = new SQLConnection();
var dbFile:File = File.applicationDirectory.resolvePath("db/Contacts.db");
var dbWorkedFile:File = File.documentsDirectory.resolvePath("Contacts.db");

if(dbFile.exists){
conn.addEventListener(SQLEvent.OPEN, openHandler);
}
else {
dbFile.copyTo(dbWorkedFile);
conn.addEventListener(SQLEvent.OPEN, createHandler);
}
conn.addEventListener(SQLErrorEvent.ERROR, errorHandler);
conn.openAsync(dbFile);
}

Looking at this code just now I realized that I didn't use the dbWorkedFile in the conn.openAsync(). Maybe I don't really need it. Not sure if this helps but I wanted to add it in case it might be of use.