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