Manage your SQLite database easily
- initDB : initi database.
- openDB : open database.
- createTables : create tables.
- patch : update tables (ALTER ...).
NB : The symbol '?' means : Optional param
@Params
- tableName: string,
- columnSelected?: string,
- column?: string,
- operator?: string,
- value?: any,
- orderBy?: string
@Return : Promise
this.select({
tableName: 'person',
columnSelected: 'name, birthday',
column: 'name',
operator: 'like',
value: 'a%',
orderBy: 'birthday ASC'
});
Equivalent query in SQL
SELECT name, birthday FROM person WHERE name LIKE 'a%' ORDER BY birthday ASC;
@Params
- tableName: string,
- customColumns?: string, default customColumns = '*'
- custom?: string,
- orderBy?: string
@Return : Promise
this.selectCustom({
tableName: 'product',
customColumns: '*',
custom: 'WHERE price > 100',
orderBy: 'price DESC, name ASC'
});
Equivalent query in SQL
SELECT * FROM product WHERE WHERE price > 100 ORDER BY price DESC, name ASC;
@Params
- tableName: string,
- obj: any,
- ignore?: string[],
@Return : Promise
var myPerson = new Person();
this.insert({tableName: 'person', obj: myPerson});
Equivalent query in SQL
INSERT INTO person
(firstName, lastName, birthday, birthplace, address, weight, married, nbrChild, job, hasCar)
VALUES ('Mohamed', 'Ali', '10/03/1959', 'Algeria', 'Algiers', '78', 'Y', '4', 'Officer', 'Y');
It's very clear the big difference between DbManager and SQL native
@Params
- tableName: string,
- obj: any,
- colsRef: string[],
- colsVal: any[],
- ignore?: string[],
@Return : Promise
var myProduct = new Product();
this.insert({
tableName: 'product',
obj: myProduct,
colsRef: ['name', 'constructor'],
colsVal: ['myProdName', 'myConstructor']
});
Insert my object only if there no one with name = 'myProdName' and constructor = 'myConstructor'
@Params
- tableName: string,
- obj: any,
- column?: string,
- operator?: string,
- value?: any,
- ignore?: string[],
@Return : Promise
var myPerson = new Person();
this.update({
tableName: 'person',
obj: myPerson,
column: 'firstName',
value: 'Mohamed',
ignore: ['firstName', 'lastName', 'birthday', 'birthplace']
});
Equivalent query in SQL
UPDATE person
SET address = 'Algiers', weight = 82, married = 'Y', nbrChild = '5', job = 'Director', hasCar = 'N'
WHERE firstName = 'Mohamed'
The attributes : 'firstName', 'lastName', 'birthday', 'birthplace' are ignored because we have set them in ignore array
Update only specified attributes. @Params
- tableName: string,
- columns: string[],
- values: any[],
- column?: string,
- operator?: string,
- value?: any,
@Return : Promise
var myPerson = new Person();
this.updateOnly({
tableName: 'person',
columns: ['address', 'weight', 'hasCar'],
values: [myPerson.address, myPerson.weight, myPerson.hasCar],
column: 'firstName',
operator: '=',
value: 'Mohamed'
});
Equivalent query in SQL
UPDATE person
SET address = 'Algiers', weight = 82, hasCar = 'Y'
WHERE firstName = 'Mohamed'
Customized update query. @Params
- tableName: string,
- obj: any,
- customWhere: string,
- ignore?: string[],
@Return : Promise
var myPerson = new Person();
var custom = "WHERE firstName = 'Mohamed' AND lastName='Ali' AND hasCar = 'Y' ";
this.update({
tableName: 'person',
obj: myPerson,
customWhere: custom,
ignore: ['firstName', 'lastName']
});
Equivalent query in SQL
UPDATE person
SET address = 'Algiers', weight = 82, married = 'Y', nbrChild = '5', job = 'Director', hasCar = 'N'
birthday = '10/03/1959', birthplace = 'Algeria',
WHERE firstName = 'Mohamed' AND lastName='Ali' AND hasCar = 'Y'
@Params
- tableName: string,
@Return : Promise
this.deleteAll({tableName: 'product'});
Delete all my products in product table without exception
@Params
- tableName: string,
- column: string,
- operator?: string, (optional) default : '='
- value: any,
@Return : Promise
this.deleteFiltred({
tableName: 'product',
column: 'price',
operator: '<',
value: 125
});
Equivalent query in SQL
DELETE FROM product WHERE price < 125;
@Params
- tableName: string,
- custom: string,
@Return : Promise
var custom = "WHERE price < 125 AND name like 'a%'";
this.deleteCustom({tableName: 'product', custom: custom});
Equivalent query in SQL
DELETE FROM product WHERE price < 125 AND name like 'a%';
1- Go in your project Ionic 2
/your-project/src/providers/db/
2- Clone the db-manager in your db file
git clone https://github.com/yajuve/db-manager.git
3- Import DbProvider module in /your-project/src/app/app.module.ts add
...
providers: [
...
DbProvider
...
]
})
export class AppModule {}