Question

i am trying to get remote mysql data from the server to be inserted into sqlite database

i have gone through tutorials from here where the remote data is displayed using json array. i want to insert the mysql data into sqlite db stored on the phone it self. i m developing the app in titanium appcelerator . the refrence i took was from here i have been looing for a simple tutorial but could not find any my problem is using the tutorial i have been able to display the remote data and local data from the app which i have previously inserted in the sqlite db . however when i try to insert the remote data from mysql db to sqlite db it doesnot get inserted. i am using the same database that the tuorial has provided for mysql and have created a simlar database in sqlite.the structure of both the db's is same id, integer and shade, text

the code i am trying is

var win = Ti.UI.Createwindow;

var db = Ti.Database.install('titanium_json_db.sqlite','colors');
    //db.execute('CREATE TABLE IF NOT EXISTS DATA (id INTEGER, shade TEXT)');
    var resultSet = db.execute('SELECT * FROM colors');


if(resultSet.rowCount < 1){
alert("no records");
    while(resultSet.isValidRow()){
var xhr = Ti.Network.createHTTPClient(); 

xhr.open('GET', 'http://192.168.0.xxx/Titanium-Mobile_Database-Driven_Source/read.php');
xhr.send();


xhr.onload = function(){
        var json = JSON.parse(this.responseText);
        if (json) { 
        Titanium.API.info('Error - Null return!');
        alert("json error");
        return;
        }

        for(var i=0; i < json.colors.length; i++){
        db.execute('INSERT INTO colors (id, shade) VALUES(?,?)',json.colors[i].id,json.colors[i].shade);

        };

    };
};
};    

win.open();

any help would be great.

Was it helpful?

Solution

Your XHR request will never execute.

if(resultSet.rowCount < 1){
alert("no records");
    while(resultSet.isValidRow()){

If the result set has no rows, it alerts "no records", then it conditionally executes the XHR if there is a valid returned row.

That code block could be re-written as follows:

var rows = [];
if(rows.length < 1) {
    alert('No rows!');
    if (rows[0]) {
        rows.push('Never going to happen.');
    }
}

It's also important to note that the XHR request is asynchronous. The while loop isn't going to wait for the request to finish before it continues on.

OTHER TIPS

actually i solved the issue you are right the code that i used is, it was a logical error . should have not used the if condition here is my code might be useful for someone trying

var currentWin = Ti.UI.currentWindow;
var sendit = Ti.Network.createHTTPClient();
sendit.open('GET', 'http://192.168.0.xxx/Remote_LBR/read.php');
sendit.send();
sendit.onload = function(){
    var json = JSON.parse(this.responseText);
    alert("json parsed");
    var json = json.tablename;

    var dataArray = [];

    var pos;
    alert("for loop started");

    // db.execute("DROP TABLE IF EXISTS tablename");

    for( pos=0; pos < json.length; pos++){

        dataArray.push({id:'' + json[pos].id + ''},{title:'' + json[pos].Title + ''}, {title:'' + json[pos].Latitude + ''}, {title:'' + json[pos].Longitude + ''}, {title:'' + json[pos].Description + ''}, {Approved:'' + json[pos].Approved + ''});
        // set the array to the tableView
        tableview.setData(dataArray);
     var db = Ti.Database.install('LBR_Local.sqlite','tablename_local');    


    db.execute('INSERT INTO tablename(id, Title, Latitude, Longitude, Description, Approved) VALUES("'+ json[pos].id +'", "'+ json[pos].Title +'", "'+ json[pos].Latitude +'", "'+ json[pos].Longitude +'", "'+ json[pos].Description +'", "'+ json[pos].Approved +'")');

    alert("Rows Inserted");

    };
    alert("for loop ended");

};

var tableview = Ti.UI.createTableView({
});

currentWin.add(tableview);
setData(dataArray);
Ti.App.addEventListener('reload',setData);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top