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
Post a Comment