odbc database connenction to access database hey guys, i got a little problem with the qodbc database connection. i have to open and use a access databasefile (.accdb) within my qt programm, but i can't get it run. i think there is a mista
odbc database connenction to access database
hey guys,
i got a little problem with the qodbc database connection. i have to open and use a access databasefile (.accdb) within my qt programm, but i can't get it run. i think there is a mistake in the database name but i am not that familiar with database handling so i need a little help with this. i started a simple mainwindow projekt and put all the database stuff in the main function. here is the code:
650) this.width=650; src=http://cdn.verydemo.com/upload/2013_05_01/13673573082180.png alt=> source code
123456789101112131415161718192021222324252627282930
#include #include mainwindow.h#include #include #include int main(int argc, char *argv[]){ qapplication a(argc, argv); mainwindow w; w.show(); qsqldatabase datab = qsqldatabase::adddatabase(qodbc); qstring databasename = driver={microsoft access driver (*.accdb)};fil={ms access};dbq=c://users/thor/pers/projekt madinger/datenbank/daten_hauptprogramm.accdb; datab.setdatabasename(databasename); if(!datab.open()) { datab.setconnectoptions(); return 1; } else return 0; return a.exec();}
i can run the project without any problems but the open() funktion alway returns false. maybe its just a noob mistake but it drives me crazy 650) this.width=650; src=http://cdn.verydemo.com/upload/2013_05_15/13686086275891.png alt=:cursing:> .
650) this.width=650; src=http://cdn.verydemo.com/upload/2013_05_15/13686086276822.png alt=go to the top of the page> 650) this.width=650; src=http://cdn.verydemo.com/upload/2013_05_15/13686086277843.png> quote
650) this.width=650; src=http://cdn.verydemo.com/upload/2013_05_15/13686086278744.png alt= title=thorsfeldherr is offline>thorsfeldherr
beginner
650) this.width=650; src=http://cdn.verydemo.com/upload/2013_05_15/13686086279705.jpg alt=>
650) this.width=650; src=http://cdn.verydemo.com/upload/2013_05_15/13686086280766.png alt= thorsfeldherr is male title=> 650) this.width=650; src=http://cdn.verydemo.com/upload/2013_05_15/13686086281657.png alt= thorsfeldherr started this thread title=>
posts: 3
location: bavaria/germany
occupation: master of engineering
650) this.width=650; src=http://cdn.verydemo.com/upload/2013_05_15/13686086282538.png alt=send private message title=send private message>
2
650) this.width=650; id=postedit117475 src=http://cdn.verydemo.com/upload/2013_05_15/13686086283449.png alt=>
friday, february 24th 2012, 5:53pm
ok now i am a little bit further. i debugged it and i cant connect because of this error:
[microsoft][odbc driver manager] der datenquellenname wurde nicht gefunden, und es wurde kein standardtreiber angegeben
in english: the data source name not found and no default driver specified
can you help me?
650) this.width=650; src=http://cdn.verydemo.com/upload/2013_05_15/13686086276822.png alt=go to the top of the page> 650) this.width=650; src=http://cdn.verydemo.com/upload/2013_05_15/13686086277843.png> quote
650) this.width=650; src=http://cdn.verydemo.com/upload/2013_05_15/13686086278744.png alt= title=thorsfeldherr is offline>thorsfeldherr
beginner
650) this.width=650; src=http://cdn.verydemo.com/upload/2013_05_15/13686086279705.jpg alt=>
650) this.width=650; src=http://cdn.verydemo.com/upload/2013_05_15/13686086280766.png alt= thorsfeldherr is male title=> 650) this.width=650; src=http://cdn.verydemo.com/upload/2013_05_15/13686086281657.png alt= thorsfeldherr started this thread title=>
posts: 3
location: bavaria/germany
occupation: master of engineering
650) this.width=650; src=http://cdn.verydemo.com/upload/2013_05_15/13686086282538.png alt=send private message title=send private message>
3
650) this.width=650; id=postedit117476 src=http://cdn.verydemo.com/upload/2013_05_15/13686086283449.png alt=>
friday, february 24th 2012, 7:38pm
ok i got it by myself. i post the solution in case anyone else has the same problem.
i use windows 7, 32 bit. if you want to use a database from type .mdb or .accdb with odbc you have to register the database as datasource before you can use it via c++ in any case. it is rather simple to to do:
you go into start->system
type odbc into the search field
the result is datasource(odbc)-> click it
under user-dsn you can add a new source
you have to choose a database (which is the one you want to use) and you can give it a sourcename for example yoursource
after you have done that you can call the database like that:
650) this.width=650; src=http://cdn.verydemo.com/upload/2013_05_01/13673573082180.png alt=> source code
123456
qsqldatabase datab = qsqldatabase::adddatabase(qodbc);qstring databasename = yoursource;datab.setdatabasename(databasename);datab.open();
now it works! 650) this.width=650; src=http://cdn.verydemo.com/upload/2013_05_15/136860862887219.png alt=:thumbsup:>本文来源:http://www.qtforum.org/article/37487/odbc-database-connenction-to-access-database.html