Hello, I’m novice in IONIC.
I’m trying open existing DB (SQLite) with a table and this table is populated with data.
I copy my DB into *www* folder directory of Windows10
My code:
this.sqlite.create({
name: ‘Keeper.db3’,
location: ‘default’,
DB is opened correctly (i have log) but there’s no data retrievend because rows.lenght returned is not greater than 0.
My Code:
str1: String;
let str1 = “select * from users”;
db.executeSql(str1, ).then(res => {
if (res.rows.lenght > 0)
{
alert(“there are rows”);
}
else
{
alert(“there aren’t rows”);
}
Can you help me?
Check your code for typos…
On the if it should be … res.rows.length
It doesn 't work.
i’ve read that i need sqlite-cordova-ext plugin to acces to pre-populated Dbs.
is it right?
thnks
from the plugin docs on ionic
- Add plugin to your project
ionic cordova plugin add cordova-sqlite-storage
npm install @ionic-native/sqlite
- In the file you want to use the DB plugin maybe a dataservice.ts that provides data to the app file for example
import { SQLite, SQLiteObject } from '@ionic-native/sqlite/ngx';
constructor(private sqlite: SQLite) { }
...
// How to create a db or open
// create(config) Opens or creates a SQLite database file.
this.sqlite.create({
name: 'data.db',
location: 'default'
})
.then((db: SQLiteObject) => {
db.executeSql('create table danceMoves(name VARCHAR(32))', [])
.then(() => console.log('Executed SQL'))
.catch(e => console.log(e));
})
.catch(e => console.log(e));
// Add CRUD functions and other functionalities you may need
addData(){
// Implementation
...
}
readData(){
// Implementation
...
}
updateData(){
// Implementation
...
}
deleteData(){
// Implementation
...
}
...
Read through this tute (though it says ionic3) and you’ll have a general roadmap as to what you can do…
Ionic 3, Angular 4 and SQLite CRUD Offline Mobile App
Post your code from your file…