views:

60

answers:

2

Today I use SQLite and send a database-file with the project.

However I want the database to be created when the user first starts the software.

Is there a way to copy the code needed to create a database based on the existing database? The problem is that when a user downloads a new version he might be tricked into copying over his last database and lose the data. I'd like a nice way to check the version of the database and modify it if I need new columns or tables etc. Or, if it does not exist at all, create a new database?

I know I can probably make create the code to make the database from the beginning but I want it to be based on the existing database I have created by a gui.

Answered. Final code:

            //Copy the database from the resource
            using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("resourcename"))
            {
                if (stream == null)
                    throw new NullReferenceException("Stream is null. Cannot find the database in the resources");
                FileStream writer = new FileStream(Constants.SqLiteFile, FileMode.Create);
                int Length = 256;
                Byte[] buffer = new Byte[Length];
                int bytesRead = stream.Read(buffer, 0, Length);
                // write the required bytes
                while (bytesRead > 0)
                {
                    writer.Write(buffer, 0, bytesRead);
                    bytesRead = stream.Read(buffer, 0, Length);
                }
                stream.Close();
                writer.Close();
            }
A: 

It sounds like you just need a location to save the version of their current system. Create a config table and have a record that maintains whatever version created/update the database. When they run an installer/upgrade, have it check that version to see if it exists, and/or what version is in place to determine what steps to take.

Thyamine
The problem is not to check what to do, that I can figure out. Just save the database version in the database. What I'm wondering is how do I write the code to perform the change?
Oskar Kjellin
It depends on how you record the changes and what you are doing. Obviously you can issue an ALTER TABLE command and add/update columns. You would just need to know what is missing between versions to know what you want to do.
Thyamine
Yes, that's essentially what I asked for. How to know what is different from the databases as well as creating a new one from scrach(based on the one I have)
Oskar Kjellin
+1  A: 

Well... I think you could go for doing a sinchronization between the two sqlite databases, my idea is getting the tables and fields of each database, and loop over them to see what tables and fields are in one and not in the other.

For getting started, these queries can be useful for you.

-- For getting tables in current db
SELECT name FROM sqlite_master WHERE type = 'table' ORDER BY name;

-- For getting info about a table
PRAGMA table_info('tabla_name');
Jhonny D. Cano -Leftware-
Great! Good idea, thanks for the queries, most helpful. Some concerns tho: I'd like to be able to ship my software without the database. This means that I have no database to compare to and I cannot create the databsae using the code you gave. I'd like to perhaps embed the database as a resource(is this possible) and then copy it to local if none exist. Then I can compare with it. Is it possible to have the db as a resource?
Oskar Kjellin
sure you can, embed it and then you can read it with an instruction like this: GetType().Assembly.GetManifestResourceStream("name of resource");
Jhonny D. Cano -Leftware-
Don't forget to put the Compilation Action to "Embedded Resource"
Jhonny D. Cano -Leftware-
Worked like a charm. Check my edit in the question for the final code.
Oskar Kjellin