Re: [SailfishDevel] Connect to 2 databases

2016-09-14 Thread Matthias Fehring
Am Donnerstag, 8. September 2016, 14:40:16 CEST schrieb Chris Walker:
> Can anybody point me in the direction of some C++ code to connect to
> two SQL databases please?

Does it have to be pure C++ code? If not and if it can be the Qt way, read on. 
:)

> I want to have one set as read-only and the other as read-write.
> 
> Whatever I'm doing is wrong as I always end up with a default
> connection.

Have a look at QSqlDatabase and it's addDatabase() method. [1]

Example for two SQLite databases:

You have to set a connection name for each database.

> QSqlDatabase rodb = QSqlDatabase::addDatabase(QStringLiteral("QSQLITE"), 
QStringLiteral("rodb"));
> rodb.setConnectOptions(QStringLiteral("QSQLITE_OPEN_READONLY");
> rodb.setDatabaseName(QStringLiteral("/full/path/to/ro_database.sqlite"));
>
> if (rodb.open()) {
>   QSqlQuery qro(rodb);
>   .
> }
>
> QSqlDatabase rwdb = QSqlDatabase::addDatabase(QStringLiteral("QSQLITE"), 
QStringLiteral("rwdb"));
> rwdb.setDatabaseName(QStringLiteral("/full/path/to/rw_database.sqlite"));
> if (rwdb.open()) {
>   QSqlQuery qrw(rwdb);
>   .
> }

Later on you can refer everywhere in your application to the databases by 
using the connection names (rodb and rwdb):

> QSqlDatabase rodb = QSqlDatabase::database(QStringLiteral("rodb"));
> QSqlQuery q(rodb);
> 


Best greetings
Matthias


[1] http://doc.qt.io/qt-5/qsqldatabase.html#addDatabase

-- 
Das Gesetz hat zum Schneckengang verdorben, was Adlerflug geworden wäre.
(Friedrich Schiller - Die Räuber)

Und der Buschfunk spielt gerade "Pikse Palve" von "In Extremo".

www.buschmann23.de
GPG-Key: 3A70A936614C3258
GPG Fingerprint: D786 DDF8 4CA9 00BC CDE0 9A5F CCC5 125D 6E87 D4FC

signature.asc
Description: This is a digitally signed message part.
___
SailfishOS.org Devel mailing list
To unsubscribe, please send a mail to devel-unsubscr...@lists.sailfishos.org

Re: [SailfishDevel] Connect to 2 databases

2016-09-14 Thread E.S. Rosenberg
Assuming you need SQLite since you are developing for SailfishOS the first
result here seems to be what you are looking for:
https://www.google.nl/search?q=c%2B%2B+sqlite

Instead of creating one SQLite object you create 2 or more, one for each
database though if you are opening a lot of databases you might want to
consider if they shouldn't all be different tables in the same database.

Of course you could also be more forthcoming about what exact db you're
trying to connect to and what you tried so that we don't do double work for
you, I believe as a community we need to be helpful etc. but as a member
looking for help I also need to do my due diligence, without that expect
short basic answers or none...

The general idea should be along these lines:

sqlite3 *db1, *db2;
sqlite_open(path1, );
sqlite_open(path2, );

Regards,
Eli

2016-09-14 18:03 GMT+03:00 Chris Walker <
cdw_noki...@the-walker-household.co.uk>:

> On Wed, 14 Sep 2016 00:19:02 +0300
> "E.S. Rosenberg"  wrote:
>
> > 2 different connection objects?
> >
> > 2016-09-08 16:40 GMT+03:00 Chris Walker <
> > cdw_noki...@the-walker-household.co.uk>:
> >
> > > Can anybody point me in the direction of some C++ code to connect to
> > > two SQL databases please?
>
> I'd like to point out the wording of my question? I said ".. point me in
> the direction of some code". So can anybody help with that please?
> ___
> SailfishOS.org Devel mailing list
> To unsubscribe, please send a mail to devel-unsubscribe@lists.
> sailfishos.org
>
___
SailfishOS.org Devel mailing list
To unsubscribe, please send a mail to devel-unsubscr...@lists.sailfishos.org

Re: [SailfishDevel] Connect to 2 databases

2016-09-14 Thread Chris Walker
On Wed, 14 Sep 2016 00:19:02 +0300
"E.S. Rosenberg"  wrote:

> 2 different connection objects?
> 
> 2016-09-08 16:40 GMT+03:00 Chris Walker <
> cdw_noki...@the-walker-household.co.uk>:
> 
> > Can anybody point me in the direction of some C++ code to connect to
> > two SQL databases please?

I'd like to point out the wording of my question? I said ".. point me in
the direction of some code". So can anybody help with that please?
___
SailfishOS.org Devel mailing list
To unsubscribe, please send a mail to devel-unsubscr...@lists.sailfishos.org