Quantcast
Channel: Appcelerator Developer Center Q&A Unanswered Questions 20
Viewing all articles
Browse latest Browse all 8068

SQLite update query

$
0
0

Hello all.

I am new to Titanium Alloy development but not new to Javascript. I have been trying to work with SQLite in a Titanium Alloy application. The SQLite insert and create DB queries work fine. However my update query is not working at all. Following is the code that I am using.

My db.js file

var items = [];
var db = Ti.Database.open('listDB');
//db.file.setRemoteBackup(false);
 
db.execute('CREATE TABLE IF NOT EXISTS liststatus(id INTEGER PRIMARY KEY, name TEXT, value INTEGER);');
var my_result_set = db.execute('SELECT * FROM liststatus');
var records = my_result_set.rowCount;
 
Ti.API.info(records);
if(records == 0){
    db.execute('INSERT INTO liststatus(id, name, value) VALUES (1, "Node #1", 0)');
    db.execute('INSERT INTO liststatus(id, name, value) VALUES (2, "Node #2", 0)');
    db.execute('INSERT INTO liststatus(id, name, value) VALUES (3, "Node #3", 1)');
}
 
var rows = db.execute('SELECT * FROM liststatus');
 
while (rows.isValidRow())
{
    if(rows.fieldByName('value') == 1){
        var truVal = "true";
    }else{
        var truVal = "false";
    }
    items.push({"id": rows.fieldByName('id'), "label":{text: rows.fieldByName('name')},"switchVal":{value: truVal}, "template": "title_only"});
    rows.next();
};
 
$.dynamicListView.sections[0].setItems(items);
 
var itemsUpdate = [];
 
function outputState(e){
    var section = $.dynamicListView.sections[e.sectionIndex];
    var item = section.getItemAt(e.itemIndex);
    //itemsUpdate.push({"id": item.id, "value": e.value});
    var val = e.value;
    var itemId = item.id;
    //db.execute('UPDATE liststatus SET value=? WHERE id=?',1,1);
    db.execute('UPDATE liststatus SET value=? WHERE id=?',val,itemId);
}
rows.close();
db.close();
My view is pretty simple again - the db.xml file is as follows.
<Alloy>
    <Window class="container" id="dbWin">
        <!--<Toolbar top="0" platform="ios">
            <Items>
                <Button id="button" onClick="saveData">Save</Button>
            </Items>
     </Toolbar>-->
        <ListView id="dynamicListView">
            <Templates>
                <ItemTemplate name="title_only">
                    <View layout="horizontal">
                        <Label class="label" bindId="label"/>
                        <Switch bindId="switchVal" onChange="outputState" />
                    </View>
                </ItemTemplate>
            </Templates>
            <ListSection/>
        </ListView>
    </Window>
</Alloy>
What I am trying to achieve is that on change of the toggle switch, my DB should get updated.

Any hep in this regard is greatly appreciated.

Regards. Shreerang


Viewing all articles
Browse latest Browse all 8068

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>