puks1978
puks1978

Reputation: 3697

iOS AS3 AIR local storage

I am developing an app which requires a SQL DB on the device. I am using the File.applicationStorageDirectory and folder.resolvePath to add a new DB. When debugging the app it all looks like it executes correctly and I am able to successfully create a new table.

I haven't gone too far with inserting and reading records however I just wanted to ask, when I re-run the app does the existing DB file get replaced with a new empty one? If so do I need to check if the file exists etc.

How can I look at the DB on the device (iOS at this stage)?

Thanks

Upvotes: 0

Views: 1843

Answers (2)

Josh
Josh

Reputation: 8159

Yes, it will be saved and you can access it at any time. This will work for anything, not just databases (thus the generic names and object types)

var file:File = File.applicationStorageDirectory.resolvePath( "file.path" );
var obj:Object = { someRandom:"data" };
var fs:FileStream = new FileStream();
fs.open( file, FileMode.WRITE );
fs.writeObject( obj );
fs.close();

The above will write the Object obj to the device. Keep in mind that this will not update the file, it will overwrite the file. To update the file, do this:

var file:File = File.applicationStorageDirectory.resolvePath( "file.path" );
if ( file.exists ) {
    var fs:FileStream = new FileStream();
    fs.open( file, FileMode.READ);
    var obj:Object = fs.readObject();
    fs.close();
}

Then you update your data and proceed to write as I showed in the first snippet

Upvotes: 1

Raja Jaganathan
Raja Jaganathan

Reputation: 36167

Try with this

If you using resolvePath if file doesn't exist it created for you else it leave as it is.Overwrite never happen if you using resolvepath.

OR

If you want to check if already exists through

        if(dbFile.exist) 
           trace('File already exists');

Use this

        var dbFile:File = new File("app-storage:/" + "localdb.db");
        sqlConnection = new SQLConnection();            
        sqlConnection.addEventListener(SQLEvent.OPEN, onAuthorDatabase_OpenHandler);
        sqlConnection.addEventListener(SQLErrorEvent.ERROR, onSQLErroHandler);
        sqlConnection.openAsync(dbFile);

I don't know how to look db file on device storage place(iOS).

Upvotes: 0

Related Questions