How to use ODB with QT
-
Hello all, as a bit of background, I'm a mechanical engineer trying to become a software developer and I'm running into a bit of an issue setting up tools for a database manager.
I know/understand that ODB is a great tool for managing databases (in my case sqlite) and looking at their downloads page:
https://www.codesynthesis.com/products/odb/download.xhtml
I believe I need to get the following zip files for my application:- odb-2.5.0-x86_64-windows10.
- libodb-2.5.0-x86_64-windows10-msvc17.10
- libodb-sqlite-2.5.0-x86_64-windows10-msvc17.10
- libodb-qt-2.5.0-x86_64-windows10-msvc17.10
Now that I have the zip files, what do I do next? I found a guide online here:
https://codesynthesis.com/products/odb/doc/install-windows.xhtml
That discusses how to install ODB for use with Visual Studio. However I do not have VS on this computer.Could someone walk me through the next steps? Any help would be greatly appreciated.
-
Hi and welcome :)
Regarding your topic title:
Qt provides drivers to connect to various SQL databases, including ODB via ODB ConnectorThat discusses how to install ODB for use with Visual Studio. However I do not have VS on this computer.
If you want to use these libraries in your Qt program you need Microsoft Visual Compiler v17 anyway (comes with Microsoft Visual Studio 22) since they were pre-built for/with msvc17 as the filenames tell.
When you already use QtCreator and a Qt Framework installation with MinGW, use the MinGW libraries.
But what is your ultimate goal?
Do you want to connect your Qt app to a SQL Database? Do you want to build your own driver?
I'm not 100% sure whether you need to build all this yourself to use Qt SQL, for example. -
Thanks!
The ultimate objective is to use an ORM to connect to what is now a sqlite database. However, it is very likely that the database will change from sqlite to something else.
As such I would like an abstraction layer that minimizes the rework required when I need to make the switch. I believe I need an ORM like ODB to do this, but if not, I'm happy to hear that.
-
@ATE-ENGE Qt already provides an abstraction layer, see https://doc.qt.io/qt-6/qtsql-index.html
When switching to another database you just need to change https://doc.qt.io/qt-6/qsqldatabase.html#addDatabase call. -
Ok, the program I inherited uses the QSqlDatabase::addDatabase to connect. However when interacting with the database, code like the following is used:
QList<QString> Ps; QString selectQuery = QString( "SELECT DISTINCT column.name " "FROM table " "WHERE col = :value" ); QSqlQuery query; query.prepare(selectQuery); query.bindValue(":value", value); if (query.exec()) { while (query.next()) { QString P; P = query.value("Properties.name").toString(); Ps.append(P); } }
My understanding is that ODB and other ORMs would allow me to instead write something more like:
using table_query = ODBquery<table>; for (QString& P: db.query<P> (table_query::value ="value);