· 8 years ago · Aug 26, 2018, 06:50 AM
1Using Vici cool Storage with monodroid
2string dbName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "mydb.db3");
3
4// The following line will tell CoolStorage where the database is,
5// create it if it does not exist, and call a delegate which
6// creates the necessary tables (only if the database file was
7// created new)
8
9CSConfig.SetDB(dbName, true, () => {
10 CSDatabase.ExecuteNonQuery(@"CREATE TABLE person
11 (PersonID INTEGER PRIMARY KEY AUTOINCREMENT,
12 Name TEXT(50) NOT NULL,
13 DateOfBirth TEXT(30) NULL)");
14
15});
16
17public static void SetDB (CSDataProvider db);
18public static void SetDB (CSDataProvider db, string contextName);
19public static void SetDB (string dbName);
20public static void SetDB (string dbName, Action creationDelegate);
21public static void SetDB (string dbName, SqliteOption sqliteOption);
22public static void SetDB (string dbName, SqliteOption sqliteOption, Action creationDelegate);
23
24CSConfig.SetDB(dbName, () => {
25 CSDatabase.ExecuteNonQuery(
26 @"CREATE TABLE person
27 (PersonID INTEGER PRIMARY KEY AUTOINCREMENT,
28 Name TEXT(50) NOT NULL,
29 DateOfBirth TEXT(30) NULL)");
30});
31
32using System;
33using System.IO;
34using Mono.Data.Sqlite;
35
36namespace Vici.CoolStorage
37{
38 [Flags]
39 public enum SqliteOption
40 {
41 None = 0,
42 CreateIfNotExists = 1,
43 UseConnectionPooling = 2
44 }
45
46 public static partial class CSConfig
47 {
48 public static void SetDB(string dbName)
49 {
50 SetDB(dbName,SqliteOption.UseConnectionPooling);
51 }
52
53 public static void SetDB(string dbName, Action creationDelegate)
54 {
55 SetDB(dbName,SqliteOption.UseConnectionPooling|SqliteOption.CreateIfNotExists, creationDelegate);
56 }
57
58 public static void SetDB(string dbName, SqliteOption sqliteOption)
59 {
60 SetDB(dbName,sqliteOption,null);
61 }
62
63 public static void SetDB(string dbName, SqliteOption sqliteOption, Action creationDelegate)
64 {
65 bool exists = File.Exists(dbName);
66 bool createIfNotExists = (sqliteOption & SqliteOption.CreateIfNotExists) != 0;
67 bool usePooling = (sqliteOption & SqliteOption.UseConnectionPooling) != 0;
68
69 if (!exists && createIfNotExists)
70 SqliteConnection.CreateFile(dbName);
71
72 SetDB(new CSDataProviderSQLite("Data Source=" + dbName + ";Pooling=" + usePooling), DEFAULT_CONTEXTNAME);
73
74 if (!exists && createIfNotExists && creationDelegate != null)
75 creationDelegate();
76 }
77 }
78}