Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved SQLite3 Insert function

    General and Desktop
    sqlite3 c++
    2
    2
    952
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • mandruk1331
      mandruk1331 last edited by

      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

      Mandruk1331

      1 Reply Last reply Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by mrjj

        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();
        
        
        
        
        1 Reply Last reply Reply Quote 3
        • First post
          Last post