Ionic1 SQLite Add and use

Source: Internet
Author: User
Tags sqlite

The reason to start using this storage method is that the previously used local Storage is stored on the iOS device when the memory reaches a certain level and iOS automatically clears part of the app's storage so that things that were previously stored may be erased to the desired functional effect, using SQLite

First, a Chinese version of the Official document: Http://www.runoob.com/sqlite/sqlite-tutorial.html (too much too messy don't want to see)

Refer to someone else's blog link: https://yq.aliyun.com/articles/69473 (using the method is clear to add, delete, find but some of the explanations are not clear there are problems different points of view I will say clearly the 3rd)

1. Add Plugins

cordova plugin add https://github.com/brodysoft/Cordova-SQLitePlugin.git

2. Add Dependency Ingest Plugin

Add Ngcordova dependency angular.module (' myApp ', [' Ngcordova '] in JS)

Introduction of JS file in index.html <script src= "Lib/ngcordova/dist/ng-cordova.js" ></script>

3. Create a database after the platform is ready

Don't forget to inject $cordovaSQLite

Name is the names of the databases note that it's important to add location:1. This is not the case when the database was not created successfully and didn't find a lot of places.

There's a little ionic server. Many people say that browsers do not support the fact that it is possible to determine after the following method to create a database is available

$ionicPlatform. Ready (function () {    //CREATE DATABASE    if (Window.cordova) {      $rootScope. db = $cordovaSQLite. OPENDB ({        Name: "My.db",        location:1      });//device    } else {      $rootScope. db = Window.opendatabase ("my.db", ' 1 ', ' my ', 1024x768 * 1024x768 *);//Browser    }    //CREATE TABLE    $c Ordovasqlite.execute ($rootScope. db, "CREATE TABLE IF not EXISTS appsteptime (edition text)");        });

I've got the database as a global variable, because I'm going to use it in other places. If only the JS file is processed, it can be defined as a local variable.

When I created the table, my appsteptime was the name of my table. Only one field is stored edition type is text if you need to create more than one field, the following code: "Create TABLE if not EXISTS people (ID I Nteger primary Key, FirstName text, LastName text) "

4. Database query data Insert data

My judgment here is that if the database does not have this field to insert this field and does not judge the value of the field if you need to refer to the link above

Database query data $cordovasqlite.execute ($rootScope. DB, "Select Edition from Appsteptime"). Then (function (res) {   if ( Res.rows.length > 0) {     //have data to make corresponding processing   } else {     //alert ("No results found"); no data for processing     //Database Insert data     $ Cordovasqlite.execute ($rootScope. db, "INSERT into Appsteptime (edition) VALUES (?)", [$rootScope. Editionnum]). Then ( Function (res) {       //alert ("INSERT ID-+" + Res.insertid);     }, Function (err) {       //alert (err);     });   }, function (err) {   //alert (err);});

If the friend who has learned the database should be very clear that the query is a select Insert is the insert Delete is delete (I have studied in college but only fur)

5. Deleting a database

I have not done data deletion, so the specific method of deleting data can refer to the Official document: http://www.runoob.com/sqlite/sqlite-delete.html

Delete Database method: $cordovaSQLite. Deletedb("my.db");

Ionic1 SQLite Add and use

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.