Click here to Skip to main content
15,902,198 members
Home / Discussions / Database
   

Database

 
AnswerRe: SQL TABLE JOIN Pin
Niladri_Biswas30-Nov-11 17:03
Niladri_Biswas30-Nov-11 17:03 
QuestionSQLite in-memory database - Loading sqlite db as memory db Pin
Prathapachandran18-Nov-11 2:18
professionalPrathapachandran18-Nov-11 2:18 
AnswerRe: SQLite in-memory database - Loading sqlite db as memory db Pin
André Kraak18-Nov-11 2:21
André Kraak18-Nov-11 2:21 
GeneralRe: SQLite in-memory database - Loading sqlite db as memory db Pin
Prathapachandran18-Nov-11 2:32
professionalPrathapachandran18-Nov-11 2:32 
AnswerRe: SQLite in-memory database - Loading sqlite db as memory db Pin
Luc Pattyn18-Nov-11 3:14
sitebuilderLuc Pattyn18-Nov-11 3:14 
GeneralRe: SQLite in-memory database - Loading sqlite db as memory db Pin
Prathapachandran18-Nov-11 3:32
professionalPrathapachandran18-Nov-11 3:32 
AnswerRe: SQLite in-memory database - Loading sqlite db as memory db Pin
Luc Pattyn18-Nov-11 4:18
sitebuilderLuc Pattyn18-Nov-11 4:18 
GeneralRe: SQLite in-memory database - Loading sqlite db as memory db Pin
Prathapachandran19-Nov-11 4:02
professionalPrathapachandran19-Nov-11 4:02 
Thanks for the input. I have written my own code to implement it.

Please find the bellow steps.

1. Import the below Native methods

C#
[DllImport("System.Data.SQLite.dll")]
public static extern int sqlite3_backup_init(IntPtr pTo, string toName, IntPtr pFrom, string fromName);

[DllImport("System.Data.SQLite.dll")]
public static extern int sqlite3_backup_step(int b, int n);

[DllImport("System.Data.SQLite.dll")]
public static extern int sqlite3_backup_finish(int backupObject);

[DllImport("System.Data.SQLite.dll")]
public static extern int sqlite3_sleep(int interval);

[DllImport("System.Data.SQLite.dll")]
public static extern int sqlite3_backup_remaining(int remaining);

[DllImport("System.Data.SQLite.dll")]
public static extern int sqlite3_backup_pagecount(int pageCount);


2. Define the below constants
C#
public const int SQLITE_OK = 0;
public const int SQLITE_BUSY = 5;
public const int SQLITE_LOCKED = 6;


3. Above APIs required database as IntPtr. But we have only a connection class [System.Data.SQLite.SQLiteConnection]. So we need to find out database handle from the connection object. I don't know whether it is a right way or not, anyway implement an extension method as shown below
C#
public static IntPtr GetConnectionHandle(this SQLiteConnection connection)
{
    IntPtr returnPointer = IntPtr.Zero;
    FieldInfo[] fields = typeof(SQLiteConnection).GetFields(
            BindingFlags.NonPublic | BindingFlags.Instance |
            BindingFlags.DeclaredOnly);
    object sqLiteBase = null;
    object value = null;

    //Checks the valid connection states
    if (connection.State != System.Data.ConnectionState.Open &&
        connection.State != System.Data.ConnectionState.Fetching &&
        connection.State != System.Data.ConnectionState.Executing)
    {
        return returnPointer;
    }
    foreach (FieldInfo info in fields)
    {
        //Getting the object "internal SQLiteBase _sql;" from the
        //public class instance System.Data.SQLite.SQLiteConnection.
        if (string.Compare(info.Name, "_sql", true) == 0)
        {
            sqLiteBase = info.GetValue(connection);
            break;
        }
    }
    if (sqLiteBase != null)
    {
        fields = sqLiteBase.GetType().GetFields(
            BindingFlags.NonPublic | BindingFlags.Instance |
            BindingFlags.DeclaredOnly);

        //Getting the object "protected SQLiteConnectionHandle _sql;" from the
        //internal class instance System.Data.SQLite.SQLite3.
        foreach (FieldInfo info in fields)
        {
            if (string.Compare(info.Name, "_sql", true) == 0)
            {
                value = info.GetValue(sqLiteBase);
                break;
            }
        }
    }
    if (value != null)
    {
        fields = value.GetType().GetFields(
            BindingFlags.Default | BindingFlags.NonPublic |
            BindingFlags.Instance | BindingFlags.FlattenHierarchy);

        foreach (FieldInfo info in fields)
        {
            //Getting the object "protected IntPtr handle;" from the
            //internal class instance System.Data.SQLite.SQLiteConnectionHandle.
            if (string.Compare(info.Name, "handle", true) == 0)
            {
                returnPointer = (IntPtr)info.GetValue(value);
                break;
            }
        }
    }
    return returnPointer;
}


4. Now implement the Load method, which will load the schema and data to the memory-db as in the documentation http://www.sqlite.org/backup.html[^]

C#
SQLiteConnection sqliteConnection = null;
 public bool Load(string databaseFile)
 {
     sqliteConnection = new SQLiteConnection("Data Source=:memory:");
     sqliteConnection.Open();

     if (!File.Exists(databaseFile))
     {
         return false;
     }

     IntPtr toHandle = sqliteConnection.GetConnectionHandle();

     // Creating a readonly connection
     SQLiteConnection fromConnection = new SQLiteConnection(
         string.Format("Data Source={0};Read Only=True", databaseFile));
     fromConnection.Open();

     IntPtr fromHandle = fromConnection.GetConnectionHandle();

     int retValue = sqlite3_backup_init(toHandle,
         "main", fromHandle, "main");
     if (retValue > 0)
     {
         sqlite3_backup_step(retValue, -1);
         sqlite3_backup_finish(retValue);
     }
     fromConnection.Close();
     return true;
 }


5. Do any query operations in the sqliteConnection (memory) and finally save it to the disk as shown below.

C#
public bool Save(string databaseFile)
{
    bool isSuccess = false;
    SQLiteConnection fileConnection = new SQLiteConnection(
        string.Format("Data Source={0};Version=3;New=true", databaseFile));
    try
    {
        fileConnection.Open();
        IntPtr pFileTo = fileConnection.GetConnectionHandle();
        IntPtr pFileFrom = sqliteConnection.GetConnectionHandle();

        int pBackup = sqlite3_backup_init(pFileTo, "main",
            pFileFrom, "main");

        int rc = 0;
        if (pBackup > 0)
        {
            do
            {
                rc = sqlite3_backup_step(pBackup, 10000);

                if (SaveProgressEvent != null)
                {
                    SaveProgressEvent(
                        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);
            sqlite3_backup_finish(pBackup);
        }
        isSuccess = true;
    }
    catch
    {
    }
    finally
    {
        if (fileConnection.State != ConnectionState.Closed)
        {
            fileConnection.Close();
        }
    }
    return isSuccess;
}

AnswerRe: SQLite in-memory database - Loading sqlite db as memory db Pin
jschell18-Nov-11 10:48
jschell18-Nov-11 10:48 
GeneralRe: SQLite in-memory database - Loading sqlite db as memory db Pin
Prathapachandran19-Nov-11 3:30
professionalPrathapachandran19-Nov-11 3:30 
GeneralRe: SQLite in-memory database - Loading sqlite db as memory db Pin
jschell25-Nov-11 8:35
jschell25-Nov-11 8:35 
QuestionSelf-join to return either/or results? Pin
AHSTech17-Nov-11 6:12
AHSTech17-Nov-11 6:12 
AnswerRe: Self-join to return either/or results? Pin
David Mujica17-Nov-11 6:17
David Mujica17-Nov-11 6:17 
GeneralRe: Self-join to return either/or results? Pin
AHSTech17-Nov-11 7:14
AHSTech17-Nov-11 7:14 
AnswerRe: Self-join to return either/or results? Pin
SilimSayo18-Nov-11 4:53
SilimSayo18-Nov-11 4:53 
AnswerRe: Self-join to return either/or results? Pin
Chris Meech18-Nov-11 6:13
Chris Meech18-Nov-11 6:13 
GeneralRe: Self-join to return either/or results? Pin
SilimSayo18-Nov-11 6:53
SilimSayo18-Nov-11 6:53 
GeneralRe: Self-join to return either/or results? Pin
Chris Meech18-Nov-11 7:46
Chris Meech18-Nov-11 7:46 
QuestionSSIS - SQL Job Pin
Sun Rays17-Nov-11 5:41
Sun Rays17-Nov-11 5:41 
Question"Lazy Spool" problem Pin
Bernhard Hiller17-Nov-11 1:00
Bernhard Hiller17-Nov-11 1:00 
AnswerRe: "Lazy Spool" problem Pin
R. Giskard Reventlov17-Nov-11 1:47
R. Giskard Reventlov17-Nov-11 1:47 
JokeRe: "Lazy Spool" problem Pin
Bernhard Hiller18-Nov-11 0:46
Bernhard Hiller18-Nov-11 0:46 
AnswerRe: "Lazy Spool" problem Pin
Jörgen Andersson17-Nov-11 3:45
professionalJörgen Andersson17-Nov-11 3:45 
GeneralRe: "Lazy Spool" problem Pin
Bernhard Hiller18-Nov-11 0:47
Bernhard Hiller18-Nov-11 0:47 
QuestionDatabase Mail 2008 problem Pin
Falconapollo16-Nov-11 22:49
Falconapollo16-Nov-11 22:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.