Search code examples
sqlitetclsqlitestudio

Populating SQLite table with Tcl script in SQLiteStudio


I'm trying to populate two INTEGER fields of a table in SQLite Studio with Tcl script feature of SQLiteStudio. The table itself is a representation of a m✕n rectangular matrix, where those two fields represent elements' indices.

CREATE TABLE matrix ( i INTEGER NOT NULL, j INTEGER NOT NULL, e REAL );

I've tried to recreate a surrogate for loop with the following script for the first field:

set i 0; #Initialization code
set i [expr {$i==100?1:$i+1}]; #Per step code

and this script for the second one:

set j 0; #Initialization code
set j [expr {$i==100?$j+1:$j}]; #Per step code

While the first script populates field i normally, the second one displays Error while executing populating code: can't read "i": no such variable error message, and the j field is populated with null values.

Is there any way to use SQLiteStudio's population mechanism this way (i.e. accessing field's new value from scope of another field's population Tcl script)?

REMARK

The final solution looked like this:

#1st script initialization code
set n_i 10
set i 0
#1st script per step code
set i [expr {$i%$n_i==0?1:$i+1}]
#2nd script initialization code
set n_i 10
set i 0
set j 1
#2nd script per step code
set i [expr {$i%$n_i==0?1:$i+1}]
set j [expr {$i%$n_i==0?$j+1:$j}]

It seems, that population Tcl script does not actually care about variable naming, and simply takes the last set variable's value to populate a field for a given row.


Solution

  • You cannot share variables between two scripts for populating two different columns (although it's a neat idea), but in your case you should be fine by copying script from first column (the one with i) and paste it before your current code for j. Sure, you will be calculating i twice, but it should get the job done.