从 Titanium Appcelerator 数据库列获取值
我环顾四周,但似乎找不到我想要做什么。它应该相当简单......
我有一个像这样设置的数据库表:
var db = Ti.Database.open('playerInfo.db');
db.execute('CREATE TABLE IF NOT EXISTS playersTable (id INTEGER PRIMARY KEY, name TEXT NOT NULL, "50" INTEGER, "25" INTEGER )');
我有两个按钮,其分配值分别为 25 和 50。每个按钮都有一个“值”键,我可以在其中分配它们的值。我正在努力完成三件事: 当按下按钮时,找到相应值的列。 将此列的值增加 1。 检索新值并控制台记录它。
这就是按下按钮时我的代码的样子:
var rows = db.execute("SELECT '" + button.value + "' FROM playersTable WHERE name= '" + owner + "'");
var imagesString = rows.fieldByName(button.value);
Ti.API.debug(imagesString)
这一切都在单击事件侦听器中,其中变量“owner”作为字符串传入。
这是我得到的错误:
message = "Attempted to access unknown result column 25";
我没有太多的 sql 经验,所以我不确定我做对了什么,做错了什么。任何帮助表示赞赏!
谢谢。
I've looked around everywhere, but I can't seem to find exactly what I'm trying to do. It should be fairly simple...
I have a db table set up like this:
var db = Ti.Database.open('playerInfo.db');
db.execute('CREATE TABLE IF NOT EXISTS playersTable (id INTEGER PRIMARY KEY, name TEXT NOT NULL, "50" INTEGER, "25" INTEGER )');
I have two buttons with an assigned value of 25, and 50, respectively. Each button has a "value" key, where I assign their values. I am trying to accomplish three things:
When a button is pressed, find the column of corresponding value.
increase the value of this column by 1.
Retrieve the new value and console log it.
This is what my code looks like when a button is pressed:
var rows = db.execute("SELECT '" + button.value + "' FROM playersTable WHERE name= '" + owner + "'");
var imagesString = rows.fieldByName(button.value);
Ti.API.debug(imagesString)
This is all in a click event listener where the variable "owner" is passed in as a string.
This is the error I get:
message = "Attempted to access unknown result column 25";
I don't have too much experience with sql, so I'm not sure what I'm doing right and what I'm doing wrong. Any help is appreciated!
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不太清楚问题是什么,但以下内容对我有用。注意“?”变量替换语法确保 MySQL 正确引用值:
如果您收到“未找到行”错误,则可能您的数据一开始就没有正确插入。以下是我为玩家“foo”插入测试行的方法:
我认为这应该可以解决您的问题。如果这对您不起作用,请告诉我。
I'm not sure quite exactly what the problem is, but the following works for me. Note that the "?" variable substitution syntax makes sure that the values are quoted properly for MySQL:
If you get the row not found error, it's possible your data isn't getting inserted properly in the first place. Here's how I inserted my test row for player "foo":
I think this should solve your problem. Let me know if this doesn't work for you.