PDA

View Full Version : Connect To Database



mustermann.klaus@gmx.de
15th May 2018, 12:25
Hi Everybody,

I run a program that connects to a database like this:



QSqlDatabase dB = QSqlDatabase::addDatabase("QODBC");
dB.setHostName("localhost");
dB.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DSN='';DBQ=C:/Program Files (x86)/myFolder/myAccesApp.mdb;");


This obviously refers to an *mdb-file on localhost. Now: I'd like to connect directly to the database and not go through the mdb-file. Here's a snippet off the regEdit:



[HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\SomeName]
"Driver"="%systemroot%\\System32\\SQLSRV32.DLL"
"Description"="SomeDescription"
"Server"="MYHOSTNAME"
"LastUser"="me"
"Trusted_Connection"="Yes"
"Database"="NameOfDatabase"



How (if) would I form a sufficient string for the setDatabaseName() - methode?
Or am I completely wrong and without *mdb there is no way?

My basic problem is, that if I run more than one instance of the program (all looking at the same database of course), the database goes nuts and writes completely irrelevant crap into the tables using the sql - "update" - command.
Perhaps there is another major blindspot on my side?

Every help highly appreciated.

Regards, Lars

d_stranz
16th May 2018, 00:40
the database goes nuts and writes completely irrelevant crap into the tables using the sql - "update" - command.
Perhaps there is another major blindspot on my side?

Seems pretty obvious that the way you are using the MDB driver doesn't support multiple concurrent write access if you end up with a corrupted database when you try to use it that way.

Here is a good Stackoverflow discussion of MDB concurrency (https://stackoverflow.com/questions/694921/ms-access-mdb-concurrency).


Now: I'd like to connect directly to the database and not go through the mdb-file.

I am not at all sure what you mean by this. The mdb file -is- the database.

mustermann.klaus@gmx.de
17th May 2018, 16:22
Hey d_stranz, nice to hearing from you again.

Yeah, this is what I think too. That's why my approach is to find out how this particular database really works. What is puzzling me is the fact, that the mdb-file itself is just of the size 7MB. Impossible to be a database. The file-type-description names it a windows-access-application.

Now:
If you skip the first part of my question and you only see the snippet of the ini-file posted earlier, you can come to the conclusion, that the mdb-file is just a vehicle to adress the data on

HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\SomeName]
isn't it?

The company runs this particular *.mdb on a lot of computers and they pretty much do a good job working on the same datasource/(database?) simultaniously.

As I explained much earlier, I'm not an expert on database-stuff. So, here's my question again, but hopefully more precise: If you wouldn't know about some mdb-file, could you connect to any database only from the information given by the snippet, shown here again?



HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\SomeName]
"Driver"="%systemroot%\\System32\\SQLSRV32.DLL"
"Description"="SomeDescription"
"Server"="MYHOSTNAME"
"LastUser"="me"
"Trusted_Connection"="Yes"
"Database"="NameOfDatabase"


regards, Lars

d_stranz
17th May 2018, 20:45
If you wouldn't know about some mdb-file, could you connect to any database only from the information given by the snippet

AFAIK, this registry entry simply tells the ODBC driver which specific DB access DLL to load when someone requests to open "SomeName" using ODBC. In essence, this is what your QSqlDatabase calls are doing - you've told it to use ODBC, and you've told it the name of the database, so the ODBC driver is going to out to the registry, look up this information, and open the database if it can.

Any program that uses the Microsoft ODBC driver will go through the same process, whether through Qt's layer, .NET, or some other database access layer. So unless you have code that will read MDB files directly, without going through an ODBC driver layer, you will be following this path too.

mustermann.klaus@gmx.de
29th May 2018, 17:39
Hey d_stranz,
sorry to come back to you too late. Was out of the office. This last post of you gave me a lot of homework. Step by step I begin now to understand databases. Hopefully I'll start liking it one day. The more I dig, the more I suffer ;-)
My root field of programming was data-acquisition and processing of vid/aud-data. This, now, is a good extension of my skills.
Thanks for that great support. Sure I'll return with new silly problems :-) Soon.

Lars