Add new data to SQLite table



  • There is an existing database with information. How do you add a new row?

    What I did it's:

    
    
        QString texto;
        texto=ui->lineEdit_interseccion->text();
    
        QString texto2;
        texto2=ui->lineEdit_interseccion->text().remove(" ", Qt::CaseInsensitive);
        QDate nuevo;
        QString a1;
        a1.append("INSERT INTO ");
        a1.push_back(texto2);
    
        a1.push_back(" (a,b) VALUES(:a,:b);");
    
        QSqlQuery insert(fallas);
        if(!insert.prepare(a1))
        {
            qDebug()<<":::::::"<<insert.lastQuery()<<endl;
        }
    
    
        insert.bindValue(":a", nuevo.currentDate().toString(Qt::RFC2822Date));
        insert.bindValue(":b", ui->textEdit->toPlainText());
    
    
         if(!insert.exec())
         {
             qDebug()<<"...."<<insert.lastError()<<endl;
         }
    
    

    The error it's in exec and it's :

    .... QSqlError("19", "Unable to fetch row", "UNIQUE constraint failed: ayb.a")

    by the way ayb it's the database name. and the prepare argument is INSERT INTO ayb (a,b) VALUES(:a,:b);



  • Hi @Julian

    I think that the error :

    "UNIQUE constraint failed: ayb.a"

    Means that you have already a row that contain the value that you want to answer

    You can start by checking which values are inserted on column a

    Hope this can help !


  • Banned

    This post is deleted!

Log in to reply
 

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