SQLite Forum

Can an application-defined function be used to convert one row of data to multiple rows and insert them into another table?
Login
Hello,

I'm trying to use the SQLite Tcl API to transform rows that contain binary text of a few sentences into rows of words and write the rows to a new table, after removing any prefixed/suffixed punctuation.

The code below, of course, doesn't work; one reason at least being that the procedure returns only one value, the list, instead of three values. 

    set sql {insert into table_2 (id, index, word) select transfunc(id, textdata) from table_1}
    db function transfunc -argcount 2 -deterministic -directonly -returntype text {SplitToWords}

    proc SplitToWords {id str} {
     global punc
     set listw {}
     lmap word [encoding convertfrom utf-8 $str] {
      lappend $listw {[incr i] [string map $punc $word]}
     }
     return $listw
    }

    db eval $sql

If the procedure is changed to write each row from within it, as below, it works. But is this the correct approach?
Thank you for any guidance you may be able to provide.

    set sql {select transfunc(id, textdata) from table_1}

    proc SplitToWords {id str} {
     global punc
     lmap word [encoding convertfrom utf-8 $str] {
      incr i
      set p [string map $punc $word]
      set sql {insert into table_2 (id, index, word) values($id, $i, $p)}"
      db eval $sql
     }
    }