我写了一个SQLite
数据库。 DB
已创建,但我无法在其中插入消息。我有一个服务器,我希望每个消息来自客户端保存在DB中。但在调试期间我收到此错误:
"倾听港口" " INSERT INTO消息(IPAddress,日期,消息) 值&#34(,?,?); QSqlError("","参数计数不匹配","")
我应该如何将数据库连接到服务器? 非常感谢
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();
}
}