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. Sql query help
Qt 6.11 is out! See what's new in the release blog

Sql query help

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 815 Views 1 Watching
  • 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.
  • J Offline
    J Offline
    jocala
    wrote on last edited by
    #1

    I'm trying to work with a simple sqlite db, 1 row, 1 table with two columns, id and name.
    Creating the db works and updating the db works, but reading it does not. I've done
    the query manually at the sqlite3 prompt w/o a problem. query.isValid() is never
    true and query.lastError().text() is empty.

    @
    ////////////////////////////////
    void createTables()
    {
    QSqlQuery query;
    query.exec("create table device(id int primary key, name varchar(20))");
    query.exec("insert into device values(1, '')");

    }

    ////////////////////////////////
    void updateTables()
    {
    QSqlQuery query;
    QString sqlstatement = "UPDATE device SET name='"+daddr+"' WHERE Id=1";
    query.exec(sqlstatement);
    }

    ////////////////////////////////
    void readTables()
    {
    QSqlQuery query;
    query.exec("SELECT name FROM device");

    if (query.isValid())
    daddr = query.value(1).toString();
    else
     daddr = "";
    

    }

    //////////////////////////////////////////////
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {

     ui->setupUi(this);
    

    QFile Fout("/Applications/myprog/myprog.db");

    if(!Fout.exists())
    {
    dbexists = false;
    }
    else
    {
    dbexists = true;
    }

    dbstring = "/Applications/myprog/myprog.db";

    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(dbstring);

    if (!db.open()) {
    QMessageBox::critical(0, qApp->tr("Cannot open database"),
    qApp->tr("Unable to establish a database connection.\n"
    ), QMessageBox::Cancel);

    }

    if (!dbexists)
    createTables();
    else
    readTables();

    ui->device->setText(daddr); 
    

    }

    MainWindow::~MainWindow()
    {
    updateTables();
    db.close();
    delete ui;

    }
    @

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jocala
      wrote on last edited by
      #2

      It's fixed:

      @
      QSqlQuery query;
      query.exec("SELECT name FROM device");
      while (query.next()) {
      daddr = query.value(0).toString();
      }
      @

      I guess the query in the first post was sitting on EOD?

      1 Reply Last reply
      0

      • Login

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