Security News
Introducing the Socket Python SDK
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
@capacitor-community/sqlite
Advanced tools
@capacitor-community/sqlite
Capacitor community plugin for Native and Electron SQLite Databases. In Native databases could be encrypted with SQLCipher
A refactoring has been started more than a month ago to reach the following objectives:
db.open(), db.close, ...
androidx.sqlite.db.xxx
@journeyapps/sqlcipher
typeorm
or spatialite
drivers.This was discussed lengthly in issue#1 and issue#52
It is now available in a beta release 2.9.0-beta.1
for all platforms (Android, iOS & Electron).
Developers are encouraged to start looking at it and using it as this will have some impacts on yours developments. The interface to the plugin is now achieved through the use of connection wrappers
As you will see it is a Major
change and the release will become a 3.0.0
as soon as capacitor@3.0.0
will be released. So both interfaces to the plugin will be kept and maintained to that stage, after this, the refactor interface
will be released as the master and maintained.
The test has been achieved on:
Other frameworks will be tested later
vue-sqlite-hook
.When you will find issues, please report them with the REFACTOR
word at the start of the issue title.
To install it
npm i --save @capacitor-community/sqlite@refactor
Hope you will enjoy it.
Maintainer | GitHub | Social |
---|---|---|
QuΓ©au Jean Pierre | jepiqueau |
The plugin follows the guidelines from the Capacitor Team
,
meaning that it will not work in IE11 without additional JavaScript transformations, e.g. with Babel.
npm install @capacitor-community/sqlite
npx cap sync
npx cap add ios
npx cap add android
npx cap add @capacitor-community/electron
import com.getcapacitor.community.database.sqlite.CapacitorSQLite;
public class MainActivity extends BridgeActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Initializes the Bridge
this.init(
savedInstanceState,
new ArrayList<Class<? extends Plugin>>() {
{
// Additional plugins you've installed go here
// Ex: add(TotallyAwesomePlugin.class);
add(CapacitorSQLite.class);
}
}
);
}
}
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"
"scripts": {
"electron:start": "electron ./",
"postinstall": "electron-rebuild -f -w sqlite3"
},
Execute the postinstall script
npm run postinstall
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
No configuration required for this plugin
Name | Android | iOS | Electron | Web |
---|---|---|---|---|
open (non-encrypted DB) | β | β | β | β |
open (encrypted DB) | β | β | β | β |
close | β | β | β | β |
execute | β | β | β | β |
executeSet | β | β | β | β |
run | β | β | β | β |
query | β | β | β | β |
deleteDatabase | β | β | β | β |
importFromJson | β | β | β | β |
exportToJson | β | β | β | β |
createSyncTable | β | β | β | β |
setSyncDate | β | β | β | β |
isJsonValid | β | β | β | β |
isDBExists | β | β | β | β |
addUpgradeStatement | β | β | β | β |
ImportExportJson_Documentation
UpgradeDatabaseVersion_Documentation
In your code
import { Plugins, Capacitor } from '@capacitor/core';
import '@capacitor-community/sqlite';
const { CapacitorSQLite } = Plugins;
@Component( ... )
export class MyPage {
_sqlite: any;
_platform: string;
_isPermission: boolean = true;
...
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;
}
});
try {
this.sqlite.requestPermissions();
} catch (e) {
console.log('Error requesting permissions!' + JSON.stringify(e));
}
}
...
}
async testSQLitePlugin(): Promise<void> {
if(!this._isPermission) {
console.log("Android permissions not granted");
return;
}
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))
// Insert a new User with SQL and Values
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);
// 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);
// 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;
...
} else {
console.log("Error: Open database failed");
return;
}
}
...
}
The IOS and Android codes are using SQLCipher allowing for database encryption The Electron code use sqlite3
Thanks goes to these wonderful people (emoji key):
Jean Pierre QuΓ©au π» | Paul Antoine π» | Karyfars π» |
This project follows the all-contributors specification. Contributions of any kind welcome!
2.4.4 (2020-12-26)
FAQs
Community plugin for native & electron SQLite databases
The npm package @capacitor-community/sqlite receives a total of 11,205 weekly downloads. As such, @capacitor-community/sqlite popularity was classified as popular.
We found that @capacitor-community/sqlite demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.Β It has 43 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.
Security News
A new Rust RFC proposes "Trusted Publishing" for Crates.io, introducing short-lived access tokens via OIDC to improve security and reduce risks associated with long-lived API tokens.