ActionScript and SQLite parameters on Select using Like

此生再无相见时 提交于 2019-12-10 23:47:33

问题


I tried looking on google but without luck...

I have a SELECT SQLStatement and I want to use the LIKE operator but the parameters won't work and the query give me an error

    public function getUsersList(username:String):SQLStatement
    {           
        selectRecord= new SQLStatement();
        selectRecord.sqlConnection = connection;                                
        selectRecord.text =
            "SELECT id_user, username,password,profile,leg_cliente " +
            "FROM userlist  " +
            "WHERE username like '%:username%'";            
        selectRecord.parameters[":username"] = username;

        return selectRecord;
    }

The error I got is

':username' parameter name(s) found in parameters property but not in the SQL specified.


回答1:


I solved putting the wildcard % in the parameters instead of the statement...

        selectRecord.text =
            "SELECT id_user, username,password,profile,leg_cliente " +
            "FROM userlist  " +
            "WHERE username like :username";            
        selectRecord.parameters[":username"] = "%"+ username+"%";

The starting problem was triggered because the query was like

    selectRecord.text =
        "SELECT id_user, username,password,profile,leg_cliente " +
        "FROM userlist  " +
        "WHERE username like '%:username%'";    

Putting the single quote in the statement won't let the statement to set the parameter, I suppose because the parameter key (in statement.text) is seen as a text and not a parameter itself...




回答2:


This is a weird issue I've been stuck with for quite some time now. One solution I've used is like this:

var statementText:String="SELECT id_user, username,password,profile,leg_cliente " +
        "FROM userlist  " +
        "WHERE username like '%:username%'";

var params:Dictionary=new Dictionary();
params[":username"] = username;


for(var key:Object in params) {
    while(statementText.indexOf(key.toString()) >= 0) {
        statementText= statementText.replace(key, params[key]);
    }   
}

selectRecord.text = statementText;


来源:https://stackoverflow.com/questions/8384464/actionscript-and-sqlite-parameters-on-select-using-like

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!