I have the following code:
set selectWeek {
SELECT strftime('%W', dayDate, '+1 day') AS WeekNo
, SUM(dayViews) AS WeekTotals
FROM dayViews
WHERE WeekNo >= ${startWeek}
AND WeekNo <= ${endWeek}
GROUP BY WeekNo
ORDER BY WeekNo
}
Only later the values for startWeek and endWeek are determined. When
they are determined, is there a possibility to have the right values substituted, or should I work with format?
I have the following code:
set selectWeek {
SELECT strftime('%W', dayDate, '+1 day') AS WeekNo
, SUM(dayViews) AS WeekTotals
FROM dayViews
WHERE WeekNo >= ${startWeek}
AND WeekNo <= ${endWeek}
GROUP BY WeekNo
ORDER BY WeekNo
}
Only later the values for startWeek and endWeek are determined. When
they are determined, is there a possibility to have the right values substituted, or should I work with format?
I have the following code:
set selectWeek {
SELECT strftime('%W', dayDate, '+1 day') AS WeekNo
, SUM(dayViews) AS WeekTotals
FROM dayViews
WHERE WeekNo >= ${startWeek}
AND WeekNo <= ${endWeek}
GROUP BY WeekNo
ORDER BY WeekNo
}
Only later the values for startWeek and endWeek are determined. When
they are determined, is there a possibility to have the right values substituted, or should I work with format?
On 23/10/2023 22:12, Cecil Westerhof wrote:
I have the following code:How are you executing the SQL?
set selectWeek {
SELECT strftime('%W', dayDate, '+1 day') AS WeekNo
, SUM(dayViews) AS WeekTotals
FROM dayViews
WHERE WeekNo >= ${startWeek}
AND WeekNo <= ${endWeek}
GROUP BY WeekNo
ORDER BY WeekNo
}
Only later the values for startWeek and endWeek are determined. When
they are determined, is there a possibility to have the right values
substituted, or should I work with format?
If you're using tdbc, then you can, and should, change your WHERE clause to
WHERE WeekNo >= :startWeek
AND WeekNo <= :endWeek
tdbc will substitute the values of the startWeek and endWeek variables
when it executes the query if they are in scope.If they aren't in scope,
then you can specify the values by passing a dictionary when you
evaluate the SQL text.
IMHO, If you aren't using tdbc, then you should be.
Alan Grunwald <[email protected]> writes:
How are you executing the SQL?
'package require sqlite3' and 'db eval …'
If you're using tdbc, then you can, and should, change your WHERE
clause to
WHERE WeekNo >= :startWeek
AND WeekNo <= :endWeek
That was it. I knew it could be done, but did not remember how.
I have the following code:
set selectWeek {
SELECT strftime('%W', dayDate, '+1 day') AS WeekNo
, SUM(dayViews) AS WeekTotals
FROM dayViews
WHERE WeekNo >= ${startWeek}
AND WeekNo <= ${endWeek}
GROUP BY WeekNo
ORDER BY WeekNo
}
Only later the values for startWeek and endWeek are determined. When
they are determined, is there a possibility to have the right values substituted, or should I work with format?
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
Cecil Westerhof <[email protected]> wrote:
Alan Grunwald <[email protected]> writes:
How are you executing the SQL?
'package require sqlite3' and 'db eval …'
If you're using tdbc, then you can, and should, change your WHERE
clause to
WHERE WeekNo >= :startWeek
AND WeekNo <= :endWeek
That was it. I knew it could be done, but did not remember how.
Sqlite's Tcl API supports three different ways to "substitute" (which
one to use depends upon exactly what it is that is in the variable).
The docs at: https://sqlite.org/tclsqlite.html explain all three in the description of the "eval" method.
Cecil Westerhof schrieb am Montag, 23. Oktober 2023 um 23:14:08 UTC+2:
I have the following code:
set selectWeek {
SELECT strftime('%W', dayDate, '+1 day') AS WeekNo
, SUM(dayViews) AS WeekTotals
FROM dayViews
WHERE WeekNo >= ${startWeek}
AND WeekNo <= ${endWeek}
GROUP BY WeekNo
ORDER BY WeekNo
}
Only later the values for startWeek and endWeek are determined. When
they are determined, is there a possibility to have the right values
substituted, or should I work with format?
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
Maybe to stupid, but why you don’t use quotes for your sql statement?
Cecil Westerhof schrieb am Montag, 23. Oktober 2023 um 23:14:08 UTC+2:
I have the following code:
set selectWeek {
SELECT strftime('%W', dayDate, '+1 day') AS WeekNo
, SUM(dayViews) AS WeekTotals
FROM dayViews
WHERE WeekNo >= ${startWeek}
AND WeekNo <= ${endWeek}
GROUP BY WeekNo
ORDER BY WeekNo
}
Only later the values for startWeek and endWeek are determined. When
they are determined, is there a possibility to have the right values
substituted, or should I work with format?
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
Maybe to stupid, but why you don’t use quotes for your sql statement?
Mole Cool <[email protected]> writes:
Cecil Westerhof schrieb am Montag, 23. Oktober 2023 um 23:14:08 UTC+2:
I have the following code:
set selectWeek {
SELECT strftime('%W', dayDate, '+1 day') AS WeekNo
, SUM(dayViews) AS WeekTotals
FROM dayViews
WHERE WeekNo >= ${startWeek}
AND WeekNo <= ${endWeek}
GROUP BY WeekNo
ORDER BY WeekNo
}
Only later the values for startWeek and endWeek are determined. When
they are determined, is there a possibility to have the right values
substituted, or should I work with format?
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
Maybe to stupid, but why you don’t use quotes for your sql statement?That is not going to work, because the variables do not exist yet, so
I will get:
WHERE WeekNo >=
AND WeekNo <=
That is not going to do what I want.
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
Alan Grunwald <[email protected]> writes:
IMHO, If you aren't using tdbc, then you should be.
I am not using tdbc. What are the advantages of it?
* Rich <[email protected]d>
| Cecil Westerhof <[email protected]> wrote:
| > Alan Grunwald <[email protected]> writes:
| >> How are you executing the SQL?
| >
| > 'package require sqlite3' and 'db eval …'
| >
| >> If you're using tdbc, then you can, and should, change your WHERE
| >> clause to
| >>
| >> WHERE WeekNo >= :startWeek
| >> AND WeekNo <= :endWeek
| >
| > That was it. I knew it could be done, but did not remember how.
| Sqlite's Tcl API supports three different ways to "substitute" (which
| one to use depends upon exactly what it is that is in the variable).
| The docs at: https://sqlite.org/tclsqlite.html explain all three in the
| description of the "eval" method.
These docs state that
db1 eval {INSERT INTO t1 VALUES(5,:bigstring)}
and
db1 eval {INSERT INTO t1 VALUES(5,$bigstring)}
are equivalent, so I wonder why the original approach did not work?
Cecil Westerhof schrieb am Mittwoch, 25. Oktober 2023 um 12:14:08 UTC+2:
Mole Cool <[email protected]> writes:
Cecil Westerhof schrieb am Montag, 23. Oktober 2023 um 23:14:08 UTC+2:That is not going to work, because the variables do not exist yet, so
I have the following code:
set selectWeek {
SELECT strftime('%W', dayDate, '+1 day') AS WeekNo
, SUM(dayViews) AS WeekTotals
FROM dayViews
WHERE WeekNo >= ${startWeek}
AND WeekNo <= ${endWeek}
GROUP BY WeekNo
ORDER BY WeekNo
}
Only later the values for startWeek and endWeek are determined. When
they are determined, is there a possibility to have the right values
substituted, or should I work with format?
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
Maybe to stupid, but why you don’t use quotes for your sql statement?
I will get:
WHERE WeekNo >=
AND WeekNo <=
That is not going to do what I want.
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
You should put the query in braces like {select ... where WeekNo >= $weekno}
package req sqlite3
sqlite3 db :memory:
proc sqlTolist {db sqltext} {
db eval $sqltext value {
set row [list]
foreach col $value(*) {
lappend row $value($col)
}
lappend rows $row
}
set rows [linsert $rows 0 $value(*)]
return $rows
}
proc sqlWbindTolist {db sqltext startWeek endWeek} {
db eval $sqltext value {
set row [list]
foreach col $value(*) {
lappend row $value($col)
}
lappend rows $row
}
set rows [linsert $rows 0 $value(*)]
return $rows
}
On 24/10/2023 15:16, Cecil Westerhof wrote:
Alan Grunwald <[email protected]> writes:
Big snip
IMHO, If you aren't using tdbc, then you should be.I am not using tdbc. What are the advantages of it?
I wasn't aware of how to handle variable substitution using the sqlite package; I guess that when I was using (before I discovered tdbc) I
didn't understand the security risks of constructing SQL text that
includes user-supplied values - referred to later in this thread.
To my mind the main point of using tdbc is that it's fairly agnostic
about the underlying database, so that if one day I need to switch my
code from running over sqlite to running over, say, MySQL I only (!?)
need to worry about inconsistencies in the SQL dialect; everything else
"just works" after I change the TCL command used to create the database connection.
These docs state that db1 eval {INSERT INTO t1 VALUES(5,:bigstring)} and
db1 eval {INSERT INTO t1 VALUES(5,$bigstring)} are equivalent, so I
wonder why the original approach did not work?
Schelte <[email protected]> writes:
On 25/10/2023 16:54, Ralf Fassel wrote:
These docs state that db1 eval {INSERT INTO t1 VALUES(5,:bigstring)}
and db1 eval {INSERT INTO t1 VALUES(5,$bigstring)} are equivalent, so
I wonder why the original approach did not work?
Because the OP insists on writing his variables as ${startWeek} etc. Had
he used $startWeek, it would have worked.
You do not know what you are talking about, that is completely b***t.
You may not like how I write my variables, but that does not mean it
is wrong.
On 25/10/2023 16:54, Ralf Fassel wrote:
These docs state that db1 eval {INSERT INTO t1 VALUES(5,:bigstring)}
and db1 eval {INSERT INTO t1 VALUES(5,$bigstring)} are equivalent, so
I wonder why the original approach did not work?
Because the OP insists on writing his variables as ${startWeek} etc. Had
he used $startWeek, it would have worked.
On 26/10/2023 00:38, Cecil Westerhof wrote:
Schelte <[email protected]> writes:It is wrong if you want sqlite to replace the variables. It only handles $variable, :variable, and @variable. Not ${variable}.
On 25/10/2023 16:54, Ralf Fassel wrote:You do not know what you are talking about, that is completely b***t.
These docs state that db1 eval {INSERT INTO t1 VALUES(5,:bigstring)}
and db1 eval {INSERT INTO t1 VALUES(5,$bigstring)} are equivalent, so
I wonder why the original approach did not work?
Because the OP insists on writing his variables as ${startWeek} etc. Had >>> he used $startWeek, it would have worked.
You may not like how I write my variables, but that does not mean it
is wrong.
Schelte <[email protected]> writes:
On 25/10/2023 16:54, Ralf Fassel wrote:
These docs state that db1 eval {INSERT INTO t1 VALUES(5,:bigstring)}
and db1 eval {INSERT INTO t1 VALUES(5,$bigstring)} are equivalent, so
I wonder why the original approach did not work?
Because the OP insists on writing his variables as ${startWeek} etc. Had
he used $startWeek, it would have worked.
You do not know what you are talking about, that is completely b***t.
You may not like how I write my variables, but that does not mean it
is wrong.
That works if you use it as a 'local' list, but that is not what I am
doing. I have the query in a variable and use:
db eval ${sqlCommand}
Then $variable does not work, but :variable does.
And at the same time it is secure.
Cecil Westerhof <[email protected]> writes:
Schelte <[email protected]> writes:
On 25/10/2023 16:54, Ralf Fassel wrote:
These docs state that db1 eval {INSERT INTO t1 VALUES(5,:bigstring)}
and db1 eval {INSERT INTO t1 VALUES(5,$bigstring)} are equivalent, so
I wonder why the original approach did not work?
Because the OP insists on writing his variables as ${startWeek} etc. Had >>> he used $startWeek, it would have worked.
You do not know what you are talking about, that is completely b***t.
You may not like how I write my variables, but that does not mean it
is wrong.
No reason for strong words.
The Tcl sqlite3 package (from tclsqlite3.c) takes everything after the $ including the enclosing braces, and passes it to Tcl_GetVar2Ex as the variable name.
The Tcl interpreter uses Tcl_ParseVarName which checks if the character
after the $ is a "{", and if it is takes everything between the matching braces as the variable name.
[email protected] writes:
The Tcl sqlite3 package (from tclsqlite3.c) takes everything after the $
including the enclosing braces, and passes it to Tcl_GetVar2Ex as the
variable name.
The Tcl interpreter uses Tcl_ParseVarName which checks if the character
after the $ is a "{", and if it is takes everything between the matching
braces as the variable name.
This is concerning the thread I started?
I would find it logical if it would work this way. If at one place
$variable is the same as ${variable}, I would expect it everywhere to
be the case.
But sadly it is not.
Tho show it I use this code:
puts "Before first select"
db eval { SELECT 37 } {}
set testVal 37
puts "Before second select"
db eval { SELECT $testVal } {}
puts "Before third select"
db eval { SELECT ${testVal} } {}
puts "After selects"
When I run it I get:
Before first select
Before second select
Before third select
unrecognized token: "$"
while executing
"db eval { SELECT ${testVal} } {}"
(file "/home/cecil/bin/ytWeek.tcl" line 52)
On 10/26/2023 9:31 AM, Cecil Westerhof wrote:sqlite3 has it's own language. It supports $, :, and @ however.
clt.to:
The Tcl sqlite3 package (from tclsqlite3.c) takes everything after the $ >> including the enclosing braces, and passes it to Tcl_GetVar2Ex as the
variable name.
The Tcl interpreter uses Tcl_ParseVarName which checks if the character >> after the $ is a "{", and if it is takes everything between the matching >> braces as the variable name.
This is concerning the thread I started?
I would find it logical if it would work this way. If at one place $variable is the same as ${variable}, I would expect it everywhere to
be the case.
But sadly it is not.
Tho show it I use this code:
puts "Before first select"
db eval { SELECT 37 } {}
set testVal 37
puts "Before second select"
db eval { SELECT $testVal } {}
puts "Before third select"
db eval { SELECT ${testVal} } {}
puts "After selects"
When I run it I get:
Before first select
Before second select
Before third select
unrecognized token: "$"
while executing
"db eval { SELECT ${testVal} } {}"
(file "/home/cecil/bin/ytWeek.tcl" line 52)
If you enclose your sqlite3 statement inside of braces, then tcl doesn't parse anything inside the braces, except to keep a level count of braces so it can find the final closing brace (with \{ or \} not counted).
That's it, everything else is passed on to sqlite3 as is. See rule 6 of the 12. So, it's up to sqlite3 to deal with the $ however it chooses to. And it does not (appear) to support the ${var} format that tcl does. So, it's not tcl's fault, since
[email protected] writes:
The Tcl sqlite3 package (from tclsqlite3.c) takes everything after the $
including the enclosing braces, and passes it to Tcl_GetVar2Ex as the
variable name.
The Tcl interpreter uses Tcl_ParseVarName which checks if the character
after the $ is a "{", and if it is takes everything between the matching
braces as the variable name.
This is concerning the thread I started?
I would find it logical if it would work this way. If at one place
$variable is the same as ${variable}, I would expect it everywhere to
be the case.
But sadly it is not.
et99 schrieb am Donnerstag, 26. Oktober 2023 um 19:30:18 UTC+2:sqlite3 has it's own language. It supports $, :, and @ however.
On 10/26/2023 9:31 AM, Cecil Westerhof wrote:
clt.to:If you enclose your sqlite3 statement inside of braces, then tcl doesn't parse anything inside the braces, except to keep a level count of braces so it can find the final closing brace (with \{ or \} not counted).
The Tcl sqlite3 package (from tclsqlite3.c) takes everything after the $ >>>> including the enclosing braces, and passes it to Tcl_GetVar2Ex as the
variable name.
The Tcl interpreter uses Tcl_ParseVarName which checks if the character >>>> after the $ is a "{", and if it is takes everything between the matching >>>> braces as the variable name.
This is concerning the thread I started?
I would find it logical if it would work this way. If at one place
$variable is the same as ${variable}, I would expect it everywhere to
be the case.
But sadly it is not.
Tho show it I use this code:
puts "Before first select"
db eval { SELECT 37 } {}
set testVal 37
puts "Before second select"
db eval { SELECT $testVal } {}
puts "Before third select"
db eval { SELECT ${testVal} } {}
puts "After selects"
When I run it I get:
Before first select
Before second select
Before third select
unrecognized token: "$"
while executing
"db eval { SELECT ${testVal} } {}"
(file "/home/cecil/bin/ytWeek.tcl" line 52)
That's it, everything else is passed on to sqlite3 as is. See rule 6 of the 12. So, it's up to sqlite3 to deal with the $ however it chooses to. And it does not (appear) to support the ${var} format that tcl does. So, it's not tcl's fault, since
characters enclosed in parentheses (u0028 and u0029).
from
https://www.sqlite.org/draft/tokenreq.html
Variables are used as placeholders in SQL statements for constant values that are to be bound at start-time.
H40310: SQLite shall recognize as a VARIABLE token the a question-mark (u003f) followed by zero or more NUMERIC characters.
A "parameter name" is defined to be a sequence of one or more characters that consists of ALPHANUMERIC characters and/or dollar-signs (u0025) intermixed with pairs of colons (u003a) and optionally followed by any sequence of non-zero, non-WHITESPACE
Because I do not use a 'local' list, but a 'global' string variable:
db1 eval ${commandStr}
Cecil Westerhof <[email protected]> writes:
[email protected] writes:
The Tcl sqlite3 package (from tclsqlite3.c) takes everything after the $ >>> including the enclosing braces, and passes it to Tcl_GetVar2Ex as the
variable name.
The Tcl interpreter uses Tcl_ParseVarName which checks if the character
after the $ is a "{", and if it is takes everything between the matching >>> braces as the variable name.
Harald Oehlmann <[email protected]> wrote:
If your variable data contains quotes, it must be handled.
That is, how SQL Injection attacks work.
...
To avoid this, use the ":" syntax. It cares about correct quoting.
I don’t get your point, if it gets substituted then there is no
different if you use $CustId or :CustId because your data contains an
sql statement.
The whole text has a couple of errors and misleading statements!
For example:
select item from things where color = 'purple''; drop table things;';
The statement above will search for "purple'; drop table things;" and
will NOT drop the table!
select item from things where color = 'purple''; drop table things;';
And as you likely don't have a color named "purple'; drop table things;"
zero rows return instead of your things table being deleted.
... where color = :var -- where are the single quotes surrounding :var
Correct subs with ':var' ... where color = 'purple'; drop table things;'
You will get an unrecognized token: !
| Sysop: | Keyop |
|---|---|
| Location: | Huddersfield, West Yorkshire, UK |
| Users: | 715 |
| Nodes: | 16 (3 / 13) |
| Uptime: | 146:37:13 |
| Calls: | 12,091 |
| Calls today: | 4 |
| Files: | 15,000 |
| Messages: | 6,517,509 |