Hello Ionic Developers. Im currently working with Ionic 2 using Sqlite from Ionic native. I'm having Issue with this. I need to Insert into table I have write query and codes but its not working. I have also Inject SqliteObject in ap.module.ts But when I tried to make apk It gave me run time error with.
Can't resolve all parameters for SQLiteObject.
And also I need to understand What is the Difference between executeSql and addStatement?
Hi. I'm going through the same problem. Can someone help us?
You're not supposed to inject SQLiteObject in your module. Only SQLite.
For more information about the methods and what the difference is between them, please refer to the original plugin repo: https://github.com/litehelpers/Cordova-sqlite-storage
I have Implement SQLite But it still having issue..
@Ghayyas can you share your code here, and what is the issue that you are having
@rivailruiz I have made Simple Demo SQLite core In ionic 2 with CRUD operation. You can see it here..
https://github.com/TeamClouders/Ionic-2-sqlite-demo
@ihadeed I have tried using Ionic native. But it always gave me new errors. So I have done it with SQLite Core. NOT IONIC NATIVE...
@Ghayyas thank you for it. It Works it for me 馃憤
Hi guys
Please check in this provider I am calling insert function and in console I am able to check inserted message but not after in message i.e.after execute insert function...
export class UserProvider {
private db: SQLite;
private db1: SQLiteObject;
public constructor() {
this.db=new SQLite();
this.db.create({
name: 'data.db',
location: 'default'
}).then((db1: SQLiteObject) => {
db1.executeSql('create table if not exists user (name VARCHAR(32),email VARCHAR(32),number VARCHAR(32),password VARCHAR(32))', {})
.then((data) => {
console.log("created")
}).catch(e => console.log(JSON.stringify(e)));
})
}
public insert(name:string, email:string, number:string, password:string) {
console.log("inserted");
this.db1.executeSql('insert into user(name ,email,number,password) values ("' + name + '","' + email + '","' + number + '","' +password + '")', {});
console.log("in");
}
app.module.ts
import { SQLite, SQLiteObject } from '@ionic-native/sqlite';
.
.
providers: [
SQLite,
]
this worked for me
@nikhildhar123
Before executing create table query ....write this :
this.db1 = db1
I have also used
import { SQLite, SQLiteObject } from '@ionic-native/sqlite';
and
getData(){
this.sqlite.create({
name: 'data.db',
location: 'default'
})
.then((db: SQLiteObject) => {
db.executeSql('CREATE TABLE IF NOT EXISTS emp(id INTEGER PRIMARY KEY, name TEXT, lname TEXT, age INT, gender TEXT, salary INT, date TEXT)', {})
.then(() => console.log('Executed SQL'))
.catch(e => console.log(e));
db.executeSql('SELECT * FROM emp ORDER BY id DESC',{})
.then(res=>{
console.log('Executed SQL SELECT done ');
this.data = [];
for(var index = 0; index < res.rows.length; index++){
this.data.push({
id :res.row.item(index).id,
name :res.row.item(index).name,
lname :res.row.item(index).lname,
age :res.row.item(index).age,
gender :res.row.item(index).gender,
salary :res.row.item(index).salary,
date :res.row.item(index).date,
})
}
}).catch(e => console.log(e));
}).catch(e => console.log(e));
}
but it is showing error
Error: exec proxy not found for :: SQLitePlugin :: close
Error: exec proxy not found for :: SQLitePlugin :: open
OPEN database: data.db FAILED, aborting any pending transactions
Error: Could not open database
聽 | sqlError = new Error(error);
import { SQLite, SQLiteObject } from '@ionic-native/sqlite';
import { Injectable } from '@angular/core';
@Injectable()
export class DataBase {
public db = null
constructor(
public sqlite: SQLite
) {
this.createDataBase()
.then(() => {
// call your function here containing queries
this.db.transaction((tx) => {
tx.executeSql('CREATE TABLE IF NOT EXISTS emp(id INTEGER PRIMARY KEY, name TEXT, lname TEXT,
age INT, gender TEXT, salary INT, date TEXT)', {})
.then(() => console.log('Executed SQL'))
.catch(e => console.log(e));
})
});
}
createDataBase() {
return new Promise((resolve)=>{
this.sqlite.create({
name: 'isfaSales.db',
location: 'default'
})
.then((Db: SQLiteObject) => {
this.db = Db
resolve()
})
})
}
}
I have used but it is showing error
import { NgModule, ErrorHandler } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { MyApp } from './app.component';
import { AboutPage } from '../pages/about/about';
import { ContactPage } from '../pages/contact/contact';
import { HomePage } from '../pages/home/home';
import { TabsPage } from '../pages/tabs/tabs';
import { AddemployeePage } from '../pages/addemployee/addemployee';
import { EditemployeePage } from '../pages/editemployee/editemployee';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { SQLite } from '@ionic-native/sqlite';
import { LocalNotifications } from '@ionic-native/local-notifications';
import { Geolocation } from '@ionic-native/geolocation';
@NgModule({
declarations: [
MyApp,
AboutPage,
ContactPage,
HomePage,
TabsPage,
AddemployeePage,
EditemployeePage
],
imports: [
BrowserModule,
IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
AboutPage,
ContactPage,
HomePage,
TabsPage,
AddemployeePage,
EditemployeePage
],
providers: [
SQLite,
LocalNotifications,
Geolocation,
StatusBar,
SplashScreen,
{provide: ErrorHandler, useClass: IonicErrorHandler}
]
})
export class AppModule {
}
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { SQLite, SQLiteObject } from '@ionic-native/sqlite';
import { LocalNotifications } from '@ionic-native/local-notifications';
import { AddemployeePage } from '../addemployee/addemployee';
import { EditemployeePage } from '../editemployee/editemployee';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
data:any = [];
constructor(public navCtrl: NavController, public sqlite: SQLite, public localNotifications: LocalNotifications) {
}
ionViewDidLoad(){
this.getData();
}
doRefresh(refresher) {
console.log('Begin async operation', refresher);
this.getData();
setTimeout(() => {
console.log('Async operation has ended');
refresher.complete();
}, 2000);
}
refresh(){
this.getData();
}
getData(){
this.sqlite.create({
name: 'records.db',
location: 'default',
createFromLocation: 1
})
.then((db: SQLiteObject) => {
db.executeSql('CREATE TABLE IF NOT EXISTS emp(id INTEGER PRIMARY KEY, name TEXT, lname TEXT, age INT, gender TEXT, salary INT, date TEXT)', {})
.then(() => console.log('Executed SQL'))
.catch(e => console.log(e));
db.executeSql('SELECT * FROM emp ORDER BY id DESC',{})
.then(res=>{
console.log('Executed SQL SELECT done ');
this.data = [];
for(var index = 0; index < res.rows.length; index++){
this.data.push({
id :res.row.item(index).id,
name :res.row.item(index).name,
lname :res.row.item(index).lname,
age :res.row.item(index).age,
gender :res.row.item(index).gender,
salary :res.row.item(index).salary,
date :res.row.item(index).date,
})
}
}).catch(e => console.log(e));
}).catch(e => console.log(e));
}
addEmployee(){
this.navCtrl.push(AddemployeePage);
}
editEmployee(id, name, lname, age, gender, salary, date){
this.navCtrl.push(EditemployeePage,{
id:id,
name:name,
lname:lname,
age:age,
gender:gender,
salary:salary,
date:date
});
}
deleteEmployee(id){
this.sqlite.create({
name: 'data.db',
location: 'default'
})
.then((db: SQLiteObject) => {
db.executeSql('DELETE FROM emp WHERE id=?', [id])
.then(() => {console.log('Executed SQL delete')
this.getData();
}).catch(e => console.log(e));
}).catch(e => console.log(e));
}
notify(){
// Schedule a single notification
this.localNotifications.schedule({
id: 1,
title: 'the is title',
text: 'Single ILocalNotification',
sound: null
});
}
}
replace {} in db1.executeSql('create table if not exists user (name VARCHAR(32),email VARCHAR(32),number VARCHAR(32),password VARCHAR(32))', {}) with [] so it becomes db1.executeSql('create table if not exists user (name VARCHAR(32),email VARCHAR(32),number VARCHAR(32),password VARCHAR(32))', [])
Most helpful comment
app.module.ts
this worked for me