如何通过Node.js将动态字符串附加到Postgres中的文本类型列?

时间:2017-04-13 10:17:00

标签: sql node.js postgresql

我试图在updatePostgres from Node.js 文字类型列。我想在文本类型列的数据中添加动态字符串

但是我收到了错误

  

"错误:无法确定参数$ 1"

的数据类型



var pg = require('pg');

var connectionString = process.env.DATABASE_URL || "pg://admin:guest@localhost:5432/imagesearchexperiment";

var client = new pg.Client(connectionString);
client.connect();

client.query("DROP TABLE IF EXISTS imagesearchexperiment");
client.query("CREATE TABLE IF NOT EXISTS imagesearchexperiment(ipAddress varchar(64), searchqueries text)");

client.query("INSERT INTO imagesearchexperiment(ipAddress, searchqueries) VALUES($1, $2)",["ipAddress1","query1<>query2<>query3"]);
client.query("INSERT INTO imagesearchexperiment(ipAddress, searchqueries) VALUES($1, $2)",["ipAddress2","query1<>query2<>query3"]);
client.query("INSERT INTO imagesearchexperiment(ipAddress, searchqueries) VALUES($1, $2)",["ipAddress3","query1<>query2<>query3"]);

var searchQuery = "query4";
var ip = "ipAddress3";

// The following line is causing an error - error: could not determine data type of parameter $1
client.query("UPDATE imagesearchexperiment SET searchqueries = CONCAT($1, searchqueries) WHERE ipAddress = $2", [searchQuery, ip]);
&#13;
&#13;
&#13;

我已查看以下链接。

如果我没有动态字符串,他们的解决方案就有效。

  1. SQL Server - Adding a string to a text column (concat equivalent)
  2. How can I add text to SQL Column
  3. How to prepend a string to a column value in MySQL?
  4. How can I append a string to an existing field in MySQL?
  5. 请分享建议。谢谢:))

2 个答案:

答案 0 :(得分:0)

函数CONCAT不确定它获取的数据类型。显式类型转换为::text将有助于:

CONCAT($1::text, searchqueries)

答案 1 :(得分:0)

UPDATE imagesearchexperiment SET searchqueries = '"+ searchqueries="' WHERE ipAddress = "'+ ip+'"