Thank you Ryan,
i wrote a small test routine using the same backup routine i use in my code
and in fact, here it works. Attached.

Need to investigate for differences.
Thanks to community for support

Am 10.03.2016 um 21:56 schrieb R Smith:
>
>
> On 2016/03/10 10:41 PM, asdf asdf wrote:
>> Hello,
>> what do you mean, please ? What code is not shown:my own code (and what
>> could be the cause then) using the example or what i posted in
>> stackoverflow.
>>
>>   I would be happy to solve it. Any information appreciated-
>
> He means that there is something else that is wrong in your code that
> you've used to test this Backup feature. We cannot see your full code,
> so we do not know what is wrong with it, but we know something is
> wrong because the backup from memory works when we do it, and works
> when done like the example. You can easily use the command line
> SQLite3.exe tool to test it.
>
> So, if it works correctly for us, meaning it is not an SQLite bug, but
> there might be something in your code that is missing or weird and we
> would like to help you find it, however, only if you show us the exact
> code you've used to test with. Also the DB schema - some SQL perhaps
> to populate the in-memory database. Perhaps something about the schema
> is strange and causes the problem, then it might even be a bug - but
> we can't know that until we have exactly what you have.
>
> Thanks,
> Ryan
>
> _______________________________________________
> sqlite-users mailing list
> sqlite-users at mailinglists.sqlite.org
> http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users

-------------- next part --------------
#include "stdafx.h"
#include "sqlite3.h"
#include <string>
#include <iostream>
#include <sstream> 
//#include "sqlite3ext.h"


int backupDb(
  sqlite3 *pDb,               /* Database to back up */
  const char *zFilename,      /* Name of file to back up to */
  void(*xProgress)(int, int)  /* Progress function to invoke */     
){
  int rc;                     /* Function return code */
  sqlite3 *pFile;             /* Database connection opened on zFilename */
  sqlite3_backup *pBackup;    /* Backup handle used to copy data */

  /* Open the database file identified by zFilename. */
  rc = sqlite3_open(zFilename, &pFile);
  if( rc==SQLITE_OK ){

    /* Open the sqlite3_backup object used to accomplish the transfer */
    pBackup = sqlite3_backup_init(pFile, "main", pDb, "main");
    if( pBackup ){

      /* Each iteration of this loop copies 5 database pages from database
      ** pDb to the backup database. If the return value of backup_step()
      ** indicates that there are still further pages to copy, sleep for
      ** 250 ms before repeating. */
      do {
        rc = sqlite3_backup_step(pBackup, 5);
        if(xProgress != nullptr)
        {
            xProgress(
                sqlite3_backup_remaining(pBackup),
                sqlite3_backup_pagecount(pBackup)
                );
        }
        if( rc==SQLITE_OK || rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
          sqlite3_sleep(250);
        }
      } while( rc==SQLITE_OK || rc==SQLITE_BUSY || rc==SQLITE_LOCKED );

      /* Release resources allocated by backup_init(). */
      (void)sqlite3_backup_finish(pBackup);
    }
    rc = sqlite3_errcode(pFile);
  }

  /* Close the database connection opened on database file zFilename
  ** and return the result of this function. */
  (void)sqlite3_close(pFile);
  return rc;
}

bool    backup_test_sqlite_mem(void)
{
    sqlite3*    db=nullptr;
    /// create memory db
    if(sqlite3_open(":memory:", &db) == SQLITE_OK)
    {
        /// create table
        std::string sql = "CREATE TABLE IF NOT EXISTS [stock] ("
            "[sid] VARCHAR(40) UNIQUE NOT NULL PRIMARY KEY,"
            "[price] FLOAT "
            " ) ";


        char* db_err = nullptr;
        if(sqlite3_exec(db, sql.c_str(), nullptr, nullptr, &db_err) == 
SQLITE_OK)
        {
            /// fill table with 10000 records
            for(int i=0;i < 10000;i++)
            {                
                std::ostringstream sstr;
                sstr<<"INSERT INTO stock (sid,price) VALUES(";
                sstr<<"'"<<i<<"',";
                sstr<<(double) i / 7;
                sstr<<")";
                sql=sstr.str();

                if(sqlite3_exec(db, sql.c_str(), nullptr, nullptr, &db_err) != 
SQLITE_OK)
                {
                    std::cout<<"\nsql error: "<<sql.c_str();
                    return false;
                }
            }

            /// check we have content
            sqlite3_stmt* stmt=nullptr;           
            sql="SELECT count(*) FROM stock";         
            int res=sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, 0);
            if(res == SQLITE_OK)
            {
                 /// cycle result rows
                 do{
                     res = sqlite3_step(stmt);
                     int row=0;
                     int cols=sqlite3_column_count(stmt);
                     switch (res)
                     {
                         case SQLITE_DONE:
                         break;
                         case SQLITE_ROW:
                         {                            
                             /// cycle result columns
                             for (int col = 0; col < cols; col++)
                             {
                                 std::string sres=(const 
char*)sqlite3_column_text(stmt, 0);
                                 std::cout << "\nsql res("<<row<<","<<col<<") : 
" << sres.c_str();
                             }
                         }
                         break;
                         default:
                         {
                             sqlite3_finalize(stmt);

                             std::cout << "\nsql error: " << sql.c_str();
                             return false;
                         }
                         break;
                     } /// switch
                 } while (res == SQLITE_ROW);

                 sqlite3_finalize(stmt);
                 stmt=nullptr;
             }
             else
             {
                 sqlite3_finalize(stmt);
                 std::cout << "\nsql error: " << sql.c_str();
                 return false;
             }
        }
        else
        {
            std::cout << "\nsql error: " <<sql.c_str();
            return false;            
        }
    }


    /// do the backup
    int ibackup= backupDb(db,"memdb_file_backup.db",nullptr);

    /// set break here to check backup in explorer
    return true;
}
int main(int argc, _TCHAR* argv[])
{
    backup_test_sqlite_mem();
        return 0;
}

Reply via email to