Writing SQlite Databse in Qt



  • Hi.I am writing SQLite database in Qt for client server program! I want every message that server received from client save in database. I am new in Qt so I wrote a simple code, it doesn't have any error but it couldn't save anything in database. Could you please help me?Thanks

    bool MainWindow::createConnection()
     {
    QSqlDatabase database = QSqlDatabase::addDatabase("QSQLITE");
    
    database.setDatabaseName("Message.dat");
    
       if(!database.open())
       {
           qDebug()<<"can not open database";
    
           database.lastError().text();
    
           return false;
        }
       return true;
      }
    
    
    void MainWindow::insertMessage()
     {
       QSqlQuery query;
    
      query.prepare("INSERT INTO messages(IPAddress, date, message)"
                            " values(?,?,?)")
     }
    
     MainWindow::messagewindow()
    {
         enum {
              messages_id = 0 ,
              messages_date =1 ,
              messages_IPAddreess = 2,
              messages_message = 3,
              };
    
    
    
         model = new QSqlTableModel(this);
         {
            model->setTable("messages");
            model->setHeaderData(messages_date, Qt::Horizontal, tr("Date"));
            model->setHeaderData(messages_IPAddreess, Qt::Horizontal, tr("From"));
            model->setHeaderData(messages_message, Qt::Horizontal, tr("Message"));
            model->select();
    
         }
    
    
    
         view = new QTableView;
         {
            view->setSelectionMode(QAbstractItemView::SingleSelection);
            view->setSelectionBehavior(QAbstractItemView::SelectRows);
            view->setColumnHidden(messages_id, true);
            view->setModel(model);
            view->resizeColumnsToContents();
            view->setEditTriggers(QAbstractItemView::NoEditTriggers);
    
            QHeaderView *header = view->horizontalHeader();
            header->setStretchLastSection(true);
         }
    
     }
    

    bool create = !QFile::exists("Message.dat");
    
    if (!myserver.createConnection())
       return 1;
    
    if (create)
    
       myserver.insertMessage();
    
    
    myserver.show();
    

    void insertMessage(QString IPAddrress, QDate date, QString message)
    {
        QSqlQuery query;
        query.addBindValue(IPAddrress);
        query.addBindValue(date);
        query.addBindValue(message);
        query.exec();
    }
    
    
    
    void createdata()
    {
        QSqlQuery query;
        query.exec("DROP TABLE messages");
        query.exec("CREATE TABLE messages ("
                   "id INTEGER PRIMARY KEY AUTOINCREMENT,"
                   "HostAddress integer,"
                   "date char(10),"
                   "message varchar(30))");
    }


  • Hello,
    you have 2 different functions for inserting messages

    void MainWindow::insertMessage()
     {
       QSqlQuery query;
    
      query.prepare("INSERT INTO messages(IPAddress, date, message)"
                            " values(?,?,?)")
     }
    

    and

    void insertMessage(QString IPAddrress, QDate date, QString message)
    {
        QSqlQuery query;
        query.addBindValue(IPAddrress);
        query.addBindValue(date);
        query.addBindValue(message);
        query.exec();
    }
    

    I am not sure that it is the correct way, because only 1st function will be called.
    You can use "query.lastError();" to get debug-information.
    But you will not get any information. You are preparing query but not using it.

    void MainWindow::insertMessage()
     {
       QSqlQuery query;
    
      query.prepare("INSERT INTO messages(IPAddress, date, message)"
                            " values(?,?,?)")
      qDebug() << query.lastError(); // to see details
     }
    

    You could change your code to this example, then it should work but I do not recommend you to use it in this way.

    QSqlQuery MainWindow::insertMessage()
     {
       QSqlQuery query;
    
       query.prepare("INSERT INTO messages(IPAddress, date, message)"
                            " values(?,?,?)");
       return query;
     }
    void insertMessage(QString IPAddrress, QDate date, QString message, QSqlQuery query)
    {    
        query.addBindValue(IPAddrress);
        query.addBindValue(date);
        query.addBindValue(message);
        query.exec();
    }
    

Log in to reply
 

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