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. QSqlError("1045", "QMYSQ") Cannot edit table with QSqlTableModel
QtWS25 Last Chance

QSqlError("1045", "QMYSQ") Cannot edit table with QSqlTableModel

Scheduled Pinned Locked Moved Unsolved General and Desktop
qsqltablemodelqsqlerrorerror 1045mysqllinux desktop
9 Posts 3 Posters 953 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.
  • S Offline
    S Offline
    Shellcpp73
    wrote on last edited by VRonin
    #1

    But I have password to this user and in mysql console I can login but to here when i try to change data in DB with QSqlTablemodel

    QSqlError("1045", "QMYSQL: Невозможно соединиться", "Access denied for user 'myuser'@'localhost' (using password: NO)")

    entity.connectToDb();
    if(entity.db.open())
    
    {
    
       // entity.querymodel->setQuery("SELECT * FROM telcom");
       // ui->tableView->setModel(entity.querymodel);
      entity.model = new QSqlTableModel(this,entity.db);
      entity.model->setEditStrategy(QSqlTableModel::OnManualSubmit);
      entity.model->setTable("telcom");
      entity.model->select();
      this->ui->tableView->setModel(entity.model);
    
    }
    

    I have password for 'myuser'@'localhost'. QSqldriver always tries to connect with password NO but if set a password NO in mysql for 'myuser'@'localhost' I have other error message "query does not exixst" so i think that's impossible to connect to phpmyadmin DB with no password. My local user have all needed privileges.

    JonBJ 1 Reply Last reply
    0
    • S Shellcpp73

      But I have password to this user and in mysql console I can login but to here when i try to change data in DB with QSqlTablemodel

      QSqlError("1045", "QMYSQL: Невозможно соединиться", "Access denied for user 'myuser'@'localhost' (using password: NO)")

      entity.connectToDb();
      if(entity.db.open())
      
      {
      
         // entity.querymodel->setQuery("SELECT * FROM telcom");
         // ui->tableView->setModel(entity.querymodel);
        entity.model = new QSqlTableModel(this,entity.db);
        entity.model->setEditStrategy(QSqlTableModel::OnManualSubmit);
        entity.model->setTable("telcom");
        entity.model->select();
        this->ui->tableView->setModel(entity.model);
      
      }
      

      I have password for 'myuser'@'localhost'. QSqldriver always tries to connect with password NO but if set a password NO in mysql for 'myuser'@'localhost' I have other error message "query does not exixst" so i think that's impossible to connect to phpmyadmin DB with no password. My local user have all needed privileges.

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

      @Shellcpp73
      Hello and welcome.

      You show nothing about how you connect to the database, which is where you specify a username and a password. It does not sound as though you are setting any password. That happens before you send it a query, and just before you open it. Where is your QSqlDatabase instance code, prior to calling QSqlDatabase::open(), or maybe you call bool QSqlDatabase::open(const QString &user, const QString &password)?

      S 1 Reply Last reply
      1
      • JonBJ JonB

        @Shellcpp73
        Hello and welcome.

        You show nothing about how you connect to the database, which is where you specify a username and a password. It does not sound as though you are setting any password. That happens before you send it a query, and just before you open it. Where is your QSqlDatabase instance code, prior to calling QSqlDatabase::open(), or maybe you call bool QSqlDatabase::open(const QString &user, const QString &password)?

        S Offline
        S Offline
        Shellcpp73
        wrote on last edited by Shellcpp73
        #3

        @JonB I have fuction in Entiry class
        void Entity::connectToDb()
        {

        while(!db.open())
        {
        db.setHostName("localhost");
        db.setUserName("username");
        db.setPassword("qwerty123");
        db.setDatabaseName("Main");
        
            qDebug() << "!!!!!!!!";
        }
        

        }

        JonBJ 1 Reply Last reply
        0
        • S Shellcpp73

          @JonB I have fuction in Entiry class
          void Entity::connectToDb()
          {

          while(!db.open())
          {
          db.setHostName("localhost");
          db.setUserName("username");
          db.setPassword("qwerty123");
          db.setDatabaseName("Main");
          
              qDebug() << "!!!!!!!!";
          }
          

          }

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

          @Shellcpp73
          qDebug() << db.lastError() ?

          And what you have is odd. I do not know whether it even goes into the while() body. Can't you do normal:

          db.setHostName("localhost");
          db.setUserName("username");
          db.setPassword("qwerty123");
          db.setDatabaseName("Main");
          
          if (!db.open())
          {
              qDebug() << db.lastError();
          }
          

          Further it looks like your db/entity.db is a persistent QSqlDatabase db member variable. This may not be related to your error, but Qt asks you not to do this. See https://doc.qt.io/qt-5/qsqldatabase.html#details and use local variable QSqlDatabase db = QSqlDatabase::database(); when you need it.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Shellcpp73
            wrote on last edited by Shellcpp73
            #5

            I'm ok with oping to data base and my query works well BUT my QSqlTablemodel don't save changes in the data base and I have error "Access denied for user 'myuser'@'localhost' (using password: NO)") with QSqlTablemodel

            Christian EhrlicherC 1 Reply Last reply
            0
            • S Shellcpp73

              I'm ok with oping to data base and my query works well BUT my QSqlTablemodel don't save changes in the data base and I have error "Access denied for user 'myuser'@'localhost' (using password: NO)") with QSqlTablemodel

              Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by Christian Ehrlicher
              #6

              @Shellcpp73 said in QSqlError("1045", "QMYSQ") Cannot edit table with QSqlTableModel:

              "Access denied for user 'myuser'@'localhost'

              Then fix your access rights for the user in the database or use another db-login which has write rights.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              S 1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                @Shellcpp73 said in QSqlError("1045", "QMYSQ") Cannot edit table with QSqlTableModel:

                "Access denied for user 'myuser'@'localhost'

                Then fix your access rights for the user in the database or use another db-login which has write rights.

                S Offline
                S Offline
                Shellcpp73
                wrote on last edited by Shellcpp73
                #7

                @Christian-Ehrlicher ohhhh I tried many times "myuser" has all needed rights and i have tried with root user and also i have same mistake , I can login to phpmyadmin with there users and make changes but not form QSqlTableModel ((( I don't know why QSQL driver always trying to connect with password NO.

                JonBJ 1 Reply Last reply
                0
                • S Shellcpp73

                  @Christian-Ehrlicher ohhhh I tried many times "myuser" has all needed rights and i have tried with root user and also i have same mistake , I can login to phpmyadmin with there users and make changes but not form QSqlTableModel ((( I don't know why QSQL driver always trying to connect with password NO.

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

                  @Shellcpp73
                  I would check how (and who) phpmyadmin is doing it then.

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Online
                    Christian EhrlicherC Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    It's or sure no problem of Qt or the QSqlTableModel - fix your access rights.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    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