Cordova Plugin: Database Copy

2.1.2 · maintenance · verified Wed Apr 22

cordova-plugin-dbcopy is a Cordova/Phonegap plugin designed to copy a pre-populated SQLite database from the `www` directory of an application to the device's native app database directory. The current stable version is 2.1.2. This plugin addresses a common need for mobile applications that require initial data or a pre-configured database upon first launch, providing methods to copy the database, check its existence, and handle different storage locations, especially for iOS iCloud backup exclusion. Its release cadence appears to be stable rather than rapid, focusing on core functionality within the Cordova ecosystem. A key differentiator is its explicit handling of platform-specific database locations and error responses for common issues like file not found or already existing databases.

Common errors

Warnings

Install

Imports

Quickstart

This code snippet demonstrates how to copy a pre-populated database from the `www` directory to the app's data directory after the Cordova `deviceready` event. It includes basic error handling.

document.addEventListener('deviceready', function() {
  const dbName = 'myprepopulated.db';
  // For iOS, location 0 disables iCloud backup. For Android, use 0.
  const location = 0; 

  window.plugins.sqlDB.copy(dbName, location, function(response) {
    console.log('Database copy success:', response.message, 'Code:', response.code);
    // Now you can open the database using SQLitePlugin
    // const db = window.sqlitePlugin.openDatabase({
    //   name: dbName, 
    //   location: 'default' // Or other appropriate location based on 'location' param above
    // });
    // db.transaction(function(tx) { /* ... */ });
  }, function(error) {
    console.error('Database copy error:', error.message, 'Code:', error.code);
  });
}, false);

view raw JSON →