Ionic3学习笔记(六)存储之使用 SQLite
来源:互联网 发布:netgear 访客网络 编辑:程序博客网 时间:2024/05/29 02:45
本文为原创文章,转载请标明出处
目录
- 安装
- CURD操作
1. 安装
命令行输入
ionic cordova plugin add cordova-sqlite-storagenpm install --save @ionic-native/sqlite
在 ./src/app/app.module.ts
中添加
import {SQLite} from "@ionic-native/sqlite";
在 provides
中添加
SQLite,
2. CURD操作
user.ts
export class User { username: string; password: string; gender: boolean; age: number; intro: string; email: string; phone: string; location: string; constructor() { }}
data.ts
import {SQLite, SQLiteObject} from "@ionic-native/sqlite";export class DataProvider { myAppDatabase: SQLiteObject; constructor(private sqlite: SQLite) { } initDatabase() { this.sqlite.create({ name: 'myApp.db', location: 'default' }).then((database: SQLiteObject) => { database.executeSql('CREATE TABLE IF NOT EXISTS users(email VARCHAR(320) PRIMARY KEY, username VARCHAR(20) NOT NULL, password VARCHAR(30) NOT NULL, gender BOOLEAN, age TINYINT, intro VARCHAR(300), phone CHAR(11), location VARCHAR(100));', {}).then(() => console.log('init database successfully')).catch(e => console.log(e)); this.myAppDatabase = database; }) }}
user-data.ts
import {Injectable} from '@angular/core';import {SQLiteObject} from "@ionic-native/sqlite";import {DataProvider} from "../data/data";import {User} from "./user";@Injectable()export class UserDataProvider { database: SQLiteObject; constructor(private dataProvider: DataProvider) { this.database = this.dataProvider.myAppDatabase; } insertIntoUserTable(user: User) { this.database.executeSql('INSERT INTO users VALUES (?, ?, ?, NULL, NULL, NULL, NULL, NULL);', [user.email, user.username, user.password]).then(() => console.log('insert into users table successfully')).catch(e => console.log(e)); } queryUserTable() { this.database.executeSql('SELECT * FROM users;', {}).then(() => console.log('query users table successfully')).catch(e => console.log(e)); } updateUserTable(user: User) { this.database.executeSql('UPDATE users SET username=?, password=?, gender=?, age=?, intro=?, phone=?, location=? WHERE email=?;', [user.username, user.password, user.gender, user.age, user.intro, user.phone, user.location, user.email]).then(() => console.log('update users table successfully')).catch(e => console.log(e)); }}
更多可详见
1. Ionic Native - SQLite
2. GitHub - Cordova-sqlite-storage
如有不当之处,请予指正,谢谢~
阅读全文
1 0
- Ionic3学习笔记(六)存储之使用 SQLite
- ionic3学习笔记(六)--轮播图
- Ionic3学习笔记(五)动画之使用 animate.css
- Ionic3学习笔记(一)
- Android学习笔记六:SQLite使用
- android学习笔记之SQLite数据库存储
- Android学习笔记(六)SQLite
- Ionic3学习笔记(二)主题化
- Ionic3学习笔记(七)Storage
- ionic3学习笔记
- ionic3学习笔记(二)
- ionic3学习笔记(三)
- ionic3学习笔记(四)
- Android数据存储(六)、SQLite数据库使用实例
- Docker学习笔记六:存储(未完成)
- Docker学习笔记六:存储(未完成)
- android学习笔记之十一数据存储(Shared Preferences、SQLite)
- android学习笔记之十一数据存储(Shared Preferences、SQLite)
- (笔记)神经网络压缩,Ristretto: Hardware-Oriented Approximation of Convolutional Neural Networks(一)
- Lua学习笔记8-迭代器
- Openstack之Nova创建虚机流程分析
- 《go语言网络编程》笔记
- Bootstrap的基本操作
- Ionic3学习笔记(六)存储之使用 SQLite
- codeblock的安装与使用
- 给定一个整数n,和k,返回包含k个所有所有子数组
- maven 项目提示找不到javax.servlet.xxx问题解决
- Linux常用工具及命令
- 判断素数
- JS 对象(Object)和字符串(String)互转方法
- Tomcat配置Web虚拟目录
- java 程序为什么总是运行第一个程序