2015-09-25 52 views
1

我想从一个数据库检索数据使用ajax调用,然后将数据放入插入查询,以便它可以添加到新的数据库。 (PHP的MySQL到phonegap本地数据库)。这是我需要结合的两个代码。 ajax代码当前只是输出到一个表中,我只是为了看到它正在工作。 javascript函数可以工作并添加到数据库中。如何添加/组合我的ajax调用输出到mysql插入查询?

jQuery(document).ready(function(){ 


jQuery.ajax({ 
    url : "http://cmlsys/toby/fetchdata.php", 
    type : "POST", 
    dataType : "json", 
    data : "param=no", 
    success : function (html){ 


     jQuery.each(html, function(key, value){ 

     $("table#DOM").append('<tr><td>'+value.CurrencyCode+'</td></tr>'); 

     }); 


    }, error : function (e){ 

     alert(e); 

    } 


}); 

});


function getEmployees(tx) { 
var sql = "select id, CurrencyCode " + "from employee"; 
tx.executeSql(sql, [], getEmployees_success); 

}

function populateDB(tx) { 
$('#busy').show(); 
tx.executeSql('DROP TABLE IF EXISTS employee'); 
var sql = 
    "CREATE TABLE IF NOT EXISTS employee ("+ 
    "id INTEGER PRIMARY KEY AUTOINCREMENT, " + 
    "CurrencyCode VARCHAR(50))"; 

tx.executeSql(sql); 


tx.executeSql("INSERT INTO employee (id,CurrencyCode) VALUES (1,'**THE AJAX RETURN**')"); 

}


这里是我的所有代码 -

var db; 
 
var dbCreated = false; 
 

 
var scroll = new iScroll('wrapper', { vScrollbar: false, hScrollbar:false, hScroll: false }); 
 

 
document.addEventListener("deviceready", onDeviceReady, false); 
 

 
function onDeviceReady() { 
 
    db = window.openDatabase("EmployeeDirectoryDB", "1.0", "PhoneGap Demo", 200000); 
 
    if (dbCreated) 
 
    \t db.transaction(getEmployees, transaction_error); 
 
    else 
 
    \t db.transaction(populateDB, transaction_error, populateDB_success); 
 
} 
 

 
function transaction_error(tx, error) { 
 
\t $('#busy').hide(); 
 
    alert("Database Error: " + error); 
 
} 
 

 
function populateDB_success() { 
 
\t dbCreated = true; 
 
    db.transaction(getEmployees, transaction_error); 
 
} 
 

 

 
function getEmployees(tx) { 
 
\t var sql = "select id, CurrencyCode " + "from employee"; 
 
\t tx.executeSql(sql, [], getEmployees_success); 
 
} 
 

 

 
function getEmployees_success(tx, results) { 
 
\t 
 
\t $('#busy').hide(); 
 
    var len = results.rows.length; 
 
    for (var i=0; i<len; i++) { 
 
    \t var employee = results.rows.item(i); 
 

 
\t \t $('#employeeList').append('<p class="line2">' + employee.CurrencyCode + '</p>'); 
 
    } 
 
\t 
 
\t setTimeout(function(){ 
 
\t \t scroll.refresh(); 
 
\t },100); 
 
\t db = null; 
 
} 
 

 

 

 
jQuery(document).ready(function() { 
 
     jQuery.ajax({ 
 
      url: "http://cmlsys/toby/fetchdata.php", 
 
      type: "POST", 
 
      dataType: "json", 
 
      data: "param=no", 
 
      success: function (html) {      
 
       populateDB(tx, html); 
 
       getEmployees(tx); 
 

 
      }, 
 
      error: function (e) { 
 
       alert(e); 
 
      } 
 
     }); 
 
    }); 
 

 
    function populateDB(tx, html) { 
 
     $('#busy').show(); 
 
     tx.executeSql('DROP TABLE IF EXISTS employee'); 
 
     var sql = 
 
      "CREATE TABLE IF NOT EXISTS employee (" + 
 
      "id INTEGER PRIMARY KEY AUTOINCREMENT, " + 
 
      "CurrencyCode VARCHAR(50))"; 
 

 
     tx.executeSql(sql); 
 
     var values = jQuery.map(html, function (val, i) { 
 
      return "('" + val.CurrencyCode + "')"; 
 
     }).join(','); 
 

 
     tx.executeSql("INSERT INTO employee (id,CurrencyCode) VALUES " + values); 
 
    } 
 

 
\t 
 
\t

+0

请帮助我的人所有的努力都失败了:( – rapidwaters

回答

1

您可以在成功关闭时将您的ajax成功数据传递给populateDB函数。请检查下面的代码。我猜你的tx是一个全局变量。

var db; 
var dbCreated = false; 

var scroll = new iScroll('wrapper', { 
    vScrollbar: false, 
    hScrollbar: false, 
    hScroll: false 
}); 

document.addEventListener("deviceready", onDeviceReady, false); 

function onDeviceReady() { 
    db = window.openDatabase("EmployeeDirectoryDB", "1.0", "PhoneGap Demo", 200000); 
    if (dbCreated) db.transaction(getEmployees, transaction_error); 
    else CallAjax(); 
} 

function transaction_error(tx, error) { 
    $('#busy').hide(); 
    alert("Database Error: " + error); 
} 

function populateDB_success() { 
    dbCreated = true; 
    db.transaction(getEmployees, transaction_error); 
} 


function getEmployees(tx) { 
    var sql = "select id, CurrencyCode from employee"; 
    tx.executeSql(sql, [], getEmployees_success); 
} 


function getEmployees_success(tx, results) { 

    $('#busy').hide(); 
    var len = results.rows.length; 
    for (var i = 0; i < len; i++) { 
     var employee = results.rows.item(i); 

     $('#employeeList').append('<p class="line2">' + employee.CurrencyCode + '</p>'); 
    } 

    setTimeout(function() { 
     scroll.refresh(); 
    }, 100); 
    db = null; 
} 


function CallAjax() { 
    jQuery.ajax({ 
     url: "http://cmlsys/toby/fetchdata.php", 
     type: "POST", 
     dataType: "json", 
     data: "param=no", 
     success: function (html) { 
      $('#busy').show(); 
      db.transaction(function (tx) { 
       tx.executeSql('DROP TABLE IF EXISTS employee'); 
       var sql = 
        "CREATE TABLE IF NOT EXISTS employee (" + 
        "id INTEGER PRIMARY KEY AUTOINCREMENT, " + 
        "CurrencyCode VARCHAR(50))"; 

       tx.executeSql(sql); 
       var values = jQuery.map(html, function (val, i) { 
        return "('" + val.CurrencyCode + "')"; 
       }).join(','); 

       tx.executeSql("INSERT INTO employee (id,CurrencyCode) VALUES " + values); 
      }, transaction_error, populateDB_success); 

     }, 
     error: function (e) { 
      alert(e); 
     } 
    }); 
} 
+0

谢谢你们的帮助,看起来不错,但我不希望任何被显示在成功我只是这样做是为了看看阿贾克斯在工作。我想从新数据库中检索我刚刚添加的代码 – rapidwaters

+0

@rapidwaters如果你不想看到任何显示内容,只需删除或注释表追加成功函数。 –

+0

@rapidwaters检查代码 –