5.7.3-3 • Published 4 days ago

@capacitor-community/sqlite v5.7.3-3

Weekly downloads
781
License
MIT
Repository
github
Last release
4 days ago

Maintainers

MaintainerGitHubSocial
Quéau Jean Pierrejepiqueau

Installation

npm install @capacitor-community/sqlite
npx cap sync
  • On iOS, no further steps are needed.

  • On Android, register the plugin in your main activity:

    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);
              }
            }
          );
      }
    }
  • On Electron, go to the Electron folder of YOUR_APPLICATION

    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

    Go back in the main folder of your application Add a script in the index.html file of your application in the body tag

    • case databases under YourApplication/Electron/
    <body>
      <app-root></app-root>
      <script>
        try {
          if (
            process &&
            typeof process.versions.electron === 'string' &&
            process.versions.hasOwnProperty('electron')
          ) {
            const sqlite3 = require('sqlite3');
            const fs = require('fs');
            const path = require('path');
            window.sqlite3 = sqlite3;
            window.fs = fs;
            window.path = path;
          }
        } catch {
          console.log("process doesn't exists");
        }
      </script>
    </body>
    • case databases under User/Databases/APP_NAME/
    <body>
      <app-root></app-root>
      <script>
        try {
          if (
            process &&
            typeof process.versions.electron === 'string' &&
            process.versions.hasOwnProperty('electron')
          ) {
            const sqlite3 = require('sqlite3');
            const fs = require('fs');
            const path = require('path');
            const homeDir = require('os').homedir();
            window.sqlite3 = sqlite3;
            window.fs = fs;
            window.path = path;
            window.appName = 'YOUR_APP_NAME';
            window.homeDir = homeDir;
          }
        } catch {
          console.log("process doesn't exists");
        }
      </script>
    </body>

    Then build YOUR_APPLICATION

    npm run build
    npx cap copy
    npx cap copy web
    npx cap open android
    npx cap open ios
    npx cap open electron

Configuration

No configuration required for this plugin

Supported methods

NameAndroidiOSElectronWeb
open (non-encrypted DB)
open (encrypted DB)
close
execute
executeSet
run
query
deleteDatabase
importFromJson
exportToJson
createSyncTable
setSyncDate
isJsonValid
isDBExists

Documentation

API_Documentation

ImportExportJson_Documentation

Applications demonstrating the use of the plugin

Ionic/Angular

Ionic/React

Usage

 import { Plugins } from '@capacitor/core';
 import * as CapacitorSQLPlugin from '@capacitor-community/sqlite';
 const { CapacitorSQLite,Device } = Plugins;

 @Component( ... )
 export class MyPage {
   _sqlite: any;

   ...

   async ngAfterViewInit()() {
     const info = await Device.getInfo();
     if (info.platform === "ios" || info.platform === "android") {
       this._sqlite = CapacitorSQLite;
     } else if(info.platform === "electron") {
       this._sqlite = CapacitorSQLPlugin.CapacitorSQLiteElectron;
     } else {
       this._sqlite = CapacitorSQLPlugin.CapacitorSQLite;
     }

   }

   async testSQLitePlugin() {
       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) resolve(false);


       ...
       }
   }
   ...
 }

Dependencies

The IOS and Android codes are using SQLCipher allowing for database encryption The Electron code use sqlite3

6.0.0-alpha.1

4 days ago

5.7.3-3

11 days ago

5.7.3-2

17 days ago

5.7.3-1

17 days ago

5.7.2

1 month ago

5.7.1

1 month ago

5.6.4

1 month ago

5.7.0

1 month ago

5.6.3

2 months ago

5.6.2

2 months ago

5.6.1

2 months ago

5.6.1-4

2 months ago

5.6.1-2

2 months ago

5.6.1-3

2 months ago

5.6.1-1

3 months ago

5.6.0

3 months ago

5.5.2

4 months ago

5.5.1

4 months ago

5.5.1-4

4 months ago

5.5.1-3

4 months ago

5.5.1-2

5 months ago

5.5.1-1

5 months ago

5.5.0

5 months ago

5.0.8

8 months ago

5.0.7

9 months ago

5.0.6

10 months ago

5.0.5

10 months ago

5.0.4

11 months ago

5.2.5

7 months ago

5.2.4

7 months ago

5.2.3

8 months ago

5.4.2-2

6 months ago

5.4.1

7 months ago

5.4.0

7 months ago

5.4.2-1

7 months ago

5.0.7-1

9 months ago

5.0.7-2

9 months ago

5.0.5-1

10 months ago

5.0.5-2

10 months ago

5.0.3

11 months ago

5.0.2

11 months ago

5.0.3-1

11 months ago

5.0.4-alphabetter.4

11 months ago

5.0.4-alphabetter.3

11 months ago

5.0.4-alphabetter.2

11 months ago

5.0.4-alphabetter.1

11 months ago

5.0.3-2.test1

11 months ago

5.0.3-2.test2

11 months ago

5.0.4-betabetter.0

11 months ago

5.0.0-beta.2

1 year ago

5.0.1

11 months ago

5.0.0

12 months ago

4.8.0

12 months ago

4.8.0-0

1 year ago

4.8.0-1

12 months ago

5.0.0-beta.1

1 year ago

5.0.0-beta.0

1 year ago

4.6.3-4

1 year ago

4.6.3-2

1 year ago

4.6.3-3

1 year ago

4.6.3

1 year ago

4.6.3-1

1 year ago

4.6.2

1 year ago

4.6.2-3

1 year ago

4.6.2-1

1 year ago

4.6.2-2

1 year ago

4.6.1-2

1 year ago

4.6.1-1

1 year ago

4.6.1

1 year ago

4.2.2

2 years ago

4.6.0

1 year ago

4.5.0

1 year ago

4.2.2-1

2 years ago

4.2.1

2 years ago

4.2.0

2 years ago

4.0.1

2 years ago

4.1.0-8

2 years ago

4.1.0-4

2 years ago

4.1.0-5

2 years ago

4.1.0-6

2 years ago

4.1.0-7

2 years ago

4.1.0-1

2 years ago

4.1.0-2

2 years ago

4.1.0-3

2 years ago

4.1.0

2 years ago

4.1.1

2 years ago

3.5.1-2

2 years ago

3.5.2

2 years ago

3.5.1

2 years ago

3.5.2-dev1

2 years ago

3.5.2-dev2

2 years ago

4.0.0-0

2 years ago

4.0.0-1

2 years ago

3.7.0

2 years ago

3.5.1-1

2 years ago

3.4.3-2

2 years ago

3.4.3-3

2 years ago

3.5.0

2 years ago

3.4.3

2 years ago

3.4.3-1

2 years ago

3.4.2-1

2 years ago

3.4.2-2

2 years ago

3.4.2-3

2 years ago

3.4.2-4

2 years ago

3.4.2-5

2 years ago

3.4.2

2 years ago

3.4.1

2 years ago

3.4.1-2

2 years ago

3.4.1-3

2 years ago

3.4.1-4

2 years ago

3.4.0-3

2 years ago

3.4.0

2 years ago

3.4.1-1

2 years ago

3.4.0-2

2 years ago

3.4.0-1

2 years ago

3.3.3-4

2 years ago

3.3.3-5

2 years ago

3.3.3-2

2 years ago

3.3.3-3

2 years ago

3.3.3-1

2 years ago

3.3.3

2 years ago

3.3.2

2 years ago

3.3.3-test211

2 years ago

3.2.5

3 years ago

3.3.1

2 years ago

3.2.5-2

3 years ago

3.2.4

3 years ago

3.2.3

3 years ago

3.2.4-1

3 years ago

3.2.4-2

3 years ago

3.2.5-1

3 years ago

3.2.3-1

3 years ago

3.2.2

3 years ago

3.2.2-3

3 years ago

3.2.2-2

3 years ago

3.2.2-1

3 years ago

3.2.0

3 years ago

3.2.0-11

3 years ago

3.2.0-10

3 years ago

3.2.0-9

3 years ago

3.2.0-3

3 years ago

3.2.0-8

3 years ago

3.2.0-5

3 years ago

3.2.0-4

3 years ago

3.2.0-7

3 years ago

3.2.0-6

3 years ago

3.2.0-2

3 years ago

3.2.0-1

3 years ago

3.1.3-web.3

3 years ago

3.1.3-web.2

3 years ago

3.1.3-web.1

3 years ago

3.1.3-3

3 years ago

3.1.3-2

3 years ago

3.1.3-1

3 years ago

3.1.2

3 years ago

3.1.2-1

3 years ago

3.1.1

3 years ago

3.0.0

3 years ago

3.0.0-beta.14

3 years ago

3.0.0-rc.2

3 years ago

3.0.0-rc.1

3 years ago

3.0.0-beta.11

3 years ago

3.0.0-beta.12

3 years ago

3.0.0-beta.13

3 years ago

3.0.0-beta.10

3 years ago

2.9.16

3 years ago

3.0.0-beta.9

3 years ago

3.0.0-beta.8

3 years ago

2.9.15

3 years ago

2.9.14

3 years ago

3.0.0-beta.5

3 years ago

3.0.0-beta.7

3 years ago

3.0.0-beta.6

3 years ago

2.9.13

3 years ago

2.9.12

3 years ago

2.9.11

3 years ago

2.9.10

3 years ago

2.9.9

3 years ago

2.9.8

3 years ago

3.0.0-beta.4

3 years ago

3.0.0-beta.3

3 years ago

2.9.7

3 years ago

2.9.6

3 years ago

2.4.6

3 years ago

3.0.0-beta.1

3 years ago

3.0.0-beta.2

3 years ago

2.9.5

3 years ago

2.9.4

3 years ago

2.9.3

3 years ago

2.9.2

3 years ago

2.9.1

3 years ago

2.9.0

3 years ago

2.4.5

3 years ago

2.4.5-3

3 years ago

2.4.5-2

3 years ago

2.9.0-beta.3

3 years ago

2.4.5-1

3 years ago

2.9.0-beta.2

3 years ago

2.4.4

3 years ago

2.9.0-beta.1

3 years ago

2.4.3

3 years ago

2.9.0-alpha.7

3 years ago

2.9.0-alpha.6

3 years ago

2.9.0-alpha.5

3 years ago

2.9.0-alpha.4

3 years ago

2.9.0-alpha.3

3 years ago

2.9.0-alpha.2

3 years ago

2.4.3-1

3 years ago

2.9.0-alpha.1

3 years ago

2.4.2

3 years ago

2.4.2-9

3 years ago

2.4.2-8

4 years ago

2.4.2-7

4 years ago

2.4.2-6

4 years ago

2.4.2-5

4 years ago

2.4.2-4

4 years ago

2.4.2-3

4 years ago

2.4.2-2

4 years ago

2.4.2-1

4 years ago

2.4.1-1

4 years ago

2.4.0

4 years ago

2.4.0-beta.2

4 years ago

2.4.0-beta.1

4 years ago

2.3.0

4 years ago

2.3.0-beta3

4 years ago

2.3.0-beta.2

4 years ago

2.3.0-beta.1

4 years ago

2.3.0-2

4 years ago

2.3.0-1

4 years ago