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. SQLite content problem
Forum Updated to NodeBB v4.3 + New Features

SQLite content problem

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 2.5k Views 1 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.
  • H Offline
    H Offline
    House15
    wrote on last edited by
    #1

    Hello and thanks everyone who read this.

    I have a situation with SQLite database which was created by means QtSQK. The database was created without issue and all query running perfectly, but base it self still empty, All query which must select nedeed data give me:

    @QSqlQuery::value: not positioned on a valid record@

    It's not suprise me, because QSqlTableModel show me empty base to, but i don't get it: why it's empty? "INSERT" query have been started without problems, and "isActive" check was fine.

    Please clear my vision of this. Thanks in advance.

    Here is my code:
    @
    DataBase= QSqlDatabase::addDatabase("QSQLITE");
    DataBase.setDatabaseName("MYSQLDATABASE.db");
    qDebug()<<"From here we started new connetion session. MYSQLDATABASE was conneted.";
    QSqlDriver *driver = DataBase.driver();

    if(driver->hasFeature(QSqlDriver::Transactions))
    {qDebug()<<"DataBase has support commitTransaction feature";}
    else{qDebug()<<"DataBase hasn't' support commitTransaction feature";}
    
    if(DataBase.open())
    { 
        QStringList listOfTables=DataBase.tables();
        if(listOfTables.isEmpty())
        {
            QSqlQuery query;
            query.exec&#40;"CREATE TABLE users (login VARCHAR(30&#41;, password VARCHAR(30&#41;,post VARCHAR(50))");
            if(!query.isActive())
            {qDebug()<<query.lastError();}
            else{ qDebug()<<"In DataBase was added new table";}
        }
        else{
           qDebug()<<"DataBase was not empty. No table was created.";
        }
    }
    else
    {
        qDebug()<<DataBase.isValid();
        qDebug()<<DataBase.lastError();
    }
    

    void Dialog::on_pb_connect_clicked()
    {

    if(!ui->pb_connect->isChecked())
    {
    ...
    }

    else
    {
    if((settings.NameOfTheClient.isEmpty())&&(settings.HostOfTheClient.isEmpty()))
    {
    settings.show();
    settings.activateWindow();
    ui->pb_connect->setChecked(false);
    }
    else
    {
    QString str="SELECT * FROM users WHERE login='"+settings.NameOfTheClient+"' and password='"+settings.Some+"'";
    QSqlQuery query;
    query.exec(str);
    qDebug()<<"First value on query 'SELECT * FROM users:"<<query.value(0).toString()<<" "<<query.value(1).toString()<<" "<<query.value(2).toString();
    if((query.isActive())&&(!query.value(0).toString().isEmpty()))
    {
    QString login_from_sql = query.value(0).toString();
    QString password_from_sql=query.value(1).toString();
    qDebug()<<login_from_sql<<" "<<password_from_sql;
    _sok->connectToHost(settings.HostOfTheClient, settings.PortOfTheClient);
    ui->pb_connect->setChecked(true);
    }
    else
    {
    qDebug()<<query.lastError();
    AddUserBox.setInformativeText(tr("What shall do?"));
    int result=AddUserBox.exec();
    switch(result)
    {
    case QMessageBox::Save:
    {
    AuthDialog.exec();
    DataBase.transaction();
    query.prepare("INSERT INTO users (login, password, post)"
    "VALUES (:login, :password, :post)");
    query.bindValue(":login", AuthDialog.UserName);
    query.bindValue(":password", AuthDialog.UserPassword);
    query.bindValue(":post",AuthDialog.UserPost);
    query.exec();
    qDebug()<<"In query was intered:"<<AuthDialog.UserName<<" "<<AuthDialog.UserPassword<<" "<<AuthDialog.UserPost;
    if(!query.isActive())
    {
    QMessageBox::information(this,tr("Query failed"),tr("add_auery was unable to comply"));
    qDebug()<<query.lastError().text();
    }
    else
    {
    DataBase.commit();

                        }
                    }
                        break;
                    case QMessageBox::Retry:
                        break;
                    case QMessageBox::Cancel:
                        AddUserBox.close();
                        break;
                    }
                }
    
               }
    };
    

    }
    @

    PS. "settings" it,s my dialog with QString fields, which used for fill table "users" in DataBase.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      Please adjust the title to reflect the content of your question. “SQLite is not so bright” is less than meaningful. Thanks.

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • H Offline
        H Offline
        House15
        wrote on last edited by
        #3

        Noted

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lgeyer
          wrote on last edited by
          #4

          [quote]"QSqlQuery":http://developer.qt.nokia.com/doc/qt-4.8/qsqlquery.html#details
          Successfully executed SQL statements set the query's state to active so that isActive() returns true. Otherwise the query's state is set to inactive. In either case, when executing a new SQL statement, the query is positioned on an invalid record. An active query must be navigated to a valid record (so that isValid() returns true) before values can be retrieved.

          Navigating records is performed with the following functions: next(), previous(), first(), last(), seek().
          [/quote]

          1 Reply Last reply
          0
          • H Offline
            H Offline
            House15
            wrote on last edited by
            #5

            I belive, that navigation records will be useful, than data base will be contain something. I still have empty QSqlTableModel on table "users".

            Maybe some thing wrong with a model. Hereit is:
            @
            QSqlTableModel model;
            model.setTable("users");
            model.select();
            QTableView *view=new QTableView;
            view->setModel(&model);
            view->show();
            @

            In another words "SELECT * FROM users", and nothing there.

            1 Reply Last reply
            0
            • H Offline
              H Offline
              House15
              wrote on last edited by
              #6

              Nope, you're right, it's working. My apologies.

              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