与monodroid一起使用Vici冷藏库

本文关键字:Vici 冷藏 monodroid 一起 | 更新日期: 2023-09-27 18:07:17

嘿,目前我不确定官方对它的支持程度,但有报道称,有人成功地将mondroid与vici-coolStorage一起使用。我已经能够将程序集放入我的项目中并进行编译。但是,当我尝试使用某些类时,它们会引发编译时错误。特别是当试图像网站上的monoTouch一样进行连接时。。


string dbName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "mydb.db3");
// The following line will tell CoolStorage where the database is,
// create it if it does not exist, and call a delegate which
// creates the necessary tables (only if the database file was
// created new)
CSConfig.SetDB(dbName, true, () => {
   CSDatabase.ExecuteNonQuery(@"CREATE TABLE person 
                                 (PersonID INTEGER PRIMARY KEY AUTOINCREMENT,
                                  Name TEXT(50) NOT NULL,
                                  DateOfBirth TEXT(30) NULL)");
});

我在尝试使用时没有任何智能CSConfig的方法,当我试图将3个参数传递给CSConfig.SetDB((时,我得到了一个无效的参数数错误。

与monodroid一起使用Vici冷藏库

我认为他们的示例有缺陷。如果您使用Visual Studio程序集浏览器、MonoDevelop程序集浏览器,甚至仅使用monop -r:Vici.CoolStorage.MT.dll Vici.CoolStorage.CSConfig,您将看到SetDB:的这些重载

public static void SetDB (CSDataProvider db);
public static void SetDB (CSDataProvider db, string contextName);
public static void SetDB (string dbName);
public static void SetDB (string dbName, Action creationDelegate);
public static void SetDB (string dbName, SqliteOption sqliteOption);
public static void SetDB (string dbName, SqliteOption sqliteOption, Action creationDelegate);

这些都不接受bool作为第二个参数,所以我认为他们的示例有缺陷。

修复方法是按照编译器所说的去做,并使用一个实际存在的重载:

CSConfig.SetDB(dbName, () => {
    CSDatabase.ExecuteNonQuery(
        @"CREATE TABLE person 
        (PersonID INTEGER PRIMARY KEY AUTOINCREMENT,
         Name TEXT(50) NOT NULL,
         DateOfBirth TEXT(30) NULL)");
});

好吧,交易到此为止。这个例子是错误的,显然这个项目是开源的。

在最新版本中也是如此。bool没有过载。请参阅源代码片段。


using System;
using System.IO;
using Mono.Data.Sqlite;
namespace Vici.CoolStorage
{
    [Flags]
    public enum SqliteOption
    {
        None = 0,
        CreateIfNotExists = 1,
        UseConnectionPooling = 2
    }
    public static partial class CSConfig
    {
        public static void SetDB(string dbName)
        {
            SetDB(dbName,SqliteOption.UseConnectionPooling);
        }
        public static void SetDB(string dbName, Action creationDelegate)
        {
            SetDB(dbName,SqliteOption.UseConnectionPooling|SqliteOption.CreateIfNotExists, creationDelegate);
        }
        public static void SetDB(string dbName, SqliteOption sqliteOption)
        {
            SetDB(dbName,sqliteOption,null);
        }
        public static void SetDB(string dbName, SqliteOption sqliteOption, Action creationDelegate)
        {
            bool exists = File.Exists(dbName);
            bool createIfNotExists = (sqliteOption & SqliteOption.CreateIfNotExists) != 0;
            bool usePooling = (sqliteOption & SqliteOption.UseConnectionPooling) != 0;
            if (!exists && createIfNotExists)
            SqliteConnection.CreateFile(dbName);
            SetDB(new CSDataProviderSQLite("Data Source=" + dbName + ";Pooling=" + usePooling), DEFAULT_CONTEXTNAME);
            if (!exists && createIfNotExists && creationDelegate != null)
                creationDelegate();
        }
    }
}