The class that initializes the "database" class
void Client::run(){
cout << "Client thread has enter the thread execution\n";
message = new Message(this);
database = new Database(this);
message->knowDatabase(database);
if(!tcpSocket->setSocketDescriptor(_socketDescriptor))
cout << "Error - setting socket descriptor\n";
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readMessage()));
connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
exec();
}
void Client::run(){
cout << "Client thread has enter the thread execution\n";
message = new Message(this);
database = new Database(this);
message->knowDatabase(database);
tcpSocket = new QTcpSocket();
if(!tcpSocket->setSocketDescriptor(_socketDescriptor))
cout << "Error - setting socket descriptor\n";
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readMessage()));
connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
exec();
}
To copy to clipboard, switch view to plain text mode
The database class:
Database::Database(Client *client){
cout << "Initializing database connection\n";
_client = client;
db.setHostName("localhost");
db.setDatabaseName("infospeed");
db.setUserName("santiago");
db.setPassword("santipass");
}
void Database::connectToDatabase(){
bool ok = db.open();
if(ok)
cout << "Database connection successful\n";
else
cout << "Error connecting to the database\n";
}
connectToDatabase();
QString myQuery
("SELECT name, lastname FROM user WHERE id=\"");
myQuery += userId;
myQuery += "\" and password=\"";
myQuery += password;
myQuery += "\"";
query.next();
QString name
= query.
value(0).
toString();
QString lastName
= query.
value(1).
toString();
QString userName
= name
+ " " + lastName;
cout << "User name " << userName.toStdString() << endl;
disconnectFromDatabase();
return userName;
}
Database::Database(Client *client){
cout << "Initializing database connection\n";
_client = client;
db = QSqlDatabase::addDatabase("QMYSQL3");
db.setHostName("localhost");
db.setDatabaseName("infospeed");
db.setUserName("santiago");
db.setPassword("santipass");
}
void Database::connectToDatabase(){
bool ok = db.open();
if(ok)
cout << "Database connection successful\n";
else
cout << "Error connecting to the database\n";
}
QString Database::getName(QString userId, QString password){
connectToDatabase();
QString myQuery("SELECT name, lastname FROM user WHERE id=\"");
myQuery += userId;
myQuery += "\" and password=\"";
myQuery += password;
myQuery += "\"";
QSqlQuery query(myQuery, db);
query.next();
QString name = query.value(0).toString();
QString lastName = query.value(1).toString();
QString userName = name + " " + lastName;
cout << "User name " << userName.toStdString() << endl;
disconnectFromDatabase();
return userName;
}
To copy to clipboard, switch view to plain text mode
I hope this helps to solve the issue !!!
Bookmarks