SQLite3 Insert function



  • I'm trying to insert data into the SQLite3 database.
    query.prepare("INSERT INTO lecturer (ID,LecturerName)"
    "VALUES (ID, LecturerName);");
    query.bindValue("::ID",ui->lineEdit->text().toInt());
    query.bindValue("::LecturerName",ui->lineEdit_2->text());

    ID is and INTEGER with AUTOINCREMENT
    LecturerName is a char(50)
    The connection to the database is established.
    Error which I get when I want to insert the data into the database:
    No query Unable ti fetch row
    Can someoen help with this issue?? thank you


  • Qt Champions 2016

    Hi
    You have a ":" too much for the bindvalue it seems.
    Also just so we are on the same page. a ("INSERT INTO"..) sql statement do not return
    rows. Only a("SELECT X ") sql would. (AFAIK)

    bool createConnection() {
      QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
      db.setDatabaseName(":memory:");
      if (!db.open()) {
        QMessageBox::critical(0, qApp->tr("Cannot open database"), "Click Cancel to exit.", QMessageBox::Cancel);
        return false;  }
    
      QSqlQuery query;
      qDebug() << "table:" <<   query.exec("create table person (id int primary key, "
                                           "firstname varchar(20), lastname varchar(20), num int )");
      query.exec("insert into person values(101, 'Dennis', 'Young','1')");
      query.exec("insert into person values(102, 'Christine', 'Holand','2')");
      query.exec("insert into person values(103, 'Lars junior', 'Gordon','4')");
      query.exec("insert into person values(104, 'Roberto', 'Robitaille','5')");
      query.exec("insert into person values(105, 'Maria', 'Papadopoulos','3')");
      return true;
    }
        QSqlQuery query;
        int ok = query.prepare("SELECT firstname,num FROM person WHERE num=:thenum AND firstname=:thename AND lastname=:lastname");
        query.bindValue(":thenum", 4);
        query.bindValue(":thename", "Lars junior");
        query.bindValue(":lastname", "Gordon");
        query.exec();
    
    
    
    

Log in to reply
 

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