Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to save data to .db file?

Explorer ,
Jul 04, 2011 Jul 04, 2011

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

TOPICS
ActionScript
1.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 04, 2011 Jul 04, 2011

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);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 04, 2011 Jul 04, 2011

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);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 05, 2011 Jul 05, 2011

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();
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 06, 2011 Jul 06, 2011
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines