SQLite content problem



  • 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.



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



  • Noted



  • [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]



  • 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.



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


Log in to reply
 

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