samedi 2 janvier 2016

Error:"Parameter count mismatch" in QSQLite

I wrote a SQLite database. DB is created but I can't insert message in it. I have a server that want every message comes from client save in a DB.but during debug i get this error:

"Listening to port " "INSERT INTO messages(IPAddress, date, message) values(?,?,?)" QSqlError("", "Parameter count mismatch", "")

How should i connect my DB to my server? Thanks a lot

myserver.cpp:

myserver::myserver(QObject *parent) :

QTcpServer(parent)
{
}

void myserver::startserver()
 {
 int port = 6666;

connect(this,SIGNAL(newConnection()), this,SLOT(connectionAccepted()));

if(!this->listen(QHostAddress::Any, port))

{
   qDebug() << "Could not start server ";
}

 else

{
   qDebug() << "Listening to port " ;
}

}


void myserver::connectionAccepted()
{
    qDebug()<<"Connected";

    m_client = nextPendingConnection();

    connect(m_client, SIGNAL(readyRead()), this, SLOT(readSocket()));

    connect(m_client, SIGNAL(disconnected()), this, SLOT(disconnected()));

    QString IPAddress= m_client->peerAddress().toString();

    qDebug()<<"IP Address:"<< IPAddress;
}



void myserver::readSocket()
 {
  // qDebug()<<m_client->bytesAvailable();

    QByteArray message = m_client->readAll();

    qDebug() <<" Data in: " << message;

   QDate date=QDate::currentDate() ;

   qDebug() << date;

    m_client->write(message);

  }


void myserver::disconnected()
  {
     qDebug() << " Disconnected";

     m_client->deleteLater();

     exit(0);
  }




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



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

    query.addBindValue(IPAddress);
    query.addBindValue(date);
    query.addBindValue(message);
    query.exec();

    //qDebug() << query.lastError();

    if (query.exec()) {

        qDebug() << "ok!";
    }
    else
    {
      qDebug() << query.executedQuery();
      qDebug() << query.lastError();
    }
 }

Aucun commentaire:

Enregistrer un commentaire