2012-07-30 62 views
0

我使用鈦上市量的數量,可以在特定的產品,我現在用的查詢 「SELECT PRODUCT_NAME,SUM(product_quantity)FROM mobile_product GROUP BY PRODUCT_NAME」 ..添加查詢到鈦

我在將這個查詢實現給Titanium JS時感到困惑。

我二代碼


var currentWin = Ti.UI.currentWindow; 

var sendit = Ti.Network.createHTTPClient(); 
sendit.open('GET', 'http://localhost/mobileapp/productread.php'); 
sendit.send(); 


sendit.onload = function(){ 
var json = JSON.parse(this.responseText); 

var json = json.mobile_product; 

var picker = Ti.UI.createPicker(); 
// turn on the selection indicator (off by default) 
picker.selectionIndicator = true; 

var data = []; 
var pos; 
for (pos=0; pos < json.length; pos++) { 
data.push (Ti.UI.createPickerRow({title:''+ json[pos].product_name + '',custom_item:'b'})); 
} 
picker.add(data); 
currentWin.add(picker); 

//var rw = db.execute("SELECT product_name, SUM(product_quantity)FROM mobile_product GROUP BY product_name"); 

picker.addEventListener("change", function(e){ 
Ti.API.info(''+ json[pos].product_name + ''); 
}) 
}; 

請有人幫助我,我怎麼能使用此查詢在此代碼...我使用JSON從phpMyAdmin來解析....

+0

能有人幫我出這個????????? – user1562991 2012-07-30 14:06:03

+0

評論不會幫助你。這是鈦桌面? – 2012-07-30 15:51:39

回答

0

重讀你的問題後,你真的在​​這裏問幾個關鍵部分。您在問我如何將數據存入數據庫,然後如何針對該數據庫執行查詢。

創建數據庫:

var db = Ti.Database.open('MyDatabase'); 
db.execute('CREATE TABLE IF NOT EXISTS [mobile_product](id INTEGER, product_name TEXT, product_quantity INTEGER'); 
db.close(); 

裝載數據庫:

var json = JSON.parse(this.responseText); 

for(var i=0; json.length; i++){ 
var product = json[i]; 
addProductToDatabase(product); 
} 

function addProductToDatabase(_args){ 
    var db = Ti.Database.open('MyDatabase'); 
    var result = db.execute('INSERT INTO mobile_product(product_name, product_quantity) VALUES (?, ?)', _args.product_name, _args.product_quatity); 
    db.close(); 

} 

result = db.execute("SELECT product_name, SUM(product_quantity)FROM mobile_product GROUP BY product_name"); 
var myList = []; 
while(result.isValidRow()){ 
    myList.push({ 
    product_name: result.fieldByName('product_name'), 
    product_quantity: result.fieldByName('product_quantity') 
    }]; 
    result.next(); 
} 
result.close(); 
db.close(); 

var data = []; 
for (i=0; i<myList.length; i++) { 
data.push (Ti.UI.createPickerRow({title:''+ myList[i].product_name + '',custom_item:'b'})); 
} 
picker.add(data); 
+0

我不是在談論本地數據庫...我有一個數據庫在PHPMyAdmin(遠程數據庫),並想通過JSON執行查詢解析和檢索數據,當我改變選擇器.... – user1562991 2012-07-31 04:43:17

+0

如果你想執行一個查詢對於使用Titanium的數據庫來說,問題是這樣的,這就是你如何去做的。如果你想在一臺服務器上執行一個查詢,該服務器返回的是不運行Titanium的服務器的結果集,但事實上使用PHP可能運行在Web服務器上,這將是一個完全不同的問題。 – Martin 2012-07-31 13:21:33