-1

include "mythread.h

mythread::mythread(int ID, QObject *parent) :
QThread(parent)


{
    this->socketDescriptor = ID;
    db = QSqlDatabase::addDatabase("QODBC");
    db.setHostName("127.0.0.1");
    db.setDatabaseName("mydsn");



    if (db.open())
    {
        qDebug() << "DB is connected!";
        myquery->clear();
        myquery->exec("INSERT INTO phone_table(name_) VALUES ('asdhjs')");
        db.close();
    }
    else
    {
        qDebug() << "Db is not connected!" << db.lastError();
    }
}

When I insert data its getting error Myserver.exe Myserver.exe has stopped working Windows is checking for a solution to the problem... Can anyone help me ?

developer
  • 37
  • 1
  • 6

2 Answers2

0

You'll want to initialize your myquery object with your database.

Try changing your code to this.

if (db.open())
{
    qDebug() << "DB is connected!";

    // Allocate the query object.
    myquery = new QSqlQuery( db ); 
    myquery->clear();
    myquery->exec("INSERT INTO phone_table(name_) VALUES ('asdhjs')");
    db.close();
}
else
{
    qDebug() << "Db is not connected!" << db.lastError();
}
Matthew
  • 2,759
  • 18
  • 28
0

Try this instead of your two myquery lines:

    QSqlQuery q(db);
    q.exec("INSERT INTO phone_table(name_) VALUES ('asdhjs')");
Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173