查询在 Sqlite 中使用字符串进行。当变量传递给 Tcl 中的查询时,它会中断
我有一个带有“Toxi”模式的 Sqlite 笔记数据库(Notes 表、Tags 表、Note-has-Tag 表称为“fkeys”)。还有另一个线程涉及这个问题,以及它的查询,它非常详细。界面或“前端”是从 Tclsh 运行的 Tcl 脚本。如果我只有 1 个参数且没有 INTERSECT,Tcl 版本就可以正常工作。在 SqliteManager 中,INTERSECT 可以工作(但是 2 个参数被替换为文字字符串)。为什么它会破裂?首先是错误消息:
% can't read "rowid": no such variable
然后是代码:
proc gn {args} {
package require sqlite3
sqlite3 db jaysnotes.sqlite
set tagsofar [db eval {select tag_text from tag}]
puts "Tags so far: $tagsofar"
if {$args eq ""} {
puts "Enter 1 or more tags separated by spaces"
gets stdin taglist
} else {
set taglist $args}
set taglist [split $taglist " "]
# note (note_txt, timestamp)
# tag (tag_text)
# fkeys (note_id ,tag_id)
set srchtxt0 [lindex $taglist 0]
if {[llength $taglist] > 1} {
set srchtxt1 [lindex $taglist 1]
} else {set srchtxt1 $srchtxt0}
db eval {
SELECT DISTINCT n.rowid, n.note_txt, n.timestamp
FROM note n
JOIN fkeys f
ON n.rowid = f.note_id
JOIN tag t
ON t.rowid = f.tag_id
WHERE t.tag_text = $srchtxt0
INTERSECT
SELECT DISTINCT n.rowid, n.note_txt, n.timestamp
FROM note n
JOIN fkeys f
ON n.rowid = f.note_id
JOIN tag t
ON t.rowid = f.tag_id
WHERE t.tag_text = $srchtxt1
ORDER BY timestamp;} {puts "NOTE $rowid: $note_txt"
puts "DATE: $timestamp\n"}
}
I have a Sqlite notes database with a "Toxi" schema (Notes table, Tags table, Note-has-Tag table called "fkeys"). There is another thread that goes into this, and the querying of it, it great detail. The interface or "front end" is a Tcl script run from Tclsh. The Tcl version works just fine if I just have 1 arg and no INTERSECT. In SqliteManager INTERSECT works (but the 2 args are replaced with literal strings). Why does it break? First the error message:
% can't read "rowid": no such variable
then the code:
proc gn {args} {
package require sqlite3
sqlite3 db jaysnotes.sqlite
set tagsofar [db eval {select tag_text from tag}]
puts "Tags so far: $tagsofar"
if {$args eq ""} {
puts "Enter 1 or more tags separated by spaces"
gets stdin taglist
} else {
set taglist $args}
set taglist [split $taglist " "]
# note (note_txt, timestamp)
# tag (tag_text)
# fkeys (note_id ,tag_id)
set srchtxt0 [lindex $taglist 0]
if {[llength $taglist] > 1} {
set srchtxt1 [lindex $taglist 1]
} else {set srchtxt1 $srchtxt0}
db eval {
SELECT DISTINCT n.rowid, n.note_txt, n.timestamp
FROM note n
JOIN fkeys f
ON n.rowid = f.note_id
JOIN tag t
ON t.rowid = f.tag_id
WHERE t.tag_text = $srchtxt0
INTERSECT
SELECT DISTINCT n.rowid, n.note_txt, n.timestamp
FROM note n
JOIN fkeys f
ON n.rowid = f.note_id
JOIN tag t
ON t.rowid = f.tag_id
WHERE t.tag_text = $srchtxt1
ORDER BY timestamp;} {puts "NOTE $rowid: $note_txt"
puts "DATE: $timestamp\n"}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没关系,一旦我将两行更改为“as ....”,如下所示:
一切正常。抱歉浪费您的时间。希望有人受益。
Nevermind, as soon as I changed the 2 lines to have the "as ...." like so:
Everything worked. Sorry to waste your time. Hope someone benefits.