Get data from Qsql dataBase



  • I know with code in below can get data from dataBase

    qry.prepare( "SELECT firstname FROM names WHERE lastname = 'Roe'" );
     if( !qry.exec() )
       qDebug() << qry.lastError();
     else
     {
       qDebug( "Selected!" );
       
       QSqlRecord rec = qry.record();
       
       int cols = rec.count();
       
       for( int c=0; c<cols; c++ )
         qDebug() << QString( "Column %1: %2" ).arg( c ).arg( rec.fieldName(c) );
         
       for( int r=0; qry.next(); r++ )
         for( int c=0; c<cols; c++ )
           qDebug() << QString( "Row %1, %2: %3" ).arg( r ).arg( rec.fieldName(c) ).arg( qry.value(c).toString() );
     }
    
    

    but I want set instead of lastname = 'Roe',name that get from QlineEdit

    QString n= ui->lineEdit_name->text();
    qry.prepare( "SELECT firstname FROM names WHERE lastname = n" );
    

    how can do it??



  • Hi @isan,

    but I want set instead of lastname = 'Roe', set name that get from QlineEdit

    Try something like:

    qry.prepare( "SELECT firstname FROM names WHERE lastname = :name" );
    qry.bindValue(":name", ui->lineEdit_name->text());
    

    Cheers.



  • Tnx .it's work


Log in to reply
 

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