SQLite copy database destroyed original why?

I got this piece of code to copy a file database to a memory database. it works, but it destroys the original file database. (Destroys, I mean, sets the file size to zero)

/**
 * Exec an sql statement in values[0] against
 * the database in pData.
 */
int process_ddl_row(void * pData, int nColumns, char **values, char **columns)
{
    if (nColumns != 1) {
        return 1; // Error
    }

    sqlite3 * db = (sqlite3*)pData;
    if( SQLITE_OK != sqlite3_exec(db, values[0], NULL, NULL, NULL) ) {
        return 1; 
    }
    return 0;
}
/**
 * Insert from a table named by backup.{values[0]}
 * into main.{values[0]} in database pData.
 */
int process_dml_row(void *pData, int nColumns, char **values, char **columns)
{
    if (nColumns != 1) {
        return 1; // Error
    }

    sqlite3* db = (sqlite3*)pData;

    char *stmt = sqlite3_mprintf("insert into main.%q " "select * from backup.%q", values[0], values[0]);
    if( SQLITE_OK != sqlite3_exec(db, stmt, NULL, NULL, NULL) ) {
        return 1; 
    }
    sqlite3_free(stmt);
    return 0;
}


bool CDatabase::LoadFromFile( const char * databaseFile )
{
    if( databaseFile == NULL || this->m_db == NULL ) {
        return false; 
    }

    sqlite3 * memorydb = this->m_db->GetDb() ; // Gets the open memory database. 
    sqlite3 * backupdb = NULL ;
    if( SQLITE_OK != sqlite3_open_v2(databaseFile, &backupdb, SQLITE_OPEN_READONLY, NETBURNER_VFS_NAME ) ) {
        return false; 
    }

    // Schema 
    // ---------------------------------------------------------------------------
    // Create the in-memory schema from the backup
    if( SQLITE_OK != sqlite3_exec(backupdb, "BEGIN", NULL, NULL, NULL) ) {
        return false; 
    }
    if( SQLITE_OK != sqlite3_exec(backupdb, "SELECT sql FROM sqlite_master WHERE sql NOT NULL", &process_ddl_row, memorydb, NULL) ) {
        return false; 
    }
    if( SQLITE_OK != sqlite3_exec(backupdb, "COMMIT", NULL, NULL, NULL) ) {
        return false; 
    }
    sqlite3_close(backupdb);

    // DATA 
    // ---------------------------------------------------------------------------
    // Attach the backup to the in memory

    char sql[255];
    sprintf( sql, "ATTACH DATABASE '%s' as backup", databaseFile ); 
    // This after this line the file database is set to zero bytes. 
    if( SQLITE_OK != sqlite3_exec(memorydb, sql, NULL, NULL, NULL) ) {
        return false; 
    }

    // Copy the data from the backup to the in memory
    if( SQLITE_OK != sqlite3_exec(memorydb, "BEGIN", NULL, NULL, NULL) ) {
        return false; 
    }


    if( SQLITE_OK != sqlite3_exec(memorydb, "SELECT name FROM backup.sqlite_master WHERE type='table'", &process_dml_row, memorydb, NULL) ) {
        return false; 
    }
    if( SQLITE_OK != sqlite3_exec(memorydb, "COMMIT", NULL, NULL, NULL) ) {
        return false; 
    }
    if( SQLITE_OK != sqlite3_exec(memorydb, "DETACH DATABASE backup", NULL, NULL, NULL) ) {
        return false; 
    }

    return true; 
}

I looked at him for a while, and I can understand why this is happening. Suggestions?

+3
source share
1 answer

Your code worked fine for me (tested on WinXP). I think you should try running it without specifying a VFS object (if possible) - just replace NETBURNER_VFS_NAME with 0 in the sqlite3_open_v2 call. This will show if the problem is in configuring VFS or not.

+1
source

Source: https://habr.com/ru/post/1757218/


All Articles