Using Vici cool Storage with monodroid
Hey, At the moment I'm not sure how officially supported it is but, there have been reports of people successfully using monodroid with vici coolStorage. I have been able to drop the assemblies into my project and compile however, certain classes throw compile time errors when I attempt to use them. specially when attempting to connect like the example for monoTouch on the website..
string dbName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "mydb.db3");
// The following line will tell CoolStorage where the data开发者_如何学运维base 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)");
});
I get no intellisense when attempting to use CSConfig's methods and when I try to pass the 3 args to CSConfig.SetDB() I get an invalid number of args error.
I think their sample is buggy. If you use the Visual Studio assembly browser, or the MonoDevelop assembly browser, or even just monop -r:Vici.CoolStorage.MT.dll Vici.CoolStorage.CSConfig
, you'll see these overloads for 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);
None of these accept bool
as a second parameter, so I think their sample is buggy.
The fix is to do as the compiler says, and use an overload that actually exists:
CSConfig.SetDB(dbName, () => {
CSDatabase.ExecuteNonQuery(
@"CREATE TABLE person
(PersonID INTEGER PRIMARY KEY AUTOINCREMENT,
Name TEXT(50) NOT NULL,
DateOfBirth TEXT(30) NULL)");
});
Okay so here is the deal. The example is wrong and apparently the project is open source.
So in the latest version. there is no overload for bool. See source snippet.
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();
}
}
}
精彩评论