-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to open SQLite database in Android #881
Comments
From @jayeshanandani on December 14, 2016 16:39 I am using SQLite and I haven't faced this issue. It works perfectly fine. Can you please give some more information like android version or device on which this is being tested upon to replicate or share some piece of code which can help? |
@fjms are you waiting for |
I tried same (executing a query on a db placed under 'www\test.db.sqlite' folder of ionic project) on MAC and i get this error both on browser and on iOS simulator: ReferenceError: sqlitePlugin is not definedCode: constructor(public navCtrl: NavController, public platform: Platform,
public pps: ProdPerfService){
platform.ready().then((readySource) => {
pps.getSummary(); //pps is a provider named ProdPerfService
});
}
//ProdPerfService:
import { Injectable } from '@angular/core';
import { SQLite } from 'ionic-native';
@Injectable()
export class ProdPerfService {
constructor(){
}
getSummary(){
let db = new SQLite();
db.openDatabase({
name: 'test.db.sqlite',
location: 'default' // the location field is required
}).then(() => {
db.executeSql('select * from summary', {}).then(() => {
alert('result');
}, (err) => {
console.error('Unable to execute sql: ', err);
alert('err');
})
}, (err) => {
console.error('Unable to open database: ', err);
alert(err);
});
}
} Cordova CLI: 6.4.0 |
@megharajdeepak are you using livereload? what is the error message that you're getting? |
Yes, it's a live reload. This is the stack trace i get: prodperf-service.ts:28 Unable to open database: ReferenceError: sqlitePlugin is not defined
at eval (/Users/Deepak/Documents/ionicAngular2_Workspace/ChartApp/node_modules/ionic-native/dist/esm/plugins/sqlite.js:76:13)
at new t (http://localhost:8100/build/polyfills.js:3:15636)
at SQLite.openDatabase (/Users/Deepak/Documents/ionicAngular2_Workspace/ChartApp/node_modules/ionic-native/dist/esm/plugins/sqlite.js:75:16)
at ProdPerfService.getSummary (/Users/Deepak/Documents/ionicAngular2_Workspace/ChartApp/src/pages/prodperf/prodperf-service.ts:22:12)
at eval (/Users/Deepak/Documents/ionicAngular2_Workspace/ChartApp/src/pages/prodperf/prodperf.ts:33:17)
at t.invoke (http://localhost:8100/build/polyfills.js:3:13422)
at Object.onInvoke (/Users/Deepak/Documents/ionicAngular2_Workspace/ChartApp/node_modules/@angular/core/src/zone/ng_zone.js:238:37)
at t.invoke (http://localhost:8100/build/polyfills.js:3:13373)
at e.run (http://localhost:8100/build/polyfills.js:3:10809)
at http://localhost:8100/build/polyfills.js:3:8911(anonymous function) @ prodperf-service.ts:28t.invoke @ polyfills.js:3onInvoke @ ng_zone.js:238t.invoke @ polyfills.js:3e.run @ polyfills.js:3(anonymous function) @ polyfills.js:3t.invokeTask @ polyfills.js:3onInvokeTask @ ng_zone.js:229t.invokeTask @ polyfills.js:3e.runTask @ polyfills.js:3i @ polyfills.js:3invoke @ polyfills.js:3 |
Please follow the update instructions here https://github.com/driftyco/ionic-app-scripts/releases/tag/v0.0.47 and then try again. This is most likely an issue with |
Ok. Ill try that and let you know. But it also fails on iOS Simulator. Anyway, let me give it a try. Hope it works. thanks |
@ihadeed Yes, I use it into platform.ready().then(...) |
@fjms are you using livereload? if so, read my previous comment. |
@ihadeed I tried the following: npm install -g ionic@latest //Deleted main.dev.ts and main.prod.ts and created a main.ts file with the following content:
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';
platformBrowserDynamic().bootstrapModule(AppModule);
//made sure package.json had following scripts:
"scripts": {
"ionic:build": "ionic-app-scripts build",
"ionic:serve": "ionic-app-scripts serve"
} installed latest app scripts using: npm install @ionic/app-scripts@latest --save-dev and finally ran ionic serve. I still get the same error message like before: prodperf-service.ts:19 Unable to open database: ReferenceError: sqlitePlugin is not defined
at http://localhost:8100/build/main.js:73360:13
at new t (http://localhost:8100/build/polyfills.js:3:15636)
at SQLite.openDatabase (http://localhost:8100/build/main.js:73359:16)
at ProdPerfService.getSummary (http://localhost:8100/build/main.js:97186:12)
at http://localhost:8100/build/main.js:51462:17
at t.invoke (http://localhost:8100/build/polyfills.js:3:13422)
at Object.onInvoke (http://localhost:8100/build/main.js:33469:37)
at t.invoke (http://localhost:8100/build/polyfills.js:3:13373)
at e.run (http://localhost:8100/build/polyfills.js:3:10809)
at http://localhost:8100/build/polyfills.js:3:8911 |
When we say location of db file as 'default', where does ionic look for the db file? it is www folder right? |
Anybody from the team got any update on this? |
@megharajdeepak : This is certainly device ready issue error or plugin not being installed while running in the simulator as it works perfectly fine for me. |
@jayeshanandani plugin is installed and sqlite db is opened on device ready. Does it work on browser too for you? Can you kindly share ionic2 package json? May be libraries' versions are different in my case. |
@megharajdeepak : This will work only on simulators and devices. On browser it will fall back to WEBSQL. I use following custom provider to work with and it works well everywhere: https://gist.github.com/jayeshanandani/c3e82d6545ac0659a4e1d93814b525a6 |
Got something working: const win: any = window;
constructor(private platform: Platform) {
if (this.platform.is('cordova')) {
this._db = new SQLite();
this._db.openDatabase({
name: DB_NAME,
location: 'default' // the location field is required
}).then(() => {
this._db.executeSql('create table danceMoves(name VARCHAR(32))', {}).then(() => {
alert('query ran');
}, (err) => {
alert('Unable to execute sql: '+ err);
console.error('Unable to execute sql: ', err);
});
})
} else {
console.warn('Storage: SQLite plugin not installed, falling back to WebSQL. Make sure to install cordova-sqlite-storage in production!');
this._db = win.openDatabase(DB_NAME, '1.0', 'database', 5 * 1024 * 1024);
}
} When i run that in simulator, i get a alert saying 'query ran' which means, danceMoves table is created. I have given DB name as 'test'. But where do I find this DB (physical path)? location is 'default', shouldn't db be in 'www' folder? I couldn't find it there. Anybody knows where does ionic create this DB? Can't I give a specific location like src/app/DBs/mydb.sqlite ? |
Ionic doesn't create the database for you, this plugin does: https://github.com/litehelpers/Cordova-sqlite-storage |
I have the same Problem with Android. EDIT: found a solution that works for me. See below. My Code: private database: SQLite;
public constructor(private platform: Platform){
this.platform.ready().then(() => {
if (this.platform.is('cordova')) {
console.log(this.platform.platforms());
}
else {
console.log(this.platform.platforms());
}
this.database = new SQLite();
this.database.openDatabase({name: "data.db", location: "default"}).then(() => {
/*Do Something*/
}, (error) => {
console.log("ERROR: Unable to open databse 'data.db' (" + error + ").");
});
});
} "ionic run android" works perfectly, the database gets created or opend. When i use "ionic run android -l -c" i get
Using "ionic run android", this.platforms.platforms() returns "cordova", "mobile", "android". Has anybody an idea, why this happens? ionic info:
Edit:
i changed it to this configuration, which i got from another of my projects:
and now everything works fine, this.platform.platforms() returns "cordova, mobile, android". |
This is what I did and got it working (which solved our purpose):
Basic code: // Cordova
declare var cordova: any;
//using 'Transfer' plugin to download:
download() {
const fileTransfer = new Transfer();
let url = 'https://yourserver.com/yourdatafiles/sqlitedbfile.zip';
//cordova.file.applicationStorageDirectory is used from 'File' plugin
fileTransfer.download(url, cordova.file.applicationStorageDirectory+'/library/localdatabase/' + 'myfile.zip').then((entry) => {
console.log('download complete: ' + entry.toURL());
alert(JSON.stringify(entry.toURL())); //this will give you the path where file is stored
}, (error) => {
// handle error
this.state = 'error';
alert(JSON.stringify(error));
});
}
//code to unzip
unzip() {
let source: string = cordova.file.applicationStorageDirectory+'/library/localdatabase/myfile.zip';
let dest: string = cordova.file.applicationStorageDirectory+'/library/localdatabase/';
Zip.unzip(source, dest, (progress) => {this.perc = Math.round((progress.loaded / progress.total) * 100);})
.then((result) => {
if(result === 0) alert('SUCCESS'); // name of db file inside this zip file is 'appdata.db'
if(result === -1) alert('FAILED');
});
}
//code to read sqlite file using query which uses 'SQLite' plugin
public openDB(): void{
let dbObj: {};
if(this.platform.is('ios')){
dbObj = {name: "appdata.db", iosDatabaseLocation: 'default'};
}else if(this.platform.is('android')){
dbObj = {name: "appdata.db", location: 'default'};
}
this.db.openDatabase(dbObj).then(() => {
this.refresh();
// alert('db open');
}, (error) => {
alert("Unable to open database"+ JSON.stringify(error));
});
}
//run a query once DB is open
public refresh(): void{
this.db.executeSql("select * from companies", []).then((data) => {
let companies = [];
//alert(JSON.stringify(data.rows));
if(data.rows.length > 0) {
for(var i = 0; i < data.rows.length; i++) {
companies.push({companyName: data.rows.item(i).companyName, companySales: data.rows.item(i).companySales});
}
alert(JSON.stringify(companies));
}
}, (err) => {
//console.log("ERROR: " + JSON.stringify(error));
alert('Unable to execute sql: '+ JSON.stringify(err));
});
} Hope that helps. Cheers! |
Deepak, it was under my impressions that at least on android you cannot open a database unless it is in the www folder. I have tried your solution but when I select one of my tables is empty. (i am using the sqlite-ext plugin) Should I be able to open a database that is not within the www folder? Looks like you are saving in in library/localdatabase on android and when I try to open my file it opens but that is because I assume if you open and the database doesnt exist in www it creates a new one. plzsendhelp :D |
@chrisworrell I guess, I wasn't clear enough. I actually did not use sqlite-ext plugin, I have used sqlite-storage. These are the plugins that I used which are mentioned in ionic 2 doc (https://ionicframework.com/docs/v2/native/sqlite/): 'File' = ionic plugin add cordova-plugin-file I think your query isn't working as sqlite storage and sqlite-ext are different plugins. Try sqlite storage and see how it goes. Cheers! |
@megharajdeepak you are unzipping the SQLite database file to |
@cocowalla While opening the db, following code is used. If it's ios, 'iosDatabaseLocation' would be used as default, while for android, 'location' would be used as default. In both the cases, DB would point to the same location where it was downloaded and unzipped. `
` |
@megharajdeepak ah, I see, so I'm working in an emulator right now - do you know if this should work in the Android emulator? When I try it, it always seems to create a new, empty database, rather than using the one that was downloaded. |
@megharajdeepak I figured it out, the path does differ for Android, so you need something like:
Thanks for the sample you gave, it was really helpful for downloading and using pre-populated SQLite databses 👍 |
@cocowalla are you using cordova-sqlite-ext instead of cordova-sqlite-storage to open pre-populated databases? |
@iursevla |
@cocowalla but with cordova-sqlite-storage you can use pre-populated databases? |
Yes, The |
@cocowalla Thank you. Even ionic 2 official docs still talk about openDatabase method. |
UPDATE: I will document how this can work as discussed in storesafe/cordova-sqlite-storage-help#26. Further response will be below. I just raised storesafe/cordova-sqlite-storage-help#26 to document this further. P.S. In terms of the following change:
I would find this confusing as well. At least the documentation at https://ionicframework.com/docs/native/sqlite/ is now consistent. |
@brodybits I find that statement confusing! :) I'm using this method successfully, and it sounds like @megharajdeepak is too. Here's what I'm doing:
And it works great, exactly as expected. A couple of points:
|
Agreed. (Further response to this and a couple other points was in storesafe/cordova-sqlite-storage-help#26 (comment) for anyone interested.) As discussed in storesafe/cordova-sqlite-storage-help#26 I will update the sqlite plugin documentation to reflect this capability.
Nice idea but I would not favor it, reasoning in storesafe/cordova-sqlite-storage#212 (comment) for anyone interested. |
@megharajdeepak I could see applicationStorageDirectory in iOS is readonly mode and am getting
I am working on pre-populated database and for android its working perfect as the location is read-write. |
@arunpapena |
this.platform.is('ios') Yes, I tried this |
I figured out the solution. And storage path should be
Opening pre populated database:
|
@arunpapena Feel free to open a PR for this workaround. |
From @fjms on December 14, 2016 13:8
Ionic version: (check one with "x")
[ ] 1.x
[x ] 2.x
I'm submitting a ... (check one with "x")
[x ] bug report
[ ] feature request
[ ] support request => Please do not submit support requests here, use one of these channels: https://forum.ionicframework.com/ or http://ionicworldwide.herokuapp.com/
Current behavior:
Unable to open database in Android
Expected behavior:
Open a SQLite database
Steps to reproduce:
Create a new ionic 2 blank project
Add platform android
Install cordova-sqlite-storage
See this https://ionicframework.com/docs/v2/native/sqlite/
ionic run android
Related code:
Other information:
Ionic info: (run
ionic info
from a terminal/cmd prompt and paste output below):Copied from original issue: ionic-team/ionic-framework#9635
The text was updated successfully, but these errors were encountered: