database - Why does isOpen() function always return true? -


in constructor(qt 5.4.1 - windows 7):

qsqldatabase db = qsqldatabase::adddatabase("qsqlite"); db.setdatabasename(":memory:"); db.open();  qsqlquery q; q.exec("create table authors(num integer, birthdate date)"); q.exec("insert authors values('123', '2015-01-01')"); qdebug()<<"your info saved in db."; 

every things okay until but, later, need change db , save date permanently, so:

int dialog::saveinfospermanent() {      qsqldatabase::database().close();     if ( qsqldatabase::database().isopen ()) {         qdebug()<<"db open.";         return 1;     }      qsqldatabase db = qsqldatabase::adddatabase("qsqlite");     db.setdatabasename("newdb.db");     db.open();      qsqlquery q;     q.exec("create table authors(num integer, birthdate date)");     q.exec("insert authors values('123', '2015-01-01')");     qdebug()<<"your info saved in db.";      return 0; } 

and output:

your info saved in db. qsqlerror("", "", "") db open. 

so doing wrong? or there better other ideas changing db memory hard disk if user select checkbox?

for me best way initialize db connection use connection name:

qsqldatabase db = qsqldatabase::adddatabase("qsqlite", connectionname); 

with connection name have opportunity. removing database list of database connections, find docs here:

qsqldatabase::removedatabase(connectionname); 

get access database name, find docs here:

qsqldatabase db = qsqldatabase::database(connectionname); 

in case use removedatabase() connection name, don't forget scope of db. see example @ link attach.

you can use db same name in both case, docs says:

adds database list of database connections using driver type , connection name connectionname. if there exists database connection called connectionname, connection removed.

the database connection referred connectionname. newly added database connection returned.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -