Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否可以检查SQLite表是否存在。_Sqlite_Xamarin_Xamarin.forms - Fatal编程技术网

是否可以检查SQLite表是否存在。

是否可以检查SQLite表是否存在。,sqlite,xamarin,xamarin.forms,Sqlite,Xamarin,Xamarin.forms,我有以下代码: dbcon = DependencyService.Get<ISQLite>().GetConnection(); // create the tables dbcon.CreateTable<Category>(); dbcon.CreateTable<Settings>(); var settings = dbcon.Table<Settings>

我有以下代码:

        dbcon = DependencyService.Get<ISQLite>().GetConnection();

        // create the tables
        dbcon.CreateTable<Category>();
        dbcon.CreateTable<Settings>();

        var settings = dbcon.Table<Settings>().ToList();
        if (settings.Count <= 0)
        {
            var noa = new Settings { Setting = "NumberOfAnswers", Value = 5 };
            var cfs = new Settings { Setting = "CardFrontSide", Value = 0 };
            dbcon.Insert(noa);
            dbcon.Insert(cfs);
        }

        var categories = dbcon.Table<Category>().ToList();
        if (categories.Count <= 0)
        {
            InsertCategory();
        }
dbcon=DependencyService.Get().GetConnection();
//创建表
dbcon.CreateTable();
dbcon.CreateTable();
var settings=dbcon.Table().ToList();

if(settings.Count此查询将返回数据库中的表列表

SELECT * FROM sqlite_master WHERE type = 'table';
您可以将其筛选为一行以进行“存在”检查


您可以这样做:

public static bool TableExists<T> (SQLiteConnection connection)
{    
    const string cmdText = "SELECT name FROM sqlite_master WHERE type='table' AND name=?";
    var cmd = connection.CreateCommand (cmdText, typeof(T).Name);
    return cmd.ExecuteScalar<string> () != null;
}
公共静态bool表存在(SQLiteConnection)
{    
const string cmdText=“从sqlite_master中选择名称,其中type='table'和name=?”;
var cmd=connection.CreateCommand(cmdText,typeof(T).Name);
返回cmd.ExecuteScalar()!=null;
}

您可以使用以下代码

public bool IsTableExists(string tableName)
        {
            try
            {
                var tableInfo = database.GetConnection().GetTableInfo(tableName);
                if(tableInfo.Count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }


public SQLiteAsyncConnection database;

        public ClientDatabase(string dbPath)
        {
            database = new SQLiteAsyncConnection(dbPath);

        }

在一张表上运行一个简单的
SELECT
,根据输出/错误做你需要做的事情怎么样?1比我快:)你的也不错。它比我的更像复制品和馅饼o) 呵呵,所以我提到了来源
public bool IsTableExists(string tableName)
        {
            try
            {
                var tableInfo = database.GetConnection().GetTableInfo(tableName);
                if(tableInfo.Count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }


public SQLiteAsyncConnection database;

        public ClientDatabase(string dbPath)
        {
            database = new SQLiteAsyncConnection(dbPath);

        }