Socket
Socket
Sign inDemoInstall

@capacitor-community/sqlite

Package Overview
Dependencies
Maintainers
15
Versions
241
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@capacitor-community/sqlite - npm Package Compare versions

Comparing version 2.4.2 to 2.9.0-alpha.1

android/.gradle/5.6.4/executionHistory/executionHistory.bin

9

CHANGELOG.md

@@ -0,1 +1,10 @@

## 2.9.0-alpha.1 (2020-11-22) REFACTOR
### Added Features Android platform only
- createConnection method
- closeConnection method
- SQLiteConnection Interface & Class
- SQLiteDBConnection Interface & Class
## 2.4.2 (2020-11-22)

@@ -2,0 +11,0 @@

96

dist/esm/definitions.d.ts

@@ -6,8 +6,25 @@ declare module '@capacitor/core' {

}
/**
* CapacitorSQLitePlugin Interface
*/
export interface CapacitorSQLitePlugin {
/**
* create a database connection
* @param options capConnectionOptions
* @return Promise<capSQLiteResult>
* @since 2.9.0 refactor
*/
createConnection(options: capConnectionOptions): Promise<capSQLiteResult>;
/**
* close a database connection
* @param options capSQLiteOptions
* @return Promise<capSQLiteResult>
* @since 2.9.0 refactor
*/
closeConnection(options: capSQLiteOptions): Promise<capSQLiteResult>;
/**
* Echo a given string
*
* @param options: capEchoOptions
* @return Promise<{ value: string }
* @return Promise<capEchoResult>
* @since 0.0.1

@@ -120,3 +137,3 @@ */

}
export interface capSQLiteOptions {
export interface capConnectionOptions {
/**

@@ -132,3 +149,2 @@ * The database name

* Set to true (database encryption) / false
* - Open method only
*/

@@ -139,8 +155,17 @@ encrypted?: boolean;

* ["encryption", "secret", "newsecret"]
* - Open method only
*/
mode?: string;
}
export interface capSQLiteOptions {
/**
* The database name
*/
database?: string;
}
export interface capSQLiteExecuteOptions {
/**
* The database name
*/
database?: string;
/**
* The batch of raw SQL statements as string

@@ -152,2 +177,6 @@ */

/**
* The database name
*/
database?: string;
/**
* The batch of raw SQL statements as Array of capSQLLiteSet

@@ -159,2 +188,6 @@ */

/**
* The database name
*/
database?: string;
/**
* A statement

@@ -170,2 +203,6 @@ */

/**
* The database name
*/
database?: string;
/**
* A statement

@@ -188,2 +225,6 @@ */

/**
* The database name
*/
database?: string;
/**
* Set the mode to export JSON Object:

@@ -197,2 +238,6 @@ * "full" or "partial"

/**
* The database name
*/
database?: string;
/**
* Set the synchronization date

@@ -341,1 +386,44 @@ *

}
/**
* SQLiteConnection Interface
*/
export interface ISQLiteConnection {
echo(value: string): Promise<capEchoResult>;
createConnection(database: string, encrypted: boolean, mode: string, version: number): Promise<SQLiteDBConnection | null>;
closeConnection(database: string): Promise<capSQLiteResult>;
}
/**
* SQLiteConnection Class
*/
export declare class SQLiteConnection implements ISQLiteConnection {
private sqlite;
constructor(sqlite: any);
echo(value: string): Promise<capEchoResult>;
createConnection(database: string, encrypted: boolean, mode: string, version: number): Promise<SQLiteDBConnection | null>;
closeConnection(database: string): Promise<capSQLiteResult>;
}
/**
* SQLiteDBConnection Interface
*/
export interface ISQLiteDBConnection {
open(): Promise<capSQLiteResult>;
close(): Promise<capSQLiteResult>;
execute(statements: string): Promise<capSQLiteChanges>;
query(statement: string, values?: Array<string>): Promise<capSQLiteValues>;
run(statement: string, values?: Array<any>): Promise<capSQLiteChanges>;
executeSet(set: Array<capSQLiteSet>): Promise<capSQLiteChanges>;
}
/**
* SQLiteDBConnection Class
*/
export declare class SQLiteDBConnection implements ISQLiteDBConnection {
private dbName;
private sqlite;
constructor(dbName: string, sqlite: any);
open(): Promise<capSQLiteResult>;
close(): Promise<capSQLiteResult>;
execute(statements: string): Promise<capSQLiteChanges>;
query(statement: string, values?: Array<string>): Promise<capSQLiteValues>;
run(statement: string, values?: Array<any>): Promise<capSQLiteChanges>;
executeSet(set: Array<capSQLiteSet>): Promise<capSQLiteChanges>;
}

@@ -0,1 +1,127 @@

var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
/**
* SQLiteConnection Class
*/
export class SQLiteConnection {
constructor(sqlite) {
this.sqlite = sqlite;
}
echo(value) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.sqlite.echo({ value: value });
});
}
createConnection(database, encrypted, mode, version) {
return __awaiter(this, void 0, void 0, function* () {
const res = yield this.sqlite.createConnection({
database,
encrypted,
mode,
version,
});
console.log('>>> in SQLiteConnection createConnection ' + res.result);
if (res.result) {
return new SQLiteDBConnection(database, this.sqlite);
}
else {
return null;
}
});
}
closeConnection(database) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.sqlite.closeConnection({ database });
});
}
}
/**
* SQLiteDBConnection Class
*/
export class SQLiteDBConnection {
constructor(dbName, sqlite) {
this.dbName = dbName;
this.sqlite = sqlite;
console.log('>>> in SQLiteDBConnection dbName ' + dbName);
}
open() {
return __awaiter(this, void 0, void 0, function* () {
console.log('>>> in SQLiteDBConnection open dbName ' + this.dbName);
const res = yield this.sqlite.open({ database: this.dbName });
return res;
});
}
close() {
return __awaiter(this, void 0, void 0, function* () {
const res = yield this.sqlite.close({ database: this.dbName });
return res;
});
}
execute(statements) {
return __awaiter(this, void 0, void 0, function* () {
const res = yield this.sqlite.execute({
database: this.dbName,
statements: statements,
});
return res;
});
}
query(statement, values) {
return __awaiter(this, void 0, void 0, function* () {
let res;
if (values && values.length > 0) {
res = yield this.sqlite.query({
database: this.dbName,
statement: statement,
values: values,
});
}
else {
res = yield this.sqlite.query({
database: this.dbName,
statement: statement,
values: [],
});
}
return res;
});
}
//1234567890123456789012345678901234567890123456789012345678901234567890
run(statement, values) {
return __awaiter(this, void 0, void 0, function* () {
let res;
if (values && values.length > 0) {
res = yield this.sqlite.run({
database: this.dbName,
statement: statement,
values: values,
});
}
else {
res = yield this.sqlite.run({
database: this.dbName,
statement: statement,
values: [],
});
}
return res;
});
}
executeSet(set) {
return __awaiter(this, void 0, void 0, function* () {
const res = yield this.sqlite.executeSet({
database: this.dbName,
set: set,
});
return res;
});
}
}
//# sourceMappingURL=definitions.js.map

@@ -6,3 +6,5 @@ import { WebPlugin } from '@capacitor/core';

echo(options: capEchoOptions): Promise<capEchoResult>;
createConnection(options: capSQLiteOptions): Promise<any>;
open(options: capSQLiteOptions): Promise<capSQLiteResult>;
closeConnection(options: capSQLiteOptions): Promise<any>;
close(options: capSQLiteOptions): Promise<capSQLiteResult>;

@@ -9,0 +11,0 @@ execute(options: capSQLiteExecuteOptions): Promise<capSQLiteChanges>;

@@ -24,2 +24,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {

}
createConnection(options) {
return __awaiter(this, void 0, void 0, function* () {
console.log('createConnection', options);
return Promise.resolve({
result: false,
message: `Not implemented on Web Platform`,
});
});
}
open(options) {

@@ -34,2 +43,11 @@ return __awaiter(this, void 0, void 0, function* () {

}
closeConnection(options) {
return __awaiter(this, void 0, void 0, function* () {
console.log('closeConnection', options);
return Promise.resolve({
result: false,
message: `Not implemented on Web Platform`,
});
});
}
close(options) {

@@ -36,0 +54,0 @@ return __awaiter(this, void 0, void 0, function* () {

@@ -13,2 +13,129 @@ var capacitorPlugin = (function (exports, core) {

};
/**
* SQLiteConnection Class
*/
class SQLiteConnection {
constructor(sqlite) {
this.sqlite = sqlite;
}
echo(value) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.sqlite.echo({ value: value });
});
}
createConnection(database, encrypted, mode, version) {
return __awaiter(this, void 0, void 0, function* () {
const res = yield this.sqlite.createConnection({
database,
encrypted,
mode,
version,
});
console.log('>>> in SQLiteConnection createConnection ' + res.result);
if (res.result) {
return new SQLiteDBConnection(database, this.sqlite);
}
else {
return null;
}
});
}
closeConnection(database) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.sqlite.closeConnection({ database });
});
}
}
/**
* SQLiteDBConnection Class
*/
class SQLiteDBConnection {
constructor(dbName, sqlite) {
this.dbName = dbName;
this.sqlite = sqlite;
console.log('>>> in SQLiteDBConnection dbName ' + dbName);
}
open() {
return __awaiter(this, void 0, void 0, function* () {
console.log('>>> in SQLiteDBConnection open dbName ' + this.dbName);
const res = yield this.sqlite.open({ database: this.dbName });
return res;
});
}
close() {
return __awaiter(this, void 0, void 0, function* () {
const res = yield this.sqlite.close({ database: this.dbName });
return res;
});
}
execute(statements) {
return __awaiter(this, void 0, void 0, function* () {
const res = yield this.sqlite.execute({
database: this.dbName,
statements: statements,
});
return res;
});
}
query(statement, values) {
return __awaiter(this, void 0, void 0, function* () {
let res;
if (values && values.length > 0) {
res = yield this.sqlite.query({
database: this.dbName,
statement: statement,
values: values,
});
}
else {
res = yield this.sqlite.query({
database: this.dbName,
statement: statement,
values: [],
});
}
return res;
});
}
//1234567890123456789012345678901234567890123456789012345678901234567890
run(statement, values) {
return __awaiter(this, void 0, void 0, function* () {
let res;
if (values && values.length > 0) {
res = yield this.sqlite.run({
database: this.dbName,
statement: statement,
values: values,
});
}
else {
res = yield this.sqlite.run({
database: this.dbName,
statement: statement,
values: [],
});
}
return res;
});
}
executeSet(set) {
return __awaiter(this, void 0, void 0, function* () {
const res = yield this.sqlite.executeSet({
database: this.dbName,
set: set,
});
return res;
});
}
}
var __awaiter$1 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
class CapacitorSQLiteWeb extends core.WebPlugin {

@@ -22,3 +149,3 @@ constructor() {

echo(options) {
return __awaiter(this, void 0, void 0, function* () {
return __awaiter$1(this, void 0, void 0, function* () {
console.log('ECHO in Web plugin', options);

@@ -28,4 +155,13 @@ return options;

}
createConnection(options) {
return __awaiter$1(this, void 0, void 0, function* () {
console.log('createConnection', options);
return Promise.resolve({
result: false,
message: `Not implemented on Web Platform`,
});
});
}
open(options) {
return __awaiter(this, void 0, void 0, function* () {
return __awaiter$1(this, void 0, void 0, function* () {
console.log('open', options);

@@ -38,4 +174,13 @@ return Promise.resolve({

}
closeConnection(options) {
return __awaiter$1(this, void 0, void 0, function* () {
console.log('closeConnection', options);
return Promise.resolve({
result: false,
message: `Not implemented on Web Platform`,
});
});
}
close(options) {
return __awaiter(this, void 0, void 0, function* () {
return __awaiter$1(this, void 0, void 0, function* () {
console.log('close', options);

@@ -49,3 +194,3 @@ return Promise.resolve({

execute(options) {
return __awaiter(this, void 0, void 0, function* () {
return __awaiter$1(this, void 0, void 0, function* () {
console.log('execute', options);

@@ -59,3 +204,3 @@ return Promise.resolve({

executeSet(options) {
return __awaiter(this, void 0, void 0, function* () {
return __awaiter$1(this, void 0, void 0, function* () {
console.log('execute', options);

@@ -69,3 +214,3 @@ return Promise.resolve({

run(options) {
return __awaiter(this, void 0, void 0, function* () {
return __awaiter$1(this, void 0, void 0, function* () {
console.log('run', options);

@@ -79,3 +224,3 @@ return Promise.resolve({

query(options) {
return __awaiter(this, void 0, void 0, function* () {
return __awaiter$1(this, void 0, void 0, function* () {
console.log('query', options);

@@ -89,3 +234,3 @@ return Promise.resolve({

isDBExists(options) {
return __awaiter(this, void 0, void 0, function* () {
return __awaiter$1(this, void 0, void 0, function* () {
console.log('in Web isDBExists', options);

@@ -99,3 +244,3 @@ return Promise.resolve({

deleteDatabase(options) {
return __awaiter(this, void 0, void 0, function* () {
return __awaiter$1(this, void 0, void 0, function* () {
console.log('deleteDatabase', options);

@@ -109,3 +254,3 @@ return Promise.resolve({

isJsonValid(options) {
return __awaiter(this, void 0, void 0, function* () {
return __awaiter$1(this, void 0, void 0, function* () {
console.log('isJsonValid', options);

@@ -119,3 +264,3 @@ return Promise.resolve({

importFromJson(options) {
return __awaiter(this, void 0, void 0, function* () {
return __awaiter$1(this, void 0, void 0, function* () {
console.log('importFromJson', options);

@@ -129,3 +274,3 @@ return Promise.resolve({

exportToJson(options) {
return __awaiter(this, void 0, void 0, function* () {
return __awaiter$1(this, void 0, void 0, function* () {
console.log('exportToJson', options);

@@ -139,3 +284,3 @@ return Promise.resolve({

createSyncTable() {
return __awaiter(this, void 0, void 0, function* () {
return __awaiter$1(this, void 0, void 0, function* () {
console.log('createSyncTable');

@@ -149,3 +294,3 @@ return Promise.resolve({

setSyncDate(options) {
return __awaiter(this, void 0, void 0, function* () {
return __awaiter$1(this, void 0, void 0, function* () {
console.log('setSyncDate', options);

@@ -159,3 +304,3 @@ return Promise.resolve({

addUpgradeStatement(options) {
return __awaiter(this, void 0, void 0, function* () {
return __awaiter$1(this, void 0, void 0, function* () {
console.log('addUpgradeStatement', options);

@@ -174,2 +319,4 @@ return Promise.resolve({

exports.CapacitorSQLiteWeb = CapacitorSQLiteWeb;
exports.SQLiteConnection = SQLiteConnection;
exports.SQLiteDBConnection = SQLiteDBConnection;

@@ -176,0 +323,0 @@ return exports;

2

package.json
{
"name": "@capacitor-community/sqlite",
"version": "2.4.2",
"version": "2.9.0-alpha.1",
"description": "Capacitor SQLite Plugin",

@@ -5,0 +5,0 @@ "homepage": "https://github.com/capacitor-community/sqlite",

@@ -26,16 +26,27 @@ <p align="center"><br><img src="https://user-images.githubusercontent.com/236501/85893648-1c92e880-b7a8-11ea-926d-95355b8175c7.png" width="128" height="128" /></p>

## REFACTOR
The refactor will be a quite long process.
The aim of the refactor will be to allow
- for multiple database connections
- for a db connector allowing for easy commands `db.open(), db.close, ...`
- improve the response time of the encrypted database by removing the internal open and close database for each sqlite query
- moving to the latest `androidx.sqlite.db.xxx`
This was discussed lengthly in issue#1and issue#52
The first alpha release of the refactor will address the `android` platform only.
The test will be achieved on a Ionic/Angular app. For the other frameworks, it will require an update of the `react-sqlite-hook`and the `vue-sqlite-hook`.
## Installation
```bash
npm install @capacitor-community/sqlite
npm install @capacitor-community/sqlite@refactor
npx cap sync
npx cap add ios
npx cap add android
npx cap add @capacitor-community/electron
```
### iOS
- On iOS, no further steps are needed.
### Android

@@ -71,43 +82,2 @@

### Electron
- On Electron, go to the Electron folder of YOUR_APPLICATION
```bash
npm install --save sqlite3
npm install --save-dev @types/sqlite3
npm install --save-dev electron-rebuild
```
Modify the Electron package.json file by adding a script "postinstall"
```json
"scripts": {
"electron:start": "electron ./",
"postinstall": "electron-rebuild -f -w sqlite3"
},
```
Execute the postinstall script
```bash
npm run postinstall
```
#### Electron databases location
- There are by default under `User/Databases/APP_NAME/`
Then build YOUR_APPLICATION
```
npm run build
npx cap copy
npx cap copy @capacitor-community/electron
npx cap copy web
npx cap open android
npx cap open ios
npx cap open @capacitor-community/electron
```
## Configuration

@@ -121,19 +91,21 @@

| :---------------------- | :------ | :-- | :------- | :-- |
| open (non-encrypted DB) | ✅ | ✅ | ✅ | ❌ |
| open (encrypted DB) | ✅ | ✅ | ❌ | ❌ |
| close | ✅ | ✅ | ✅ | ❌ |
| execute | ✅ | ✅ | ✅ | ❌ |
| executeSet | ✅ | ✅ | ✅ | ❌ |
| run | ✅ | ✅ | ✅ | ❌ |
| query | ✅ | ✅ | ✅ | ❌ |
| deleteDatabase | ✅ | ✅ | ✅ | ❌ |
| importFromJson | ✅ | ✅ | ✅ | ❌ |
| exportToJson | ✅ | ✅ | ✅ | ❌ |
| createSyncTable | ✅ | ✅ | ✅ | ❌ |
| setSyncDate | ✅ | ✅ | ✅ | ❌ |
| isJsonValid | ✅ | ✅ | ✅ | ❌ |
| isDBExists | ✅ | ✅ | ✅ | ❌ |
| addUpgradeStatement | ✅ | ✅ | ✅ | ❌ |
| createConnection | ✅ | ❌ | ❌ | ❌ |
| closeConnection | ✅ | ❌ | ❌ | ❌ |
| open (non-encrypted DB) | ✅ | ❌ | ❌ | ❌ |
| open (encrypted DB) | ✅ | ❌ | ❌ | ❌ |
| close | ✅ | ❌ | ❌ | ❌ |
| execute | ✅ | ❌ | ❌ | ❌ |
| executeSet | ✅ | ❌ | ❌ | ❌ |
| run | ✅ | ❌ | ❌ | ❌ |
| query | ✅ | ❌ | ❌ | ❌ |
| deleteDatabase | ❌ | ❌ | ❌ | ❌ |
| importFromJson | ❌ | ❌ | ❌ | ❌ |
| exportToJson | ❌ | ❌ | ❌ | ❌ |
| createSyncTable | ❌ | ❌ | ❌ | ❌ |
| setSyncDate | ❌ | ❌ | ❌ | ❌ |
| isJsonValid | ❌ | ❌ | ❌ | ❌ |
| isDBExists | ✅ | ❌ | ❌ | ❌ |
| addUpgradeStatement | ❌ | ❌ | ❌ | ❌ |
## Documentation
## Documentation (to be updated)

@@ -150,18 +122,10 @@ [API_Documentation](https://github.com/capacitor-community/sqlite/blob/master/docs/API.md)

- [angular-sqlite-app-starter](https://github.com/jepiqueau/angular-sqlite-app-starter)
- [angular-sqlite-app-refactor](https://github.com/jepiqueau/angular-sqlite-app-refactor)
- [test-angular-jeep-capacitor-plugins](https://github.com/jepiqueau/capacitor-apps/tree/master/IonicAngular/jeep-test-app)
### Ionic/React (to come later)
### Ionic/React
### Ionic/Vue (to come later)
- [react-sqlite-app-starter](https://github.com/jepiqueau/react-sqlite-app-starter)
### Vue (to come later)
### Ionic/Vue
- [vue-sqlite-app-starter](https://github.com/jepiqueau/vue-sqlite-app-starter)
### Vue
- [vue-sqlite-app](https://github.com/jepiqueau/vue-sqlite-app)
## Usage

@@ -171,126 +135,238 @@

- In your code
- In your Ionic/Angular App
define a service **app/services/sqlite.service.ts**
```ts
import { Plugins, Capacitor } from '@capacitor/core';
import '@capacitor-community/sqlite';
const { CapacitorSQLite } = Plugins;
import { Injectable } from '@angular/core';
@Component( ... )
export class MyPage {
_sqlite: any;
_platform: string;
_isPermission: boolean = true;
import { Plugins, Capacitor } from '@capacitor/core';
import '@capacitor-community/sqlite';
import {
SQLiteDBConnection,
SQLiteConnection,
capEchoResult,
capSQLiteResult,
} from '@capacitor-community/sqlite';
const { CapacitorSQLite } = Plugins;
...
@Injectable({
providedIn: 'root',
})
export class SQLiteService {
handlerPermissions: any;
sqlite: SQLiteConnection;
isService: boolean = false;
platform: string;
ngAfterViewInit()() {
this._platform = Capacitor.platform;
this._sqlite = CapacitorSQLite;
if (this._platform === 'android') {
const handlerPermissions = this.sqlite.addListener(
'androidPermissionsRequest', async (data:any) => {
if (data.permissionGranted === 1) {
this._isPermission = true;
} else {
this._isPermission = false;
constructor() {}
/**
* Plugin Initialization
*/
initializePlugin(): Promise<boolean> {
return new Promise(resolve => {
this.platform = Capacitor.platform;
console.log('*** platform ' + this.platform);
const sqlitePlugin: any = CapacitorSQLite;
if (this.platform === 'android') {
this.handlerPermissions = sqlitePlugin.addListener(
'androidPermissionsRequest',
async (data: any) => {
if (data.permissionGranted === 1) {
this.handlerPermissions.remove();
this.sqlite = new SQLiteConnection(sqlitePlugin);
resolve(true);
} else {
console.log('Permission not granted');
this.handlerPermissions.remove();
this.sqlite = null;
resolve(false);
}
},
);
try {
sqlitePlugin.requestPermissions();
} catch (e) {
console.log('Error requesting permissions ' + JSON.stringify(e));
resolve(false);
}
});
try {
this.sqlite.requestPermissions();
} catch (e) {
console.log('Error requesting permissions!' + JSON.stringify(e));
} else {
this.sqlite = new SQLiteConnection(sqlitePlugin);
resolve(true);
}
});
}
async echo(value: string): Promise<capEchoResult> {
if (this.sqlite != null) {
return await this.sqlite.echo(value);
} else {
return null;
}
...
}
async testSQLitePlugin(): Promise<void> {
if(!this._isPermission) {
console.log("Android permissions not granted");
return;
async createConnection(
database: string,
encrypted: boolean,
mode: string,
version: number,
): Promise<SQLiteDBConnection | null> {
if (this.sqlite != null) {
const db: SQLiteDBConnection = await this.sqlite.createConnection(
database,
encrypted,
mode,
version,
);
if (db != null) {
return db;
} else {
return null;
}
let result:any = await this._sqlite.open({database:"testsqlite"});
retOpenDB = result.result;
if(retOpenDB) {
// Create Tables if not exist
let sqlcmd: string = `
BEGIN TRANSACTION;
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY NOT NULL,
email TEXT UNIQUE NOT NULL,
name TEXT,
FirstName TEXT,
age INTEGER,
MobileNumber TEXT
);
PRAGMA user_version = 1;
COMMIT TRANSACTION;
`;
var retExe: any = await this._sqlite.execute({statements:sqlcmd});
console.log('retExe ',retExe.changes.changes);
// Insert some Users
sqlcmd = `
BEGIN TRANSACTION;
DELETE FROM users;
INSERT INTO users (name,email,age) VALUES ("Whiteley","Whiteley.com",30);
INSERT INTO users (name,email,age) VALUES ("Jones","Jones.com",44);
COMMIT TRANSACTION;
`;
retExe = await this._sqlite.execute({statements:sqlcmd});
// will print the changes 2 in that case
console.log('retExe ',retExe.changes.changes);
// Select all Users
sqlcmd = "SELECT * FROM users";
const retSelect: any = await this._sqlite.query({statement:sqlcmd,values:[]});
console.log('retSelect.values.length ',retSelect.values.length);
const row1: any = retSelect.values[0];
console.log("row1 users ",JSON.stringify(row1))
const row2: any = retSelect.values[1];
console.log("row2 users ",JSON.stringify(row2))
} else {
return null;
}
}
async closeConnection(database: string): Promise<capSQLiteResult> {
if (this.sqlite != null) {
return await this.sqlite.closeConnection(database);
} else {
return null;
}
}
}
```
// Insert a new User with SQL and Values
Then implement a component (for example the **app/home/home.page.ts** )
sqlcmd = "INSERT INTO users (name,email,age) VALUES (?,?,?)";
let values: Array<any> = ["Simpson","Simpson@example.com",69];
var retRun: any = await this._sqlite.run({statement:sqlcmd,values:values});
console.log('retRun ',retRun.changes.changes,retRun.changes.lastId);
```ts
import { Component, AfterViewInit } from '@angular/core';
import { SQLiteService } from '../services/sqlite.service';
import { createSchema, twoUsers } from '../utils/no-encryption-utils';
import {
createSchemaContacts,
setContacts,
} from '../utils/encrypted-set-utils';
// Select Users with age > 35
sqlcmd = "SELECT name,email,age FROM users WHERE age > ?";
retSelect = await this._sqlite.query({statement:sqlcmd,values:["35"]});
console.log('retSelect ',retSelect.values.length);
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage implements AfterViewInit {
sqlite: any;
platform: string;
handlerPermissions: any;
initPlugin: boolean = false;
// Execute a Set of raw SQL Statements
let set: Array<any> = [
{ statement:"INSERT INTO users (name,FirstName,email,age,MobileNumber) VALUES (?,?,?,?,?);",
values:["Blackberry","Peter","Blackberry@example.com",69,"4405060708"]
},
{ statement:"INSERT INTO users (name,FirstName,email,age,MobileNumber) VALUES (?,?,?,?,?);",
values:["Jones","Helen","HelenJones@example.com",42,"4404030201"]
},
{ statement:"INSERT INTO users (name,FirstName,email,age,MobileNumber) VALUES (?,?,?,?,?);",
values:["Davison","Bill","Davison@example.com",45,"4405162732"]
},
{ statement:"INSERT INTO users (name,FirstName,email,age,MobileNumber) VALUES (?,?,?,?,?);",
values:["Brown","John","Brown@example.com",35,"4405243853"]
},
{ statement:"UPDATE users SET age = ? , MobileNumber = ? WHERE id = ?;",
values:[51,"4404030237",2]
}
];
result = await this._sqlite.executeSet({set:set});
console.log("result.changes.changes ",result.changes.changes);
if(result.changes.changes != 5) return;
constructor(private _sqlite: SQLiteService) {}
async ngAfterViewInit() {
// Initialize the CapacitorSQLite plugin
this.initPlugin = await this._sqlite.initializePlugin();
const result: boolean = await this.runTest();
if (result) {
document.querySelector('.sql-allsuccess').classList.remove('display');
console.log('$$$ runTest was successful');
} else {
document.querySelector('.sql-allfailure').classList.remove('display');
console.log('$$$ runTest failed');
}
}
...
} else {
console.log("Error: Open database failed");
return;
}
}
...
}
async runTest(): Promise<boolean> {
let result: any = await this._sqlite.echo('Hello World');
console.log(' from Echo ' + result.value);
// initialize the connection
const db = await this._sqlite.createConnection(
'testNew',
false,
'no-encryption',
1,
);
const db1 = await this._sqlite.createConnection(
'testSet',
true,
'secret',
1,
);
// open db
let ret: any = await db.open();
if (!ret.result) {
return false;
}
// create tables in db
ret = await db.execute(createSchema);
console.log('$$$ ret.changes.changes in db ' + ret.changes.changes);
if (ret.changes.changes < 0) {
return false;
}
// add two users in db
ret = await db.execute(twoUsers);
if (ret.changes.changes !== 2) {
return false;
}
// select all users in db
ret = await db.query('SELECT * FROM users;');
if (
ret.values.length !== 2 ||
ret.values[0].name !== 'Whiteley' ||
ret.values[1].name !== 'Jones'
) {
return false;
}
// open db1
ret = await db1.open();
if (!ret.result) {
return false;
}
// create tables in db1
ret = await db1.execute(createSchemaContacts);
console.log('$$$ ret.changes.changes in db1' + ret.changes.changes);
if (ret.changes.changes < 0) {
return false;
}
// load setContacts in db1
ret = await db1.executeSet(setContacts);
console.log('$$$ ret.changes.changes in db2' + ret.changes.changes);
if (ret.changes.changes !== 5) {
return false;
}
// select users where company is NULL in db
ret = await db.query('SELECT * FROM users WHERE company IS NULL;');
if (
ret.values.length !== 2 ||
ret.values[0].name !== 'Whiteley' ||
ret.values[1].name !== 'Jones'
) {
return false;
}
// add one user with statement and values
let sqlcmd: string = 'INSERT INTO users (name,email,age) VALUES (?,?,?)';
let values: Array<any> = ['Simpson', 'Simpson@example.com', 69];
ret = await db.run(sqlcmd, values);
console.log();
if (ret.changes.lastId !== 3) {
return false;
}
// add one user with statement
sqlcmd =
`INSERT INTO users (name,email,age) VALUES ` +
`("Brown","Brown@example.com",15)`;
ret = await db.run(sqlcmd);
if (ret.changes.lastId !== 4) {
return false;
}
ret = await this._sqlite.closeConnection('testNew');
if (!ret.result) {
return false;
}
ret = await this._sqlite.closeConnection('testSet');
if (!ret.result) {
return false;
} else {
return true;
}
}
}
```

@@ -297,0 +373,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc