C# 如何使用sqlite.NET从现有sqlite数据库中获取表名列表?

C# 如何使用sqlite.NET从现有sqlite数据库中获取表名列表?,c#,sqlite,C#,Sqlite,使用,如何使用SQLiteConnection实例从数据库获取表列表?我需要此功能,以便在数据库架构发生更改并且数据库需要重建时能够检测到 例如,我定义了实体: public class Body { [PrimaryKey] public int PrimaryKey { get; set; } } public class Foot { [PrimaryKey] public int PrimaryKey { get; set; } } public cl

使用,如何使用SQLiteConnection实例从数据库获取表列表?我需要此功能,以便在数据库架构发生更改并且数据库需要重建时能够检测到

例如,我定义了实体:

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; }

}
我需要检索字符串列表中的表,其中包含:
Body、Leg、Foot

SQLiteConnection类具有可执行此行为的
TableMappings
属性。只能在调用
SQLiteConnection.CreateTable
后使用;这是不正确的,因为调用
CreateTable
会为对象生成表绑定,并在不存在时执行
CreateTable
命令,从而更改架构


查询
“从sqlite_master中选择名称”
可以做到这一点(我已经在数据库浏览器中对其进行了测试),但我无法使用
execute
ExecuteScalar
query
执行它。如何使用此命令检索数据库中的表列表?

以下扩展方法提供了在不使用ORM层的情况下查询现有数据库中的表的功能:

using System;
using System.Collections.Generic;
using SQLite;

namespace MyApplication
{
    public static class SqliteExtensions
    {
        public static List<string> Tables (this SQLiteConnection connection)
        {
            const string GET_TABLES_QUERY = "SELECT NAME from sqlite_master";

            List<string> tables = new List<string> ();

            var statement = SQLite3.Prepare2 (connection.Handle, GET_TABLES_QUERY);

            try {
                bool done = false;
                while (!done) {
                    SQLite3.Result result = SQLite3.Step (statement);

                    if (result == SQLite3.Result.Row) {

                        var tableName = SQLite3.ColumnString (statement, 0);

                        tables.Add(tableName);
                    } else if (result == SQLite3.Result.Done) {
                        done = true;
                    } else {
                        throw SQLiteException.New (result, SQLite3.GetErrmsg (connection.Handle));
                    }
                }
            }
            finally {   
                SQLite3.Finalize (statement);
            }

            return tables;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用SQLite;
命名空间MyApplication
{
公共静态类SqliteExtensions
{
公共静态列表表(此SQLiteConnection连接)
{
const string GET\u TABLES\u QUERY=“从sqlite\u master中选择名称”;
列表表=新列表();
var语句=SQLite3.Prepare2(connection.Handle,GET\u TABLES\u QUERY);
试一试{
bool done=false;
而(!完成){
SQLite3.Result-Result=SQLite3.Step(语句);
if(result==SQLite3.result.Row){
var tableName=SQLite3.ColumnString(语句,0);
tables.Add(tableName);
}else if(result==SQLite3.result.Done){
完成=正确;
}否则{
抛出SQLiteException.New(结果,SQLite3.GetErrmsg(connection.Handle));
}
}
}
最后{
SQLite3.Finalize(语句);
}
返回表;
}
}
}

以下扩展方法提供了在不使用ORM层的情况下查询现有数据库中的表的能力:

using System;
using System.Collections.Generic;
using SQLite;

namespace MyApplication
{
    public static class SqliteExtensions
    {
        public static List<string> Tables (this SQLiteConnection connection)
        {
            const string GET_TABLES_QUERY = "SELECT NAME from sqlite_master";

            List<string> tables = new List<string> ();

            var statement = SQLite3.Prepare2 (connection.Handle, GET_TABLES_QUERY);

            try {
                bool done = false;
                while (!done) {
                    SQLite3.Result result = SQLite3.Step (statement);

                    if (result == SQLite3.Result.Row) {

                        var tableName = SQLite3.ColumnString (statement, 0);

                        tables.Add(tableName);
                    } else if (result == SQLite3.Result.Done) {
                        done = true;
                    } else {
                        throw SQLiteException.New (result, SQLite3.GetErrmsg (connection.Handle));
                    }
                }
            }
            finally {   
                SQLite3.Finalize (statement);
            }

            return tables;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用SQLite;
命名空间MyApplication
{
公共静态类SqliteExtensions
{
公共静态列表表(此SQLiteConnection连接)
{
const string GET\u TABLES\u QUERY=“从sqlite\u master中选择名称”;
列表表=新列表();
var语句=SQLite3.Prepare2(connection.Handle,GET\u TABLES\u QUERY);
试一试{
bool done=false;
而(!完成){
SQLite3.Result-Result=SQLite3.Step(语句);
if(result==SQLite3.result.Row){
var tableName=SQLite3.ColumnString(语句,0);
tables.Add(tableName);
}else if(result==SQLite3.result.Done){
完成=正确;
}否则{
抛出SQLiteException.New(结果,SQLite3.GetErrmsg(connection.Handle));
}
}
}
最后{
SQLite3.Finalize(语句);
}
返回表;
}
}
}

small correction-将任何类型传递到连接时,都会填充TableMappings属性。因此,调用connection.Get将在TableMappings中为T添加一个条目。这只是一个缓存——某种误导性的小纠正——当任何类型被传递到连接时,TableMappings属性都会被填充。因此,调用connection.Get将在TableMappings中为T添加一个条目。这只是一个缓存,有点误导