【问题标题】:Adding Query to Titanium向 Titanium 添加查询
【发布时间】:2012-07-30 13:27:22
【问题描述】:

我正在使用 Titanium 来列出特定产品中可用的数量,并且我正在使用查询 "SELECT product_name, SUM(product_quantity)FROM mobile_product GROUP BY product_name"..

我在将这个查询实现到 Titanium JS 时感到困惑。

我的 Titanium 代码是


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 解析.....

【问题讨论】:

  • 有人可以帮我解决这个问题吗?????????
  • 评论对你没有帮助。这是钛合金桌面吗?

标签: javascript sql json titanium


【解决方案1】:

重读您的问题后,您确实在这里提出了几个关键问题。您在问我如何将我的数据放入数据库,然后如何对该数据库执行查询。

创建数据库:

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

【讨论】:

  • 我不是在谈论本地数据库...我在 PHPMyAdmin 中有一个数据库(远程数据库),并且想通过 JSON 解析执行查询并在我更改选择器时检索数据....
  • 如问题所述,如果您想使用 Titanium 对数据库执行查询,您可以这样做。如果您想在服务器上执行查询,该查询从未运行 Titanium 的服务器返回结果集,但实际上使用可能在 Web 服务器上运行的 PHP,那将是一个完全不同的问题。
猜你喜欢
  • 2017-05-26
  • 2021-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-02
  • 1970-01-01
相关资源
最近更新 更多