c++ - Why it connected continuously whether the path is correct or wrong -


i've written following code connect sqlite database file .

qsqldatabase db = qsqldatabase::adddatabase("qsqlite"); db.setdatabasename("c://database.db"); if(!db.open())     ui->label->settext(ui->label->text() + "failed connect database"); else     ui->label->settext(ui->label->text() + "connected."); 

when change correct path wrong path (ex: c://datafile.db or c:database.db), remain prints connected. text.

notice: if database file not exists, or correct path changed wrong path, create empty database file wrong path.

is there problem in code prints connected time?

if using sqlite always attempt create database file if can. may not want -- example, if need use data pre-existing database. should verify file exists on disk before trying open.

qsqldatabase db = qsqldatabase::adddatabase("qsqlite");  qfile file ("c:/database.db"); if (file.exists()) {   if(!db.open()) {     ui->label->settext(ui->label->text() + "failed connect database");   }   else {     ui->label->settext(ui->label->text() + "connected.");   } } else {   ui->table->settext("database not exist!"); } 

note forward slash / works platform, , less confusing having escape \ character. have better portability should replace hard coded c:/ qdir::root() or qdir::drives().


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -