lundi 28 septembre 2015

How can I get a list of table names from an existing sqlite database using SQLite.NET?

Using the sqlite.net nuget package, how I can I get a list of tables from the database using an instance of SQLiteConnection? I need this functionality so I can detect when my database schema has changed and the database requires a rebuild.

For example, I've defined the entities:

public class Body
{
    [PrimaryKey]
    public int PrimaryKey { get; set; }
}

public class Foot
{
    [PrimaryKey]
    public int PrimaryKey { get; set; }
}

public class Leg
{
    [PrimaryKey]
    public int PrimaryKey { get; set; }

}

I need to retrieve the tables in a list of strings that would contain: Body, Leg, Foot.

The SQLiteConnection class has the TableMappings property that can perform this behaviour. It can only be used after calling SQLiteConnection.CreateTable; this is incorrect as calling CreateTable generates the table binding for an object AND executes the create table if not exists command, thus changing the schema.

The query "SELECT NAME from sqlite_master" can do this (I've tested it in a database browser) but I can't execute it using Execute, ExecuteScalar or Query. How can I retrieve a list of the tables in a database using this command?

Aucun commentaire:

Enregistrer un commentaire