Solution is quite simple.
You can't add new database connection for every worker with the same name.
You can or create single connection to each database and then get that connection by name in each worker, or you can create database connection for each worker with different name.
Depending on what you need first and second approach may be as good.
To remove the warning you get (following first approach from above), replace addDatabase( driver, connection_name ) with database( connection_name ) in your Worker constructor.
The actual connection (using addDatabase() ) create in your server constructor (or anywhere else you think it appropriate):
Worker
::Worker(int ID,
QObject* parent
){
}
{
db.setDatabaseName("test.db");
if ( !db.open() )
{
qWarning() << "Unable to connect to database, giving up:" << db.lastError().text();
return;
}
}
Worker::Worker(int ID, QObject* parent )
: QObject( parent )
{
m_database = QSqlDatabase::database( "test.db" );
}
Server::Server(QObject *parent) :
QTcpServer(parent)
{
QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE", "test.db" );
db.setDatabaseName("test.db");
if ( !db.open() )
{
qWarning() << "Unable to connect to database, giving up:" << db.lastError().text();
return;
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks