如何在 Adobe Air 的 LIKE 運算符模式中使用命名參數 (How to use named parameters inside LIKE operator pattern in Adobe Air)


問題描述

如何在 Adobe Air 的 LIKE 運算符模式中使用命名參數 (How to use named parameters inside LIKE operator pattern in Adobe Air)

I wanted to use a named parameter placeholder inside the LIKE operator pattern so that the argument string is properly escaped.

Here's my modified code where I am using the at‑param placeholder:

  

var stmt = new air.SQLStatement(); 

     

stmt.text = "SELECT * FROM comments WHERE title LIKE '%@search%';";

     

stmt.parameters["@search"] = "argument string";

     

stmt.execute();

Doing so yields an SQLError with the following details

  

message: Error #3315: SQL Error.

     

details: '@search' parameter name(s) found in parameters property but not in the SQL specified

As suggested by Mike Petty, I tried:

  

stmt.text = 'SELECT * FROM comments WHERE title LIKE "@%search%";';

Which yields to the same SQL Error details.

Documentation has this:

  

expr ::= (column‑name | expr) LIKE pattern

     

pattern ::= '[ string | % | _ ]'

My suspicion is that it is skipped due to the qoutes, any ideas on how to make it work?

‑‑‑‑‑

參考解法

方法 1:

Found a solution for this, basically instead of doing it like this:

var stmt = new air.SQLStatement();
stmt.text = "SELECT * FROM comments WHERE title LIKE '%@search%';";
stmt.parameters["@search"] = "argument string";
stmt.execute();

You have to put a placeholder for the entire LIKE operator pattern and bind the pattern as a parameter.

var stmt = new air.SQLStatement();
stmt.text = "SELECT * FROM comments WHERE title LIKE @search;";
stmt.parameters["@search"] = "%" + userInput + "%";
stmt.execute();

方法 2:

Remove your :string from your string.

Like works just fine as:

SELECT * FROM comments WHERE comments.title LIKE '%string%';

方法 3:

It's hard to tell from your question, but is your current statement either throwing a SQLError, just doesn't compile, or just doesn't return any results?

If I had to guess I'd say you have a few issues here:

  1. You shouldn't have to qualify the column in the where clause since you only have 1 table to select from
  2. The parameter string is technically a reserved word by AIR / Actionscript (although the case is different) ‑ still a bit confusing.

Even though the documentation and code allow you to use either the colon, or at‑symbol, my preference is the @ since it's a bit easier to see ;)

Don't forget to add an itemClass definition of the Class you expect for rows ‑ this helps to avoid anonymous objects.

This should be fairly straight forward:

var stmt:SQLStatement = new SQLStatement();
    stmt.itemClass = Comment;
    stmt.text = 'SELECT * FROM comments WHERE title LIKE "@%search%";';
    stmt.parameters['@search'] = userInput.text;
    stmt.addEventListener(SQLEvent.RESULT, onResults);
    stmt.execute();

(by user1423172user1423172Steve RossMike Petty)

參考文件

  1. How to use named parameters inside LIKE operator pattern in Adobe Air (CC BY‑SA 3.0/4.0)

#air #adobe #Database #sqlite






相關問題

如何在 Adobe Air 的 LIKE 運算符模式中使用命名參數 (How to use named parameters inside LIKE operator pattern in Adobe Air)

Android 和 Flash Professional CS 5 Adobe Air Audio:流錯誤 (Android & Flash Professional CS 5 Adobe Air Audio: Stream Error)

可以從 web 視圖中的鏈接觸發我的 Air 應用程序中的操作嗎? (It's possible to trigger an action in my Air app, from a link in a webview?)

動作腳本、NativeProcess、resolvePath 和 swf 不起作用 (Action script, NativeProcess , resolvePath and swf does not work)

如何在創建新線程的同時停止運行線程並恢復同一個線程? (how to stop running thread and resume the same thread beside create new thread?)

我怎樣才能很好地在視圖堆棧之間製作動畫 (How can I nicely animate between viewstacks)

Adobe AIR 從 Flash 是一場大災難? (Adobe AIR from Flash a big disaster?)

“Adobe Flex”和使用 fl.controls 和 fl.events 庫的 AS3 應用程序有什麼區別? (what is the difference between "Adobe Flex" and an AS3 app using fl.controls and fl.events libraries?)

表面上的 Playbook AIR SDK 圖像 (Playbook AIR SDK images on surface)

Adobe AIR 應用程序音頻 (Adobe AIR application audio)

需要在 AIR 上設置 cookie 標頭,使用 SWFLoader 檢索遠程 Flex .swf 文件 (Need to set cookie header on AIR use of SWFLoader to retrieve remote Flex .swf file)

AIR 應用程序的 ActionScript 項目? (ActionScript Project to AIR Application?)







留言討論