2014-09-25 28 views
0

這裏是我的代碼功能:WebStorage - 的ExecuteSQL給人不確定是不是在for循環

給定一個對象,我想在一個webStorage分貝插入一些數據。問題是,控制檯返回我一個「不確定是不是一個函數」,我不知道爲什麼

這裏是我的代碼

try { 
 
    db = openDatabase("mca", "1.0", "db mca", 200000); 
 
    db.transaction(function(tx) { 
 
    tx.executeSql("CREATE TABLE IF NOT EXISTS planning_x_material(id INTEGER PRIMARY KEY AUTOINCREMENT, planning_id INTEGER, material_id INTEGER, material_name TEXT)", []); 
 
    }, onDbError, fillDb); 
 
} catch(e) { 
 
    alert("webstorage error"); 
 
} 
 

 

 
function callback_planning_material(obj) { 
 
    console.log(obj); 
 
    
 
    db.transaction(function(tx) { 
 
    for(var k = 0; k < obj.length; k++) { 
 
     for (var i = 0; i < obj[k].material.length; i++) { 
 
     (function (k, i) { 
 
      var planning = obj[k]; 
 
      
 
      var material = planning.material[i]; 
 
      
 
      tx.execuseSql("INSERT INTO planning_x_material(planning_id, material_id, material_name) VALUES (?, ?, ?)", [planning.id, material.id, material.name]); // here console gives me "undefined is not a function" error 
 
     })(k, i); 
 
     } 
 
    } 
 
    }, onDbError); 
 
} 
 

 
function fillDb() { 
 
    // example object 
 
    var obj = [ 
 
    { 
 
     id: 1, 
 
     name: "name 1", 
 
     material: [ 
 
     { id: 1, name: "mat. 1" }, 
 
     { id: 2, name: "mat. 2" }, 
 
     ] 
 
    }, 
 
    
 
    { 
 
     id: 2, 
 
     name: "name 2", 
 
     material: [ 
 
     { id: 3, name: "mat. 3" }, 
 
     { id: 4, name: "mat. 4" }, 
 
     ] 
 
    } 
 
    ]; 
 
    
 
    
 
    callback_planning_material(obj); 
 
}
任何想法的一個片段?

+1

你'tx.ex ecuseSql',應該是'tx.executeSql'。 – RobH 2014-09-29 09:51:00

回答

1
  1. onDbError沒有定義
  2. txtx2
  3. 在插入executeSqlexecuseSql

應工作

try { 
    db = openDatabase("mca", "1.0", "db mca", 200000); 
    db.transaction(function(tx) { 
    tx.executeSql("CREATE TABLE IF NOT EXISTS planning_x_material(id INTEGER PRIMARY KEY AUTOINCREMENT, planning_id INTEGER, material_id INTEGER, material_name TEXT)", []); 
    }, onDbError, fillDb); 
} catch(e) { 
    console.log("webstorage error",e); 
} 

function onDbError(tx, err){ 
    console.log('There is error', err); 

} 


function callback_planning_material(obj) { 
    console.log(obj); 

    db.transaction(function(tx) { 
    for(var k = 0; k < obj.length; k++) { 
     for (var i = 0; i < obj[k].material.length; i++) { 
     (function (k, i) { 
      var planning = obj[k]; 

      var material = planning.material[i]; 

      tx.executeSql("INSERT INTO planning_x_material(planning_id, material_id, material_name) VALUES (?, ?, ?)", [planning.id, material.id, material.name]); // here console gives me "undefined is not a function" error 
     })(k, i); 
     } 
    } 
    }, onDbError); 
} 

function fillDb() { 
    // example object 
    var obj = [ 
    { 
     id: 1, 
     name: "name 1", 
     material: [ 
     { id: 1, name: "mat. 1" }, 
     { id: 2, name: "mat. 2" }, 
     ] 
    }, 

    { 
     id: 2, 
     name: "name 2", 
     material: [ 
     { id: 3, name: "mat. 3" }, 
     { id: 4, name: "mat. 4" }, 
     ] 
    } 
    ]; 


    callback_planning_material(obj); 
} 
+0

'tx2'是一個錯字,將我的文件轉換爲SO問題。 onDbError也在我的文件中定義,只是與這個問題無關。在'execuTeSql'上只是一個愚蠢的錯字我猜...我太愚蠢了!謝謝 - 在23小時前不能獎勵 – pistou 2014-09-29 10:04:02

1

tx2沒有定義,應該不是tx

tx.execuseSql("INSERT INTO planning_x_material(planning_id, material_id, material_name) VALUES (?, ?, ?)", [planning.id, material.id, material.name]); 
+0

是的錯字我的壞 – pistou 2014-09-29 09:49:12