Обновление
Поскольку вы используете Ionic 1, вы можете заключить функцию в обещание и использовать ее:
function exportDbToSql() {
var deferred = $q.defer();
cordova.plugins.sqlitePorter.exportDbToSql(db, {
successFn: function(sql, count) {
deferred.resolve({sql: sql, count: count})
}
});
return deferred.promise;
}
И когда вы вызываетефункция, это будет:
exportDbToSql().then(function(result) {
console.log(result.sql);
console.log(result.count);
});
Старый ответ
Если вы используете Ionic 2+, то вы можете следовать их документации здесь.
Откройте командную строку и введите
ionic cordova plugin add uk.co.workingedge.cordova.plugin.sqliteporter
npm install @ionic-native/sqlite-porter
, затем вы можете использовать ее следующим образом:
import { SQLitePorter } from '@ionic-native/sqlite-porter/ngx';
constructor(private sqlitePorter: SQLitePorter) { }
...
let db = window.openDatabase('Test', '1.0', 'TestDB', 1 * 1024);
// or we can use SQLite plugin
// we will assume that we injected SQLite into this component as sqlite
this.sqlite.create({
name: 'data.db',
location: 'default'
})
.then((db: any) => {
let dbInstance = db._objectInstance;
// we can pass db._objectInstance as the database option in all SQLitePorter methods
});
let sql = 'CREATE TABLE Artist ([Id] PRIMARY KEY, [Title]);' +
'INSERT INTO Artist(Id,Title) VALUES ("1","Fred");';
this.sqlitePorter.importSqlToDb(db, sql)
.then(() => console.log('Imported'))
.catch(e => console.error(e));
В вашем случае это должно работатькак это:
this.sqlitePorter.exportDbToSql(db)
.then(() => console.log('success'))
.catch(() => console.log('error'))