SQLite:通过一次替换复制前一行的所有值

发布于 2024-07-25 09:51:38 字数 362 浏览 0 评论 0原文

使用 SQLite,我需要从表中复制几乎所有现有行,对单个列进行更改,然后将新行插入表中。 大致就像

INSERT INTO $tablename (c1, c2, ... , cn) 
    SELECT (c1, c2, ... , cn) FROM $tablenam

我尝试将某个值 ci 替换到 SELECT 列的列表中。 如果该值是浮点数或整数,则此方法有效,但如果它是字符串,则此方法无效。 如果该值是一个字符串,SQLite 会将其解释为一个列名,该列名自然不存在,并且我收到错误。

我可以编写代码来执行此操作,但我希望在一个查询中完成此操作。

我目前正在使用 Tcl 进行编码。

Using SQLite I need to copy nearly all of an existing row from a table, make a change to a single column, and insert the new row into the table. Something roughly like

INSERT INTO $tablename (c1, c2, ... , cn) 
    SELECT (c1, c2, ... , cn) FROM $tablenam

I tried substituting a some value ci into the the list of SELECT columns. This works if the value is a float or an integer, but not if it is a string. If the value is a string, SQLite interprets it as a column name, which naturally doesn't exist and I get an error.

I can write code to perform this operation, but I was hoping to do it in one query.

I am currently coding in Tcl.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧瑾黎汐 2024-08-01 09:51:39

你的意思是这不起作用?

INSERT INTO $tablename (c1, c2, ... , cn)
    SELECT (c1, c2, ..., "othervalue", ... , cn) FROM $tablename

您如何构建查询?

伪代码,例如,这不起作用,因为 othervalue 被解释为列名,

dothquery("INSERT INTO $tablename (c1, c2, ... , cn)
        SELECT (c1, c2, ..., othervalue, ... , cn) FROM $tablename")

而这有效,因为 othervalue 周围的 " 包含在转义格式中,然后 sqllite 应该将其识别为表达式,而不是列名

dothquery("INSERT INTO $tablename (c1, c2, ... , cn)
        SELECT (c1, c2, ..., \"othervalue\", ... , cn) FROM $tablename")

You mean this doesn't work?

INSERT INTO $tablename (c1, c2, ... , cn)
    SELECT (c1, c2, ..., "othervalue", ... , cn) FROM $tablename

How are you constructing your query?

pseudocode e.g. this won't work as othervalue is interpreted as columnname

dothquery("INSERT INTO $tablename (c1, c2, ... , cn)
        SELECT (c1, c2, ..., othervalue, ... , cn) FROM $tablename")

while this works, because the " around othervalue are included in escaped format and then sqllite should recognize it as expression not anymore as columnname

dothquery("INSERT INTO $tablename (c1, c2, ... , cn)
        SELECT (c1, c2, ..., \"othervalue\", ... , cn) FROM $tablename")
断爱 2024-08-01 09:51:39

确保您已将字符串括在 'singlequotes' 中。 我发现它们在 SQL 查询中引用字符串比双引号更有效。 确保他们也逃脱了。

Make sure you have surrounded the string in 'singlequotes'. I find they work better for quoting strings in SQL queries than doublequotes. Make sure they are escaped too.

给我一枪 2024-08-01 09:51:39

我不知道tcl是否支持参数化查询。 在 sqlite 中使用参数化查询具有三个优点。

  1. 它可以防止sql注入攻击。
  2. 您不必担心 ' 和 "。不再需要引用引号或转义引号。
  3. 速度更快,因为当您使用参数时,sqlite 不必重新解析每个 sql 语句。(有关性能示例,请参阅此链接< a href="https://stackoverflow.com/questions/904796/how-do-i-get-around-the-problem-in-sqlite-and-c/926251#926251">我如何解决“ '" sqlite 和 c# 中的问题?)。

I don't know whether tcl supports parameterized queries or not. Using parameterized queries in sqlite has three advantages.

  1. It prevents sql injection attacks.
  2. You don't have to worry about ' and ". No more quoting of quotes or escaping of quotes.
  3. It is faster because sqlite doesn't have to reparse every sql statement when you use parameters. (See this link for an performance example How do I get around the "'" problem in sqlite and c#? ).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文