向Titanium添加查询

Adding Query to Titanium

本文关键字:查询 添加 Titanium      更新时间:2024-05-19

我使用Titanium列出特定产品中可用的数量,并使用Query"选择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进行解析。。。。。

重读你的问题后,你实际上在问几个关键问题。您在问我如何将数据输入数据库,然后如何对该数据库执行查询。

创建数据库:

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);