1
votes

I am writing a C++ application that embeds TCL and for its database operations I am also embedding SQLite in it. I would like to be able to do the following:

  1. Write TCL scripts for the embedded SQLite from the embedded TCL interpeter.
  2. Pass an SQLite connection from the embedded TCL interpreter to C++ and use it in C++ for db operations, as well as the other way around.

I would appreciate suggestions.

1
Do people still use TCL? Also, the question is way to broad. It's effectively your wish list only. - sehe
SQLite's own test suite is partly written in Tcl. See wiki.tcl.tk/2633 for some info on Tcl/SQLite integration. - Colin Macleod
Thanks @Colin, but I couldn't find any info on the page that could help me. - yaro

1 Answers

1
votes

Tcl 8.6 should ship with a build of the SQLite interface, the sqlite3 package. However, there's no (official) way to share a database connection from the package with your C++ code; there's simply no API in the package that you can call from C++ to get the connection. The official workaround is to create another connection from your C++ code and that shouldn't be too onerous unless you're doing weird mixing of things between queries in the two language bindings, a very much not normal use case.

You can hack it by using Tcl_GetCommandInfo() to retrieve, among other things, the Tcl database handle command's ClientData field. That can then be cast to a pointer to a structure whose first field is a sqlite3* handle, much as you'd obtain with sqlite3_open(). Which is messy and fragile. Also, you'd still need to respect the usual rules, such as needing to keep to a single thread. This really isn't what I'd recommend!