You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
And call this function in your executable projects (.net, ios, android, uwp, mac, ...):
7
+
Also add one of the https://github.com/ericsink/SQLitePCL.raw package of your choice to the netstandard project:
8
+
- SQLitePCLRaw.bundle_e_sqlite3 for a normal database file
9
+
- SQLitePCLRaw.bundle_e_sqlcipher for a crypted database file
10
+
11
+
And call this statup function in each of your platform projects:
8
12
9
13
```
10
14
SQLitePCL.Batteries_V2.Init()
11
15
```
12
16
17
+
For a simple key/value store based on sqlite, or a drop-in replacement (alternative) to the unstable Akavache, check https://github.com/softlion/KeyValueLite
13
18
14
-
See https://github.com/ericsink/SQLitePCL.raw/ for more information on how to use SQLitePCL.raw
15
-
16
-
If you search a simple key value store based on sqlite, alternative to Akavache, check https://github.com/softlion/KeyValueLite
19
+
# Features
17
20
18
-
# Changes
19
-
20
-
* Netstandard 2.0 only
21
+
* Netstandard 2+
21
22
* Uses SQLitePCLRaw for sqlite raw communication
22
-
* Requires SQLitePCL.raw v2, make sure to read the release notes on the SQLitePCL project page
23
+
* Compatible with SQLitePCLRaw standard and cypher
24
+
* Stable and used in tons of apps
23
25
24
-
# New Features compared to oysteinkrog
26
+
# Other Features (compared to oysteinkrog)
25
27
26
-
Multiple primary key support
28
+
* Multiple primary key support
27
29
Ex:
28
-
30
+
```
29
31
public class PrivacyGroupItem
30
32
{
31
33
[PrimaryKey]
@@ -36,85 +38,126 @@ If you search a simple key value store based on sqlite, alternative to Akavache,
36
38
}
37
39
38
40
db.Delete<PrivacyGroupItem>(groupId, contactId);
41
+
```
39
42
40
-
41
-
Projections now have the expected result type
43
+
* Projections now have the expected result type
42
44
Ex: `IEnumerable<int> ids = from pgi in db.Table<PrivacyGroupItem>() where pgi.PrivacyGroupId == groupId select pgi.ContactId;`
43
45
44
-
New method to query simple types (ie: string, ...) as Query<T> can query only complex types (ie: T must be a class/stuct with a default constructor)
46
+
* New method to query simple types (ie: string, ...) as Query<T> can query only complex types (ie: T must be a class/stuct with a default constructor)
Usage: `ExecuteSimpleQuery<string>("select 'drop table ' || name || ';' from sqlite_master where type = 'table'")`
47
49
50
+
* No asynchronous API. Use Task.Run() if you want asynchronous calls.
51
+
Note that while SQLitePCLRaw states that the database can be accessed by mutiple threads simultaneously, experience proves that you should always prevent multithread access, otherwise rare random crash occur. You can use `SemaphoreSlim` to serialize calls.
52
+
53
+
* Another trick
54
+
Use transactions! In sqlite they speed up all queries a lot.
55
+
48
56
# Original Fork
49
57
50
58
https://github.com/praeclarum/sqlite-net
51
59
52
-
# Examples
60
+
# Usage
53
61
54
-
Please consult the source code (see unit tests) for more examples.
62
+
Note: see unit tests for more examples.
55
63
56
-
The library contains simple attributes that you can use to control the construction of tables. In a simple stock program, you might use:
64
+
## Define the database schema using a code first approach.
57
65
58
-
public class Stock
66
+
```csharp
67
+
publicclassDbStock
59
68
{
60
69
[PrimaryKey, AutoIncrement]
61
70
publicintId { get; set; }
71
+
62
72
[MaxLength(8)]
63
73
publicstringSymbol { get; set; }
64
74
}
65
75
66
-
public class Valuation
76
+
publicclassDbValuation
67
77
{
68
78
[PrimaryKey, AutoIncrement]
69
79
publicintId { get; set; }
70
-
[Indexed]
80
+
81
+
[Indexed()]
71
82
publicintStockId { get; set; }
83
+
84
+
[Indexed("Stock",1)] //This defines an index with multiple keys
Once you have defined your entity, you can automatically generate tables in your database by calling `CreateTable`:
122
+
Simple add, update and delete:
85
123
86
-
var db = new SQLiteConnection(sqlitePlatform, "foofoo");
87
-
db.CreateTable<Stock>();
88
-
db.CreateTable<Valuation>();
124
+
```csharp
125
+
varstock=newDbStock() { Symbol="EUR" };
126
+
db.Insert(stock);
127
+
stock.Symbol="USD";
128
+
db.Update(stock);
129
+
db.Delete(stock);
130
+
131
+
db.DeleteAll(allStocks);
89
132
90
-
You can insert rows in the database using `Insert`. If the table contains an auto-incremented primary key, then the value for that key will be available to you after the insert:
133
+
//Delete by id
134
+
db.DeleteIn<DbDay>(new[] {1, 2, 3});
91
135
92
-
public static void AddStock(SQLiteConnection db, string symbol) {
93
-
var s = db.Insert(new Stock() {
94
-
Symbol = symbol
95
-
});
96
-
Console.WriteLine("{0} == {1}", s.Symbol, s.Id);
97
-
}
136
+
```
98
137
99
-
Similar methods exist for `Update` and `Delete`.
138
+
After the Insert call, stock.Id will be set, because Id has the AutoIncrement attribute.
100
139
101
-
The most straightforward way to query for data is using the `Table` method. This can take predicates for constraining via WHERE clauses and/or adding ORDER BY clauses:
140
+
Simple query using LINQ. Most linq operators work:
102
141
103
-
var conn = new SQLiteConnection(sqlitePlatform, "foofoo");
104
-
var query = conn.Table<Stock>().Where(v => v.Symbol.StartsWith("A"));
142
+
```csharp
143
+
varstocksStartingWithA=db.Table<DbStock>()
144
+
.Where(stock=>stock.Symbol.StartsWith("A"))
145
+
.OrderBy(stock=>stock.Symbol)
146
+
.ToList();
105
147
106
-
foreach (var stock in query)
107
-
Debug.WriteLine("Stock: " + stock.Symbol);
148
+
varallStocks=db.Table<DbStock>().ToList();
149
+
```
108
150
109
-
You can also query the database at a low-level using the `Query` method:
151
+
Advanced queries using SQL:
110
152
111
-
public static IEnumerable<Valuation> QueryValuations (SQLiteConnection db, Stock stock)
112
-
{
113
-
return db.Query<Valuation> ("select * from Valuation where StockId = ?", stock.Id);
114
-
}
153
+
```csharp
154
+
vardbValuation=db.Query<DbValuation> ("select * from DbValuation where StockId = ?", stock.Id);
155
+
db.Execute("delete * from DbValuation where StockId = ?", stock.Id);
156
+
```
115
157
116
-
The generic parameter to the `Query` method specifies the type of object to create for each row. It can be one of your table classes, or any other class whose public properties match the column returned by the query. For instance, we could rewrite the above query as:
158
+
The T in `db.Query<T>`specifies the object to create for each row. It can be a table class, or any other class whose public properties match the query columns.
117
159
160
+
```csharp
118
161
publicclassVal {
119
162
publicdecimalMoney { get; set; }
120
163
publicDateTimeDate { get; set; }
@@ -123,9 +166,4 @@ The generic parameter to the `Query` method specifies the type of object to crea
123
166
{
124
167
returndb.Query<Val> ("select 'Price' as 'Money', 'Time' as 'Date' from Valuation where StockId = ?", stock.Id);
125
168
}
126
-
127
-
You can perform low-level updates of the database using the `Execute` method.
128
-
129
-
## Asynchronous API
130
-
131
-
The asynchronous API has been removed, as it was only wrapping synchronous methods in Task.Run(), which has nasty side effects as multiple Tasks are queued. Use your own Task.Run to achieve the same effect.
0 commit comments