Npgsql AddWithValue不工作?

时间:2016-01-18 10:07:17

标签: c# postgresql npgsql

我有以下代码

cmd.CommandText = "SELECT * FROM product WHERE name LIKE '%@pattern%' OR description LIKE '%@pattern%' OR category LIKE '%@pattern%';";
cmd.Parameters.AddWithValue("pattern", pattern);

这会在我的代码中返回一个空的结果集。

但是,如果我像这样在PgAdmin中键入查询

SELECT * 
FROM product 
WHERE name LIKE '%otter%' 
   OR description LIKE '%otter%' 
   OR category LIKE '%otter%';

我做错了什么?

1 个答案:

答案 0 :(得分:3)

您搜索array_replace字符串文字。使用字符串连接:

def run_inference_on_image(image):
  """Runs inference on an image.
  Args:
    image: Image file name.
  Returns:
    Nothing
  """
  if not gfile.Exists(image):
    tf.logging.fatal('File does not exist %s', image)
  image_data = gfile.FastGFile(image, 'rb').read()

  # Creates graph from saved GraphDef.
  create_graph()

with tf.Session() as sess:
 # Some useful tensors:
 # 'softmax:0': A tensor containing the normalized prediction across
 #   1000 labels.
 # 'pool_3:0': A tensor containing the next-to-last layer containing 2048
 #   float description of the image.
 # 'DecodeJpeg/contents:0': A tensor containing a string providing JPEG
 #   encoding of the image.
 # Runs the softmax tensor by feeding the image_data as input to the graph.
 softmax_tensor = sess.graph.get_tensor_by_name('softmax:0')
 feature_tensor = sess.graph.get_tensor_by_name('pool_3:0') #ADDED
 predictions = sess.run(softmax_tensor,
                        {'DecodeJpeg/contents:0': image_data})
 predictions = np.squeeze(predictions)
 feature_set = sess.run(feature_tensor,
                        {'DecodeJpeg/contents:0': image_data}) #ADDED
 feature_set = np.squeeze(feature_set) #ADDED
 print(feature_set) #ADDED
 # Creates node ID --> English string lookup.
 node_lookup = NodeLookup()

 top_k = predictions.argsort()[-FLAGS.num_top_predictions:][::-1]
 for node_id in top_k:
   human_string = node_lookup.id_to_string(node_id)
   score = predictions[node_id]
   print('%s (score = %.5f)' % (human_string, score))

无论如何,以通配符开头的表达式不是SARGable所以它的性能会很差。考虑使用%@pattern%

当您将查询编写为:

cmd.CommandText = "SELECT * FROM product WHERE name LIKE '%' || @pattern || '%' OR description LIKE '%' || @pattern ||'%' OR category LIKE '%' || @pattern ||'%';";
cmd.Parameters.AddWithValue("pattern", pattern);

您希望{2}替换为FULL TEXT INDEX。但事实并非如此。它真正做的是搜索字符串文字 cmd.CommandText = "SELECT * FROM product WHERE name LIKE '%@pattern%' OR description LIKE '%@pattern%' OR category LIKE '%@pattern%';"; ,当然你没有在表格中。这就是你得到空结果集的原因。

使用字符串连接时,您将为参数的实际值添加通配符(开始/结束)。