Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 965641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:01:55+00:00 2026-05-16T02:01:55+00:00

I got this code snippet to copy a file database to a memory database.

  • 0

I got this code snippet to copy a file database to a memory database. it works but it destroys the original file database. (By 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 have been looking at it for awhile now and I can seem to figure out why this is happening. Suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T02:01:56+00:00Added an answer on May 16, 2026 at 2:01 am

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 490k
  • Answers 490k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you start with a pointer to an object, use… May 16, 2026 at 9:13 am
  • Editorial Team
    Editorial Team added an answer Provided a is exactly the right size and arrays are… May 16, 2026 at 9:13 am
  • Editorial Team
    Editorial Team added an answer Whitespace is \s, not \w ^(-?\d+(\.\d+)?),\s*(-?\d+(\.\d+)?)$ See if this works May 16, 2026 at 9:13 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I've got a slightly tricky problem to solve; imagine this: One of my applications
I have the following code (almost an exact copy of the Chat server example
I've sort of got Fsi.exe working as expected on a Mac OS X (Snow
I learned how to authenticate users in Django months ago, but I've since upgraded
I've got a plugin system in my project (running on linux), and part of
I need a little help with the JProgressBar component. My program copies files from
Are there any alternatives to LogonUser and for impersonating given account in order to
I was doing some j2mee work on ubuntu linux with Netbeans 6.7.1. I went
Im in the situation that I often send small codesnippets and xml-snippets to coworkers

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.