yaro
yaro

Reputation: 33

Script Embedded SQLite With TCL

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.

Upvotes: 1

Views: 272

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137567

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!

Upvotes: 1

Related Questions