Create SQLite database during runtime

9 posts, 1 answers
  1. Erik
    Erik avatar
    36 posts
    Member since:
    Jun 2012

    Posted 31 Jul 2012 Link to this post

    I want to create a SQLite database file during runtime, does someone know how I can achieve this?
  2. Jan Blessenohl
    Admin
    Jan Blessenohl avatar
    707 posts

    Posted 31 Jul 2012 Link to this post

    Hi Erik,
    You can use the schema handler to execute all schema related operations:

    var schema = context.GetSchemaHandler();
    var exists = schema.DatabaseExists();
    var script = "";
    if (exists == false)
    {
        schema.CreateDatabase();
        script = schema.CreateDDLScript();
    }
    else
    {
        script = schema.CreateUpdateDDLScript(new SchemaUpdateProperties() { CheckExtraColumns = false });
    }
    if (string.IsNullOrEmpty(script) == false)
    {
        schema.ExecuteDDLScript(script);
    }


    Regards,
    Jan Blessenohl
    the Telerik team
    OpenAccess ORM Q2'12 Now Available! Get your hands on all the new stuff.
  3. DevCraft banner
  4. Erik
    Erik avatar
    36 posts
    Member since:
    Jun 2012

    Posted 31 Jul 2012 Link to this post

    Hi Jan,

    Thanks for your reply.

    Using your code, the SQLite database file is created, however the line
    script = schema.CreateDDLScript();
    returns an empty string. Am I missing a step?

    Additionally, is it possible to specify file name and location for the SQLite database file?
  5. Ady
    Admin
    Ady avatar
    589 posts

    Posted 03 Aug 2012 Link to this post

    Hi Erik,

     Do you have classes in your Domain model? Can you check the 'Update Schema' property for each domain class (press F4 to open the Properties window and check the property value) ? It should be set to 'true'.
    In cae this does not work can you send me your .rlinq file so that I can have a deeper look at it?

    Regards,
    Ady
    the Telerik team
    OpenAccess ORM Q2'12 Now Available! Get your hands on all the new stuff.
  6. Erik
    Erik avatar
    36 posts
    Member since:
    Jun 2012

    Posted 04 Aug 2012 Link to this post

    Hi Ady,

    Setting the update schema property to true fixes the schema creation :)

    I want to be able to specify the SQLite file name and location during runtime, do you know if this is possible?
  7. Ady
    Admin
    Ady avatar
    589 posts

    Posted 07 Aug 2012 Link to this post

    Hello Erik,

     You can specify the fully qualified file name in the 'DataSource' property of the connection string and then use this connection string to construct the context instance. What does your connection string look like now? What is the value specified with the 'DataSource' keyword?

    Kind regards,
    Ady
    the Telerik team
    OpenAccess ORM Q2'12 Now Available! Get your hands on all the new stuff.
  8. Erik
    Erik avatar
    36 posts
    Member since:
    Jun 2012

    Posted 17 Aug 2012 Link to this post

    Hi Ady,

    The Connection.ConnectionString is:
    "data source=C:\Users\xxxx\MyFolder\testdb;failifmissing=True;enlist=False"

    Connection.DataSource is:
    "testdb"

    When I try to modify the Connection.ConnectionString during runtime I get a System.InvalidOperationException:
    "Cannot change the connection string for an OAConnection."
  9. Answer
    Ady
    Admin
    Ady avatar
    589 posts

    Posted 20 Aug 2012 Link to this post

    Hi Erik,

     Here is some sample code of how to specify the connection string at runtime

    string cs = "data source=C:\\TestFolder\\testdb;failifmissing=True;enlist=False";
    var context = new EntitiesModel(cs);
    var schema = context.GetSchemaHandler();

    Regards,
    Ady
    the Telerik team
    Follow @OpenAccessORM Twitter channel to be the first one to get the latest updates on new releases, tips and tricks and sneak peeks at our product labs!
  10. Amelie
    Amelie avatar
    1 posts
    Member since:
    Apr 2015

    Posted 07 Apr 2015 Link to this post

    I
    hope it will helpful for you

    http://sqliteviewer.blogspot.com/2015/04/create-sqlite-database-file.html
Back to Top
DevCraft banner