c++ - QSqlDatabase check the data base already exist -
i need sore value on system locally , access later in table format, chose qsqldatabase. , first have check database exist. using below code getting message data base not exist creating new....
can problem ?
#include <qtcore/qcoreapplication> #include <qtsql/qsqldatabase> #include "qfile" #include "qdebug" int main(int argc, char *argv[]) { qcoreapplication a(argc, argv); qstring dbname = "lprdb"; qsqldatabase db = qsqldatabase::adddatabase("qsqlite"); if( qfile::exists(dbname)) { qdebug()<<"data base exist...."; } else { qdebug()<<"data base not exist creating new...."; db.setdatabasename(dbname); } return a.exec(); }
as noted @armatel, should open db, create db file.
#include <qtcore/qcoreapplication> #include <qtsql/qsqldatabase> #include "qfile" #include "qdebug" int main(int argc, char *argv[]) { qcoreapplication a(argc, argv); qstring dbname = "lprdb"; qsqldatabase db = qsqldatabase::adddatabase("qsqlite"); if( qfile::exists(dbname)) { qdebug()<<"data base exist...."; } else { qdebug()<<"data base not exist creating new...."; db.setdatabasename(dbname); db.open(); // <<< add this! if(!db.isopen()) { qdebug() << "error: not open database"; } qdebug() << "db opened"; } return a.exec(); }
Comments
Post a Comment