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
Copy file name to clipboardExpand all lines: README.md
+50-18Lines changed: 50 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,6 +22,8 @@ You can find the [benchmarking code in the example app](https://github.com/OP-En
22
22
23
23
Memory consumption is also 1/4 compared to `react-native-quick-sqlite`. This query used to take 1.2 GB of peak memory usage, and now runs in 250mbs.
24
24
25
+
You can also turn on Memory Mapping to make your queries even faster by skipping the kernel during I/O, this comes with some disadvantages though. If you want even more speed and you can re-use your queries you can use prepared statements.
26
+
25
27
# Encryption
26
28
27
29
If you need to encrypt your entire database, there is [`op-sqlcipher`](https://github.com/OP-Engineering/op-sqlcipher), which is a fork of this library that uses [SQLCipher](https://github.com/sqlcipher/sqlcipher). It completely encrypts the database with minimal overhead.
@@ -102,6 +104,21 @@ const largeDb = open({
102
104
});
103
105
```
104
106
107
+
# Speed
108
+
109
+
op-sqlite is already the fastest solution it can be, but it doesn't mean you cannot tweak SQLite to be faster (at the cost of some disadvantages). One possible tweak is turning on [Memory Mapping](https://www.sqlite.org/mmap.html). It allows to read/write to/from the disk without going through the kernel. However, if your queries throw an error your application might crash.
110
+
111
+
To turn on Memory Mapping, execute the following pragma statement after opening a db:
112
+
113
+
```ts
114
+
const db =open({
115
+
name: 'mydb.sqlite',
116
+
});
117
+
118
+
// 0 turns of memory mapping, any other number enables it with the cache size
119
+
db.execute('PRAGMA mmap_size=268435456');
120
+
```
121
+
105
122
# API
106
123
107
124
```typescript
@@ -134,7 +151,7 @@ db = {
134
151
}
135
152
```
136
153
137
-
###Simple queries
154
+
## Simple queries
138
155
139
156
The basic query is **synchronous**, it will block rendering on large operations, further below you will find async versions.
140
157
@@ -162,7 +179,7 @@ try {
162
179
}
163
180
```
164
181
165
-
###Multiple statements in a single string
182
+
## Multiple statements in a single string
166
183
167
184
You can execute multiple statements in a single operation. The API however is not really thought for this use case and the results (and their metadata) will be mangled, so you can discard it.
You might have too much SQL to process and it will cause your application to freeze. There are async versions for some of the operations. This will offload the SQLite processing to a different thread.
259
276
@@ -267,7 +284,7 @@ db.executeAsync(
267
284
);
268
285
```
269
286
270
-
###Blobs
287
+
## Blobs
271
288
272
289
Blobs are supported via `ArrayBuffer`, you need to be careful about the semantics though. You cannot instantiate an instance of `ArrayBuffer` directly, nor pass a typed array directly. Here is an example:
273
290
@@ -295,7 +312,22 @@ const result = db.execute('SELECT content FROM BlobTable');
A lot of the work when executing queries is not iterating through the result set itself but, sometimes, planning the execution. If you have a query which is expensive but you can re-use (even if you have to change the arguments) you can use a `prepared statement`:
318
+
319
+
```ts
320
+
const statement =db.prepareStatement('SELECT * FROM User WHERE name = ?;');
321
+
statement.bind(['Oscar']);
322
+
let results1 =statement.execute();
323
+
324
+
statement.bind(['Carlos']);
325
+
let results2 =statement.execute();
326
+
```
327
+
328
+
You only pay the price of parsing the query once, and each subsequent execution should be faster.
329
+
330
+
# Attach or Detach other databases
299
331
300
332
SQLite supports attaching or detaching other database files into your main database connection through an alias.
301
333
You can do any operation you like on this attached database like JOIN results across tables in different schemas, or update data or objects.
@@ -322,7 +354,7 @@ if (!detachResult.status) {
322
354
}
323
355
```
324
356
325
-
###Loading SQL Dump Files
357
+
# Loading SQL Dump Files
326
358
327
359
If you have a SQL dump file, you can load it directly, with low memory consumption:
On Android, it is not possible to link the OS SQLite. It is also a bad idea due to vendor changes, old android bugs, etc. Unfortunately, this means this library will add some megabytes to your app size.
408
440
409
-
##Enable compile-time options
441
+
# Enable compile-time options
410
442
411
443
By specifying pre-processor flags, you can enable optional features like FTS5, Geopoly, etc.
412
444
413
-
###iOS
445
+
## iOS
414
446
415
447
Add a `post_install` block to your `<PROJECT_ROOT>/ios/Podfile` like so:
416
448
@@ -429,26 +461,26 @@ end
429
461
Replace the `<SQLITE_FLAGS>` part with the flags you want to add.
430
462
For example, you could add `SQLITE_ENABLE_FTS5=1` to `GCC_PREPROCESSOR_DEFINITIONS` to enable FTS5 in the iOS project.
431
463
432
-
###Android
464
+
## Android
433
465
434
466
You can specify flags via `<PROJECT_ROOT>/android/gradle.properties` like so:
435
467
436
468
```
437
469
OPSQLiteFlags="-DSQLITE_ENABLE_FTS5=1"
438
470
```
439
471
440
-
##Additional configuration
472
+
# Additional configuration
441
473
442
-
###App groups (iOS only)
474
+
## App groups (iOS only)
443
475
444
476
On iOS, the SQLite database can be placed in an app group, in order to make it accessible from other apps in that app group. E.g. for sharing capabilities.
445
477
446
478
To use an app group, add the app group ID as the value for the `OPSQLite_AppGroup` key in your project's `Info.plist` file. You'll also need to configure the app group in your project settings. (Xcode -> Project Settings -> Signing & Capabilities -> Add Capability -> App Groups)
447
479
448
-
##Contribute
480
+
# Contribute
449
481
450
482
You need to have clang-format installed (`brew install clang-format`)
0 commit comments