Android SQLite database operation

Source: Internet
Author: User
Tags gettext sqlite database

SQLite is a little different from other common database, that is, the SQLite database is stored in a file, you can directly export the file from the phone, as a file, and then put on the computer to view.

The Android operations database has the following steps:

1, inherit Sqliteopenhelper, realize the method inside.

Public class mydbhelper extends sqliteopenhelper {    public  mydbhelper (Context context)  {        super (Context,   "DB3",  null, 1);    }     @Override      public void oncreate (SQLITEDATABASE DB)  {         db.execsql ("Create table db (id int primary key autoincrement,name  varchar (ten), Phone varchar (15)); ");     }     @Override     public void  Onupgrade (sqlitedatabase db, int oldversion, int newversion)  {         db.execsql ("Alert table db add column yuan varchar (11); ");     }}2, creating a DAO layer that acts like the DAO layer in the SSH framework in Java to manipulate the database     public class contactinfodao {    private final mydbhelper  mmydbhelper;    private final sqlitedatabase mdb;    // Constructs an object     public contactinfodao (Context context) that generates an operations database directly when the class's entity object is generated {         mmydbhelper = new mydbhelper (context);         mdb = mmydbhelper.getwritabledatabase ();     }        /** *  Add contact information  *  @param  name *   @param  phone */public void add (string name,string phone) {     mdb.execsql ("Insert into db3 (Name,phone)  values (?,?)", New object[]{name,phone} );     mdb.close ();}     }

3. After writing the corresponding method in the DAO layer, invoke the method in DAO in the activity for business logic

package com.yuanlp.createdb3;import android.os.bundle;import  android.support.v7.app.appcompatactivity;import android.text.textutils;import android.view.view; import android.widget.edittext;import android.widget.toast;import  com.yuanlp.createdb3.dao.contactinfodao;public class mainactivity extends  appcompatactivity {    private edittext mname;     private edittext mphone;    private contactinfodao mcontactinfodao;      @Override     protected void oncreate (bundle  savedinstancestate)  {        super.oncreate (savedInstanceState);         setcontentview (r.layout.layout);         mName =  (EditText)  findviewbyid (r.id.et_name);        mPhone =  (EditText)  findviewbyid (r.id.et_phone);         mcontactinfodao = new contactinfodao (this);     }     /**     *  inserting a database      * @ author yuan     *  @param  view     */     public void add (View view) {         String name=mname.gettext (). toString (). Trim ();         string  phone=mphone.gettext (). toString (). Trim ();        if  ( Textutils.isempty (name) | | Name==null) {            toast.maketext (this, " Name cannot be empty ", Toast.length_short). Show ();             return;        }else if  (Textutils.isempty (phone) | | Phone==null) {            toast.maketext (this, " Number cannot be empty ", Toast.length_short). Show ();             return;        }         Mcontactinfodao.add (Name,phone);         toast.maketext (this, "Insert database succeeded", Toast.length_short). Show ();     }}


This article is from the "Yuangushi" blog, make sure to keep this source http://cm0425.blog.51cto.com/10819451/1940040

Android SQLite database operation

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.