get Column names SQLite tables using Adobe AIR + Javascript


var db = air.File.documentsDirectory.resolvePath("[database path / database name]");
conn = new air.SQLConnection();
conn.open(db);
sql = new air.SQLStatement();
sql.sqlConnection = conn;

conn.loadSchema();
ax = conn.getSchemaResult();

var col = [];

sc = ax.tables;
error(sc);
num = sc.length;
for (var i=0;i<num;i++){
    if (sc[i].name == "[table name contains the column]"){
        numc = sc[i].columns.length;
        for (var y=0;y<numc;y++){
            nn = sc[i].columns[y].name;
            col.push(nn);
            error(y);
        }
    }
}

var cx = 0;
for (var x=0;x<col.length;x++){
    error(col[x]);
    if (col[x] == "[column name we are looking for]"){
        cx = 1;
    }
}
error(cx);
if (cx != 1){
        sql.text = "ALTER TABLE [tablename] ADD COLUMN [column name] TEXT";
        sql.execute();
        error("altered");
}else{
    error("not altered");
}

Tinggalkan Balasan