Important: Please read the Qt Code of Conduct -

SQL Database Connection and Comparison

  • Hello,

    i would to ask about a qlineedit a surname of a current database.
    If the surname exists, it should show it over a QTableView.

    I made it for the surname "Mueller" but how can I generalize it or compare it
    with the other surnames in the database.

    If f you want to support,please give me the code as sample or directly.


    void Suchen::on_SucheStart_clicked()
        QString suchen1;
        QString sqlAbfrage = "anachname = '" + suchen1 + "'";
        suchen1 = ui->lineEditname->text();
        sqlAbfrage = suchen1;
        //wenn eine Verbindung zu Datenbank besteht
        if (dbVerbunden == true)
            if (suchen1 == "Mueller" )
            //sqlAbfrage == "Mueller"
            //suchen == ui->lineEditname->text()
            //den Dialog anzeigen
            suchenliste *formListeSuchen = new suchenliste();
            //den Dialog anzeigen
            QMessageBox::information(this,"Nachname Suche","keine Ergebnisse");

    Code about SQL

        //QString sqlAbfrage;
        QString suchen;
        suchen = "Strobelt";
        //den Fenstertitel setzen
        setWindowTitle("Suchen Anzeige");
        //das Modell erstellen
        QSqlTableModel *modell = new QSqlTableModel(this);
        //die Tabelle setzen
        //"anachname" == sqlAbfrage
        modell->setFilter("anachname = 'Mueller'"); //"anachname = 'Mueller'"
        //die Texte für die Spaltenköpfe setzen
        modell->setHeaderData(0, Qt::Horizontal, "ID");
        modell->setHeaderData(1, Qt::Horizontal, "Vorname");
        modell->setHeaderData(2, Qt::Horizontal, "Name");
        modell->setHeaderData(3, Qt::Horizontal, "Strasse");
        modell->setHeaderData(4, Qt::Horizontal, "PLZ");
        modell->setHeaderData(5, Qt::Horizontal, "Ort");
        modell->setHeaderData(6, Qt::Horizontal, "Telefon");
        //die Daten beschaffen
        //die TableView erstellen
        QTableView *ansicht = new QTableView(this);
        //mit dem Modell verbinden
        //die Breite der Spalten an den Inhalt anpassen
        //die Größe setzen

  • Lifetime Qt Champion


    Do you mean get the content of your QLineEdit and do a SELECT query on your database ?

  • Yes, I mean to get the information from the QLine edit and then select it from the SQL Database.

  • Lifetime Qt Champion

    Then why don't you just pass the value from that QLineEdit as a parameter to suchenliste ?

Log in to reply