Qt Forum

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

    Update: Forum Guidelines & Code of Conduct

    [SOLVED] SQLite Error: No such table!

    General and Desktop
    2
    4
    27889
    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.
    • S
      suggi87 last edited by

      Hello,

      I made a new sqlite database named auto.db with one table named autos(varchar(50),varchar(50),varchar(50),real,integer)
      I have also insert one dataset.

      If i use cmd.exe i can execute the sql statement which is in my code.
      In QT there is no such table autos???

      this is my code in the constructor:

      @
      QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
      db.setDatabaseName("C:/sqlite/auto.db");
      db.open();

      if(!db.open())
          ui->label_25->setText("FAILED TO OPEN THE DB");
      else
          ui->label_25->setText("CONNECTED DB");
      
      QSqlQuery query1;
      query1.exec("SELECT name FROM autos WHERE kraftstoff = 'Diesel'");
      qDebug(&#41; << query1.size(&#41;;
      qDebug() << query1.isSelect();
      qDebug() << query1.isValid();
      qDebug() << query1.lastError();
      
      while(query1.next())
      {
      QString name = query1.value(0).toString();
      qDebug() << name;
      }
      

      @

      and thats my console:

      -1
      false
      false
      QSqlError("1", "Unable to execute statement", "no such table: autos")

      1 Reply Last reply Reply Quote 0
      • p3c0
        p3c0 Moderators last edited by

        Hi,

        1. No need to open database twice. Instead you can do this
          @
          bool isOpened = db.open();
          if(isOpened) {
          qDebug() << "opened;
          } else {
          qDebug() << "Error";
          }
          @

        2. Try printing the error in else part to get the exact error.

        3. Try passing the current database to QSqlQuery as
          @
          QSqlDatabase db = QSqlDatabase::database;
          QSqlQuery query1(db);
          query1.exec("SELECT name FROM autos WHERE kraftstoff = 'Diesel'");
          @

        157

        1 Reply Last reply Reply Quote 0
        • S
          suggi87 last edited by

          Do I have to install any drivers for sqlite additional to QT Creator?

          1 Reply Last reply Reply Quote 0
          • S
            suggi87 last edited by

            Here is my solution:

            It was the database path. i used auto instead of auto.db

            @
            QString dbName="C:/sqlite/auto";
            @

            1 Reply Last reply Reply Quote 1
            • First post
              Last post