SQLite:通过一次替换复制前一行的所有值
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的意思是这不起作用?
您如何构建查询?
伪代码,例如,这不起作用,因为 othervalue 被解释为列名,
而这有效,因为 othervalue 周围的
"
包含在转义格式中,然后 sqllite 应该将其识别为表达式,而不是列名You mean this doesn't work?
How are you constructing your query?
pseudocode e.g. this won't work as othervalue is interpreted as columnname
while this works, because the
"
around othervalue are included in escaped format and then sqllite should recognize it as expression not anymore as columnname确保您已将字符串括在
'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.我不知道tcl是否支持参数化查询。 在 sqlite 中使用参数化查询具有三个优点。
I don't know whether tcl supports parameterized queries or not. Using parameterized queries in sqlite has three advantages.