How to save data to .db file?
Copy link to clipboard
Copied
Dear all,
How do I save data from an AIR app to a .db file?
I'm learning how to do an AIR app to take the strings from the user inputs textfield and generate a .db file that will be saved in 'My Documents' in the user's computer when the user clicked on the 'Save' button in the AIR app. Please guide me along...
Thanks in advance,
-Zainuu
Copy link to clipboard
Copied
you can start with:
var sqlConn:SQLConnection = new SQLConnection();
// add event handlers
var dbFile:File = File.applicationStorageDirectory.resolvePath("yourdbname.db");
// SQLMode.CREATE is default openAsync 2nd parameter which creates db if doesn't exist and opens it for updating if it does exist
sqlConn.openAsync(dbFile);
Copy link to clipboard
Copied
Hi kglad, thanks for your reply.
I have created an empty AIR flash file and used your code for connection and I assume that it is good and ready to run since there is no errors shown. But when I CTRL+ENTER, I cannot find yourdbname.db in 'My Documents' and how do I save the data to yourdbname.db?
var sqlConn:SQLConnection = new SQLConnection();
// add event handlers
var dbFile:File = File.documentsDirectory.resolvePath("yourdbname.db");
// SQLMode.CREATE is default openAsync 2nd parameter which creates db if doesn't exist and opens it for updating if it does exist
sqlConn.openAsync(dbFile);
Copy link to clipboard
Copied
your code using documentsDirectory is correct and will work. you probably just need to write something to your database:
var sqlConn:SQLConnection = new SQLConnection();
sqlConn.addEventListener(SQLEvent.OPEN, openHandler);
sqlConn.addEventListener(SQLErrorEvent.ERROR, errorHandler);var dbFile:File = File.documentsDirectory.resolvePath("yourdbname.db");
// SQLMode.CREATE is default openAsync 2nd parameter which creates db if doesn't exist and opens it for updating if it does exist
sqlConn.openAsync(dbFile);
function openHandler(event:SQLEvent):void {
createTableF();
}
function errorHandler(event:SQLErrorEvent):void {
}
//****************** begin create table ***********************************
function createTableF(){
var createStmt:SQLStatement = new SQLStatement();
createStmt.sqlConnection = sqlConn;
var sql:String =
"CREATE TABLE IF NOT EXISTS docks ( dockId INTEGER PRIMARY KEY AUTOINCREMENT, dockName TEXT, dataS TEXT)";
createStmt.text = sql;
// add listener functions for next step
//createStmt.addEventListener(SQLEvent.RESULT, createResult);
//createStmt.addEventListener(SQLErrorEvent.ERROR, createError);
createStmt.execute();
}
Copy link to clipboard
Copied
Hi kglad, thanks for your reply.
Im guessing that the following code is to create a table in .db
var createStmt:SQLStatement = new SQLStatement();
createStmt.sqlConnection = sqlConn;
var sql:String =
"CREATE TABLE IF NOT EXISTS docks ( dockId INTEGER PRIMARY KEY AUTOINCREMENT, dockName TEXT, dataS TEXT)";
createStmt.text = sql;createStmt.execute();
how about adding data in the table and if the application is closed and user open it next time, it will retrieve the data from the table to flash?
what is this step about?
// add listener functions for next step
//createStmt.addEventListener(SQLEvent.RESULT, createResult);
//createStmt.addEventListener(SQLErrorEvent.ERROR, createError)
-Zainuu

