Can’t open database in resource

  Kiến thức lập trình

I have a project whose file structure is the following:

   |-Header Files
   |-Sources Files
   | resources


ls -l resources/                                         
total 6388                                                                                                           
-rw------- 1 alain alain    1630 16 mai   20:29 icon.png                                                             
-rw-r--r-- 1 alain alain 6537216 16 mai   20:28 pl_db.db       

My .qrc file contains :

<RCC>
<qresource prefix="/">
    <file>resources/pl_db.db</file>
    <file>resources/icon.png</file>
</qresource> 
</RCC>

My code is

    mainDb.setDatabaseName(":/resources/pl_db.db");
    QImage *green = new QImage(":/resources/icon.png");
    if(green->isNull()) {
        qDebug("null");
    }
    else {
        qDebug(" image opened");
    }

    if(mainDb.open()){
        qDebug() << "Db opened";
    } else {
        qDebug() << "db not opened" ;
        qDebug() << mainDb.lastError();
    }

Terminal output is

image opened
 db not opened
 QSqlError("14", "Error opening database", "unable to open database file")

If I copy the database file in ~/ and replace
setDatabaseName(":/resources/pl_db.db");

by
setDatabaseName(":/home/alain/pl_db.db");

the database is correctly opened.
#sqlitebrowser resources/pl_db.db shows that the database file is valid.

I must be making an obvious error, but I am really unable to catch it. What I am doing wrong here ?
Thanks for your help.

LEAVE A COMMENT