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. there is no data in the connection error. When you connect to a database, you can't create one if it doesn't exist?
Forum Updated to NodeBB v4.3 + New Features

there is no data in the connection error. When you connect to a database, you can't create one if it doesn't exist?

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 565 Views 2 Watching
  • 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.
  • J Offline
    J Offline
    Jason Xu
    wrote on last edited by
    #1

    The MYSQL plug-in is installed to connect to an existing database. But there is no data in the connection error. When you connect to a database, you can't create one if it doesn't exist?

    jsulmJ 1 Reply Last reply
    0
    • J Jason Xu

      The MYSQL plug-in is installed to connect to an existing database. But there is no data in the connection error. When you connect to a database, you can't create one if it doesn't exist?

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

      @Jason-Xu said in there is no data in the connection error. When you connect to a database, you can't create one if it doesn't exist?:

      But there is no data in the connection error

      Can you please explain better?
      What data? What error? If you get an error please post it here...

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

      1 Reply Last reply
      0
      • J Offline
        J Offline
        Jason Xu
        wrote on last edited by Jason Xu
        #3

        When there is a Test database in the database, the application can connect normally. However, if a test database is not established in advance, the application will fail to connect。

        There is no Student database in the database. The program failed to connect to the database
        connet_error.png

        The Student database is included in the database. The program connects to the database successfully
        connet_ok.png

        Can't Qt create databases using mysql????,
        If not how to create a new database?

        JonBJ 1 Reply Last reply
        0
        • J Jason Xu

          When there is a Test database in the database, the application can connect normally. However, if a test database is not established in advance, the application will fail to connect。

          There is no Student database in the database. The program failed to connect to the database
          connet_error.png

          The Student database is included in the database. The program connects to the database successfully
          connet_ok.png

          Can't Qt create databases using mysql????,
          If not how to create a new database?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @Jason-Xu
          Your error message "Unknown database 'student'" was issued when there was indeed no such database, and so was correct. Once it was created it then connected, which is also correct.

          Can't Qt create databases using mysql????,

          Of course! You saw that you created the database in mysql by asking it to execute the statement CREATE DATABASE student, so if you want to do that from Qt use QSqlQuery::exec() to execute that statement.

          1 Reply Last reply
          1
          • J Offline
            J Offline
            Jason Xu
            wrote on last edited by
            #5

            @JonB said in there is no data in the connection error. When you connect to a database, you can't create one if it doesn't exist?:

            CREATE DATABASE student

            qslDabe = QSqlDatabase::addDatabase("QMYSQL");
            qslDabe.setHostName("localhost"); //本地数据库
            qslDabe.setPort(3306);//端口号
            qslDabe.setUserName("root"); //登录用户
            qslDabe.setPassword("123456");//登录密码
            qslDabe.setDatabaseName("student");//使用的数据库的table
            if(!qslDabe.open())
            {
                qDebug()<<"连接数据库错误"<<qslDabe.lastError()<<endl;
                
                return ;
            }
            else
            {
                qDebug()<<"连接数据库成功"<<endl;
            }
            
            QSqlQuery query(qslDabe);
            query.exec("CREATE DATABASE student");
            

            QslDabe connection failed, query.exec cannot execute. So when you first connect to the DATABASE, if the data does not exist, how do you CREATE the data using "query.exec("CREATE DATABASE Student ")" and then reconnect.

            JonBJ piervalliP 2 Replies Last reply
            0
            • J Jason Xu

              @JonB said in there is no data in the connection error. When you connect to a database, you can't create one if it doesn't exist?:

              CREATE DATABASE student

              qslDabe = QSqlDatabase::addDatabase("QMYSQL");
              qslDabe.setHostName("localhost"); //本地数据库
              qslDabe.setPort(3306);//端口号
              qslDabe.setUserName("root"); //登录用户
              qslDabe.setPassword("123456");//登录密码
              qslDabe.setDatabaseName("student");//使用的数据库的table
              if(!qslDabe.open())
              {
                  qDebug()<<"连接数据库错误"<<qslDabe.lastError()<<endl;
                  
                  return ;
              }
              else
              {
                  qDebug()<<"连接数据库成功"<<endl;
              }
              
              QSqlQuery query(qslDabe);
              query.exec("CREATE DATABASE student");
              

              QslDabe connection failed, query.exec cannot execute. So when you first connect to the DATABASE, if the data does not exist, how do you CREATE the data using "query.exec("CREATE DATABASE Student ")" and then reconnect.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @Jason-Xu
              Of course the attempt to connect specifying student as the database name will fail to open if the database does not exist. Look at the mysql command you used (mysql -u root -p) when it did not exist and you intended to create it: that command did not specify student as the database to connect to and nor should you from Qt.

              Depending on how mysql works, do not call setDatabaseName(), call it with maybe "" as the database name, or perhaps you need to pass in an existing system database, such as sys or mysql.

              1 Reply Last reply
              1
              • J Jason Xu

                @JonB said in there is no data in the connection error. When you connect to a database, you can't create one if it doesn't exist?:

                CREATE DATABASE student

                qslDabe = QSqlDatabase::addDatabase("QMYSQL");
                qslDabe.setHostName("localhost"); //本地数据库
                qslDabe.setPort(3306);//端口号
                qslDabe.setUserName("root"); //登录用户
                qslDabe.setPassword("123456");//登录密码
                qslDabe.setDatabaseName("student");//使用的数据库的table
                if(!qslDabe.open())
                {
                    qDebug()<<"连接数据库错误"<<qslDabe.lastError()<<endl;
                    
                    return ;
                }
                else
                {
                    qDebug()<<"连接数据库成功"<<endl;
                }
                
                QSqlQuery query(qslDabe);
                query.exec("CREATE DATABASE student");
                

                QslDabe connection failed, query.exec cannot execute. So when you first connect to the DATABASE, if the data does not exist, how do you CREATE the data using "query.exec("CREATE DATABASE Student ")" and then reconnect.

                piervalliP Offline
                piervalliP Offline
                piervalli
                wrote on last edited by
                #7

                @Jason-Xu
                In mysql you have the system database with name "mysql ". Use can use the db "mysql" for the connection , than run the "CREATE DATABADE student IF NOT EXISTS "

                Link
                https://dev.mysql.com/doc/refman/8.0/en/create-database.html

                1 Reply Last reply
                1
                • J Offline
                  J Offline
                  Jason Xu
                  wrote on last edited by
                  #8

                  @jsulm
                  @JonB
                  @piervalli

                  Thank you very much for your reply! It works very well!!

                  JonBJ 1 Reply Last reply
                  0
                  • J Jason Xu

                    @jsulm
                    @JonB
                    @piervalli

                    Thank you very much for your reply! It works very well!!

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #9

                    @Jason-Xu
                    Out of interest, for my future reference, did you find you could specify "no database" or did you specify perhaps mysql for the database?

                    J 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @Jason-Xu
                      Out of interest, for my future reference, did you find you could specify "no database" or did you specify perhaps mysql for the database?

                      J Offline
                      J Offline
                      Jason Xu
                      wrote on last edited by Jason Xu
                      #10

                      @JonB said in there is no data in the connection error. When you connect to a database, you can't create one if it doesn't exist?:

                      Out of interest, for my future reference, did you find you could specify "no database" or did you specify perhaps mysql for the database?

                      "SetDatabaseName()" do not specify the database name, "qsldape.open ()" will execute successfully and then create the specified database

                      qslDabe = QSqlDatabase::addDatabase("QMYSQL");
                      qslDabe.setHostName("localhost"); //本地数据库
                      qslDabe.setPort(3306);//端口号
                      qslDabe.setUserName("root"); //登录用户
                      qslDabe.setPassword("123456");//登录密码
                      qslDabe.setDatabaseName("student");
                      
                      if(!qslDabe.open())
                      {
                          qDebug()<<"student 连接数据库错误"<<qslDabe.lastError()<<endl;
                      
                          qDebug()<<"开始连接默认数据库"<<endl;
                          qslDabe.setDatabaseName("");
                          if(!qslDabe.open())
                          {
                              qDebug()<<"默认数据库连接错误!"<<qslDabe.lastError()<<endl;
                              return;
                          }
                          else
                          {
                              qDebug()<<"默认数据库连接成功"<<endl;
                          }
                      
                          qDebug()<<"创建student数据库"<<endl;
                          qslDabe.exec("CREATE DATABASE IF NOT EXISTS student");
                      
                          qslDabe.close();
                          qslDabe.setDatabaseName("student");
                          if(!qslDabe.open())
                          {
                              qDebug()<<"student数据库连接错误!"<<qslDabe.lastError()<<endl;
                              return;
                          }
                          else
                          {
                              qDebug()<<"student数据库连接成功"<<endl;
                          }
                      }
                      else
                      {
                          qDebug()<<"连接数据库成功"<<endl;
                      }
                      
                      1 Reply Last reply
                      1

                      • Login

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