mercredi 30 décembre 2015

QSqlError("", "", "")

I want to write SQLite database for my client server program. In the database, server should save every message comes from client. The table have 3 column: client IP, date and message.I wrote the following code but during debbugging in insertmessage() i get this error: "QSqlError("", "", "") " . What is wrong with it?should i have some code for connecting message that server received or client IP with DB?Thanks a lot.

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;
}

MainWindow::messagewindow()
{
     enum {
          messages_id = 0 ,
          messages_date =1 ,
          messages_HostAddreess = 2,
          messages_message = 3,
          };



      model = new QSqlTableModel(this);
     {
        model->setTable("messages");
        model->setHeaderData(messages_date, Qt::Horizontal, tr("Date"));
        model->setHeaderData(messages_HostAddreess, 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);
     }

 }

 MainWindow::~MainWindow()
 {
  delete ui;
  }


  QString HostAddrress;
  QDate currentDate;
  QString message;

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

if (create)

   myserver.createdata();

   myserver.insertMessage( HostAddrress, currentDate, message);


myserver.show();

return a.exec();

}


void MainWindow::createdata()
{
    QSqlQuery query;
    query.exec("DROP TABLE messages");
    query.exec("CREATE TABLE messages("
               "id INTEGER PRIMARY KEY AUTOINCREMENT,"
               "HostAddress varchar(20),"
               "date varchar(10),"
               "message varchar(30))");
}


void MainWindow::insertMessage(QString HostAddrress, QDate currentDate, QString message)
{
    QSqlQuery query;

    query.prepare("INSERT INTO messages(HostAddress, date, message) values(?,?,?)");

    query.addBindValue(HostAddrress);
    query.addBindValue(currentDate);
    query.addBindValue(message);
    query.exec();

    if (query.exec()) {
      // got no error, proceed
      qDebug() << "yes!";
    } else {
      // got an error, deal with it
      qDebug() << query.executedQuery();
      qDebug() << query.lastError();
    }
 }

Aucun commentaire:

Enregistrer un commentaire