Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. SQLite3 Insert function
QtWS25 Last Chance

SQLite3 Insert function

Scheduled Pinned Locked Moved Unsolved General and Desktop
sqlite3c++
2 Posts 2 Posters 1.0k Views
  • 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.
  • mandruk1331M Offline
    mandruk1331M Offline
    mandruk1331
    wrote on last edited by
    #1

    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
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      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
      3

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved