Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. cant create table in Database
Forum Updated to NodeBB v4.3 + New Features

cant create table in Database

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Thejas
    wrote on last edited by
    #1

    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

    jsulmJ 1 Reply Last reply
    0
    • T Thejas

      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

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @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."

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • T Offline
        T Offline
        Thejas
        wrote on last edited by
        #3

        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;
                   }
            }
        
        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved