Avg. Rating 3.9

Problem

Always when execute the AIR app we can connect with the database file and create the same table and this may do loose data.

Solution

The simple way to resolve it is verify if the table is created into database file using a simple SQLite instruction IF NOT EXISTS

Detailed explanation

One way to resolve this problem is the code bellow. First function only connect with the database.db and the second function will create the table into database. A normal way to create one table, with out verify if the table alread created into database.db, using SQLite instructions is only CREATE TABLE, but it will create always when run app the table. Using IF NOT EXISTS we can resolve it.

private function init():void

{
conn = new SQLConnection();
try
{
var dbFile:File = File.applicationStorageDirectory.resolvePath("database.db");
conn.open(dbFile);
}
catch (error:SQLError)
{
trace("Error opening database");
trace("error.message:", error.message);
trace("error.details:", error.details);
return;
}
createTable();
}
 
private function createTable():void
{
createStmt = new SQLStatement();
createStmt.sqlConnection = conn;
var sql:String = "";
sql += "CREATE TABLE IF NOT EXISTS rssURL (";
sql += " id INTEGER PRIMARY KEY AUTOINCREMENT,";
sql += " postName TEXT,";
sql += " postURL TEXT,";
sql += " postText TEXT";
sql += ")";
createStmt.text = sql;
try
{
createStmt.execute();
}
catch (error:SQLError)
{
trace("Error creating table");
trace("CREATE TABLE error:", error);
trace("error.message:", error.message);
trace("error.details:", error.details);
return;
}
}

+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes