SQL 類似演算子: 構文と使用法
Note
この機能はでのみ利用可能です NCache Enterprise.
NCache を使用すると、SQL のようなクエリ形式を通じて列内の特定のパターンを検索できます。 LIKE オペレータ。
で使用される XNUMX つのワイルドカード LIKE 演算子は次のとおりです。
*
:文字列内のXNUMX個以上の文字の代わりに使用されます。
?
:文字列内のXNUMX文字の代わりに使用されます。
前提条件
構文
以下は、キャッシュを検索して、 商品名 文字から始まる シャンプ, カテゴリー 文字から始まる ハウスホー ExecuteReader
.
try
{
// Pre-condition: Cache is already connected
// Items are already present in the cache
// Specify the query with the specified criteria.
// Use the Fully Qualified Name (FQN) of your own custom class
string query = "SELECT * FROM FQN.Product WHERE ProductName LIKE ? AND Category LIKE ?";
// Use QueryCommand for query execution
var queryCommand = new QueryCommand(query);
// Providing parameters for query
queryCommand.Parameters.Add("ProductName", "P*");
queryCommand.Parameters.Add("Category", "Beverage?");
// Executing QueryCommand through ICacheReader
ICacheReader reader = _cache.SearchService.ExecuteReader(queryCommand);
// Check if result set is not empty
if (reader.FieldCount > 0)
{
while (reader.Read())
{
// Get the value of the result set
string result = reader.GetValue<string>("ProductID");
// Perform operations
}
}
else
{
// Null query result set retrieved
}
}
catch (OperationFailedException ex)
{
if (ex.ErrorCode == NCacheErrorCodes.INCORRECT_FORMAT)
{
// Make sure that the query format is correct
}
else
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
}
catch (Exception ex)
{
// Any generic exception like ArgumentException, ArgumentNullException
}
try
{
// Pre-condition: Cache is already connected
// Items are already present in the cache
// Specify the query with the specified criteria.
// Use the Fully Qualified Name (FQN) of your own custom class
String query = "SELECT * FROM FQN.Product WHERE ProductName LIKE ? AND Category LIKE ?";
// Use QueryCommand for query execution
QueryCommand queryCommand = new QueryCommand(query);
// Providing parameters for query
queryCommand.getParameters().put("ProductName", "P*");
queryCommand.getParameters().put("Category", " Beverage?");
// Executing QueryCommand through CacheReader
CacheReader reader = cache.getSearchService().executeReader(queryCommand);
// Check if result set is not empty
if (reader.getFieldCount() > 0)
{
while (reader.read())
{
// Get the value of the result set
String result = reader.getValue("productID", String.class);
// Perform operations
}
}
else
{
// Null query result set retrieved
}
}
catch (OperationFailedException ex)
{
if (ex.getErrorCode() == NCacheErrorCodes.INCORRECT_FORMAT)
{
// Make sure the query format is correct
}
else
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
}
catch (Exception ex)
{
// Any generic exception like IllegalArgumentException or NullPointerException
}
//This is an async method
try
{
// Pre-condition: Cache is already connected
// Items are already present in the cache
// Specify the query with the specified criteria.
// Use the Fully Qualified Name (FQN) of your own custom class
var query = "SELECT * FROM FQN.Product WHERE ProductName LIKE ? AND Category LIKE ?";
// Use QueryCommand for query execution
var queryCommand = new ncache.QueryCommand(query);
// Providing parameters for query
let parameter1 = new Map();
parameter1.set("ProductName", "P*");
queryCommand.setParameters(parameter1);
let parameter2 = new Map();
parameter2.set("Category", " Beverage?");
queryCommand.setParameters(parameter2);
// Executing QueryCommand through CacheReader
var searchService = await this.cache.getSearchService();
var reader = await searchService.executeReader(queryCommand);
// Check if result set is not empty
if (reader.getFieldCount() > 0)
{
while (reader.read())
{
var result = reader.getValue(1, Number());
// Perform operations
}
}
else
{
// Null query result set retrieved
}
}
catch (error)
{
// Handle errors
}
try:
# Pre-condition: Cache is already connected
# Items are already present in the cache
# Create a query which will be executed on the data set
query = "SELECT * FROM FQN.Product WHERE product_name LIKE ? AND category LIKE ?"
# Use QueryCommand for query execution
query_command = ncache.QueryCommand(query)
# Providing parameters for query
parameter1 = {"product_name": "Shamp*"}
parameter2 = {"category": "Househo*"}
query_command.set_parameters(parameter1)
query_command.set_parameters(parameter2)
# Executing query command through CacheReader
search_service = cache.get_search_service()
reader = search_service.execute_reader(query_command)
# Check if result set is not empty
if reader.get_field_count() > 0:
while reader.read():
result = reader.get_value(str, columnname="product_name")
# Perform operations
print(result)
else:
# None query results retrieved
print("Query result is None")
except Exception as exp:
# Handle errors
Note
操作がフェイルセーフであることを保証するために、で説明されているように、アプリケーション内の潜在的な例外を処理することをお勧めします。 失敗の処理.
その他のリソース
NCache SQL検索用のサンプルアプリケーションを提供します GitHubの.
も参照してください
。ネット: Alachisoft.NCache.クライアント.サービス 名前空間
Java: comの。alachisoft.ncache.ランタイムキャッシュ 名前空間
Node.js: キャッシュ とに提供されます。
Python: ncache.client.services とに提供されます。