Script Embedded SQLite With TCL - c++

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:
Write TCL scripts for the embedded SQLite from the embedded TCL interpeter.
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.

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!

Related

Design of data storage C++ application (maybe relational database)

I need to store and load some data in a C++ application. This data is basically going to end up as a set of tables as per a relational database.
I write the data to tables using something like csv format, then parse them myself and apply the database logic I need in my C++ code. But it seems stupid to reinvent the wheel with this and end up effectively writing my own database engine.
However, using something like a MySQL database seems like massive overkill for what is going to be a single user local system. I have tried setting up a MySQL daemon on my Windows system and I found it rather complex and possibly even impossible without admin privileges. It would be a serious obstacle to deployment as it would need each user's system to have MySQL set up and running.
Is there a reasonable middle ground solution? Something that can provide me with a simple database, accessible from C++, without all the complexities of setting up a full MySQL install?
NB. I have edited this question such that I hope it satisfies those who have voted to close the question. I am not asking for a recommendation for a tool, or someone's favourite tool or the best tools. That would be asking which database engine should I use. I am asking for what tools and design patterns are available to solve a specific programming problem - i.e. how can I get access to database like functionality from a C++ program, without writing my own database engine, nor setting up a full database server. This is conceptually no different to asking e.g. How do I print out the contents of a vector? - it's just a bigger problem. I have described the problem and what has been done so far to solve it. My understanding from the On Topic Page is that this is within scope.
You can try sqlite.
Here are some simple code examples: https://www.tutorialspoint.com/sqlite/sqlite_c_cpp.htm

Retrieve records according to a regex from a SQLite db in QT (C++)

I'm trying to retrieve records from a SQLite database using QSqlDatabase and QSqlQuery in QT/C++. My query would look like :
select * from tableName where columnName REGEXP 'regex'
Some sources says to create a function named REGEXP, but I couldn't find any clue related to QT/C++.
Can someone help me?
As of Qt version 5.10, REGEXP can be used with SQLite without implementing it yourself. However, it is disabled by default. To enable it, set the flag QSQLITE_ENABLE_REGEXP in the connection options.
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setConnectOptions("QSQLITE_ENABLE_REGEXP");
See documentation
As described here, you need to provide some native C/C++ code that exports regular expression power to SQL. You do that by calling the function sqlite3_create_function_v2, which is part of the SQL library written in C.
Since QSqlDatabase is a generic SQL wrapper designed to support a large number of SQL engines, the provided functions are the lowest common denominator of all of them. Thus you can not access sqlite3_create_function_v2 from Qt.
If you need to use REGEXP in your Qt application, you need to do use the plain C sqlite library in your entire project. A C++ wrapper around SQLite is advisable but from my own experience there is nothing that works great, which is why we are developing our own one. Digging into SQLite can be a lot of pain, especially when you start to combine different C++ build systems. So be warned and ask yourself, if you really really really need REGEXP.

Which are suitable local database (easy to handle with C++) for a small data set

The computer has already been installed ORACLE. But I didn't try ORACLE before, I just use sqlite...So, for now I want to create a database locally, and just insert one table. I want to use a test C++ program to read and write in this database. Is there something useful for using API about this? BTW, in the computer I saw SQL plus and SQL developer, but I didn't find the DBCA, which I know can be used to create table...help me, thanks a lot!!
BTW, my supervisor wants to me to test different kinds of database, which will be written in my report. So I want to test different kinds of databases, for the data is quite small, just 100 lines in a table is enough, but it will be applied in a big program, so I need to try different kinds of database locally, and not difficult for me to use C++ API...Because I just know little about database. I need some suggestions, thank a lot!
While I wouldn't recommend Oracle for handling small data sets, I do have a pointer to C++ API documentation.
http://www.oracle.com/pls/db112/portal.portal_db?selected=5&frame=
... and scroll to the bottom of the page for links to further information.
There's a choice of either using C++ with Oracle OCI API, or using C++ with Oracle Pro*C precompiler. The precompiler actually does produce code that uses the OCI API.
I have done some work with the Pro*C precompiler (using C as the implementation language, though, not C++), and it wasn't too bad. OCI tends to be quite low-level, but apparently writing direct OCI code has its uses, too.

Database in a C++ Program

I've got a C++ program that needs to deal with a lot of typical database problems - looking at tables, inserting and deleting values, searching for records. All of the database information has to be stored locally. Let me emphasise that - I don't want to communicate with a server, I want the information to be stored on the user's computer.
Are there any libraries that can easily implement all this functionality, preferably in a SQL style syntax? Or what are some ways to easily and robustly implement this functionality?
You can use embedded DB.
I think SQLite is one of the more popular ones.
My personal preference would be SOCI, with a SQLite backend.
http://soci.sourceforge.net/
http://soci.sourceforge.net/doc/backends/sqlite3.html
http://www.sqlite.org/

Simple IPC between C++ and Python (cross platform)

I have a C++ process running in the background that will be generating 'events' infrequently that a Python process running on the same box will need to pick up.
The code on the C side needs to be as lightweight as possible.
The Python side is read-only.
The implementation must be cross-platform.
The data being sent is very simple.
What are my options?
Thanks
zeromq -- and nothing else. encode the messages as strings.
However, If you want to get serialiazation from a library use protobuf it will generate classes for Python and C++. You use the SerializeToString() and ParseFromString() functions on either end, and then pipe the strings via ZeroMq.
Problem solved, as I doubt any other solution is faster, and neither will any other solution be as easy to wire-up and simple to understand.
If want to use specific system primitives for rpc such as named pipes on Windows and Unix Domain Sockets on unix then you should look at Boost::ASIO. However, unless you have (a) a networking background, and (b) a very good understanding of C++, this will be very time consuming
Use zeromq, it's about as simple as you can get.
Google's protobuf is a great library for RPC between programs. It generates bindings for Python and C++.
If you need a distributed messaging system, you could also use something like RabbitMQ, zeromq, or ActiveMQ. See this question for a discussion on the message queue libraries.
Another option is to just call your C code from your Python code using the ctypes module rather than running the two programs separately.
How complex is your data? If it is simple I would serialize it as a string. If it was moderately complex I would use JSON. TCP is a good cross-platform IPC transport. Since you say that this IPC is rare the performance isn't very important, and TCP+JSON will be fine.
You can use Google GRPC for this
I will say you create a DLL that will manage the communication between the two. The python will load DLL and call method like getData() and the DLL will in turn communicate with process and get the data.
That should not be hard.
Also you can use XML file or SQLite database or any database to query data. The daemon will update DB and Python will keep querying. There might be a filed for indicating if the data in DB is already updated by daemon and then Python will query.
Of course it depends on performance and accuracy factors!