cant create table in Database



  • I have created a PSQL database using Qt by following code section

    QSqlDatabase db=QSqlDatabase::addDatabase("QPSQL");
        QSqlQuery query;
    
        QCoreApplication a(argc, argv);
    
        db.setPort(5432);
        db.setHostName("localhost");
        db.setUserName("postgres");
        db.setPassword("root");
        if(db.open())
        {
            if(!query.exec("CREATE DATABASE newdb"))
                {
                    qDebug()<<"Error creating DB:"<<db.lastError().text()<<endl;
                }
            else
                {
                    qDebug()<<"New Database created"<<endl;
                }
        }
        else
            qDebug()<<db.lastError().text()<<endl;
    
        db.close();
    
        if(db.open())
        {
               db.setDatabaseName("newdb");
               qDebug()<<"name of database:"<<db.databaseName()<<endl;
               if(query.exec("create table gateway "
                     "(macid varchar(17) primary key , "
                     "mode char, "
                     "status integer, "
                     "health char)"))
               {
                   qDebug()<<"gateway table created\n"<<endl;
               }
               else
               {
                   qDebug()<<"gateway table  failed\n"<<query.lastError().text()<<endl;
               }
        }
    

    but the problem is when i create table inside mydb database it is creating tables in default postgres database

    please help me


  • Moderators

    @Thejas Shouldn't you call db.setDatabaseName("newdb"); before db.open() ?
    See documentation http://doc.qt.io/qt-5/qsqldatabase.html#setDatabaseName
    "Sets the connection's database name to name. To have effect, the database name must be set before the connection is opened."



  • thanks man i missed that particular part in doc
    thank you.. :)

    here is my working code

        QSqlDatabase db=QSqlDatabase::addDatabase("QPSQL");
        QSqlQuery query;
    
        QCoreApplication a(argc, argv);
    
        db.setPort(5432);
        db.setHostName("localhost");
        db.setUserName("postgres");
        db.setPassword("root");
        if(db.open())
        {
            if(!query.exec("CREATE DATABASE newdb"))
                {
                    qDebug()<<"Error creating DB:"<<db.lastError().text()<<endl;
                }
            else
                {
                    qDebug()<<"New Database created"<<endl;
                }
        }
        else
            qDebug()<<db.lastError().text()<<endl;
    
        db.close();
        db.setDatabaseName("newdb");  /*it is the fix */
        if(db.open())
        {
    
    //           db.setDatabaseName("newdb");/*move to before db.open*/
    
               qDebug()<<"name of database:"<<db.databaseName()<<endl;
               if(query.exec("create table gateway "
                     "(macid varchar(17) primary key , "
                     "mode char, "
                     "status integer, "
                     "health char)"))
               {
                   qDebug()<<"gateway table created\n"<<endl;
               }
               else
               {
                   qDebug()<<"gateway table  failed\n"<<query.lastError().text()<<endl;
               }
        }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.