Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1062519
  • 博文数量: 264
  • 博客积分: 6005
  • 博客等级: 大校
  • 技术积分: 2798
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-08 20:15
文章分类

全部博文(264)

文章存档

2011年(42)

2010年(213)

2009年(4)

2008年(2)

2007年(3)

分类: C/C++

2011-03-08 10:42:37

To make SQLite in-memory load/save I use this steps:

1. Add two rows to .pro file:
     INCLUDEPATH = $$[QT_INSTALL_PREFIX]/src/3rdparty/sqlite
     SOURCES += $$[QT_INSTALL_PREFIX]/src/3rdparty/sqlite/sqlite3.c
2. Add include to file where I plan to use SQLite API:
     #include

3. Use function similar to this:
=============================code==================================================
bool sqliteDBMemFile( QSqlDatabase memdb, QString filename, bool save )
{
    bool state = false;
    QVariant v = memdb.driver()->handle();
    if( v.isValid() && qstrcmp(v.typeName(),"sqlite3*") == 0 )
    {
        // v.data() returns a pointer to the handle
        sqlite3 * handle = *static_cast(v.data());
        if( handle != 0 ) // check that it is not NULL
        {
            sqlite3 * pInMemory = handle;
            const char * zFilename = filename.toLocal8Bit().data();
            int rc; /* Function return code */
            sqlite3 *pFile; /* Database connection opened on zFilename */
            sqlite3_backup *pBackup; /* Backup object used to copy data */
            sqlite3 *pTo; /* Database to copy to (pFile or pInMemory) */
            sqlite3 *pFrom; /* Database to copy from (pFile or pInMemory) */

            rc = sqlite3_open( zFilename, &pFile );
            if( rc==SQLITE_OK ){
                pFrom = ( save ? pInMemory : pFile);
                pTo = ( save ? pFile : pInMemory);

                pBackup = sqlite3_backup_init(pTo, "main", pFrom, "main");
                if( pBackup ){
                        (void)sqlite3_backup_step(pBackup, -1);
                        (void)sqlite3_backup_finish(pBackup);
                }
                rc = sqlite3_errcode(pTo);
            }

            (void)sqlite3_close(pFile);

            if( rc == SQLITE_OK ) state = true;

        }

    }
    return state;
}
阅读(10546) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~