Use of the SQLite database for Android

Source: Internet
Author: User

Mobile Android Development, we are using the SQLite database, which is a lightweight database, we use, and even do not want to load the same as MySQL database driver Ah, in the ANDROIDSDK, has helped us do, we only need to use the good, Here's how to use this,

First, we need to build a class to inherit the Sqliteopenhelper, and rewrite the Personsqliteopenhelper and OnCreate methods, the former is to build a name, the latter is to build the database structure of the script, These two methods are called when the database is first created. When it is built, it is no longer called.

/** * Database construction method, used to define database name, database query result set, database version. * @param context *///factory, cursor factory, set bit null,version database version. Public Personsqliteopenhelper (Context context) {Super (context, "person.db", NULL, 1);} /** * The first time the database was created, the method called * DB: Data created,/* @Overridepublic void OnCreate (Sqlitedatabase db) {//Initialize the database's table structure. Db.execsql ("CREATE TABLE person (ID integer PRIMARY key autoincrement,name varchar (), number varchar (20))");

Database has been built, we have to implement the database has been censored some of the operations, then we can choose SQL script or Google engineers packaged API for

  

public class Persondao {private Personsqliteopenhelper helper;public Persondao (context context) {helper = new Personsqliteopenhelper (context);} public void Add (String name,string number) {Sqlitedatabase db = Helper.getwritabledatabase ();d b.execsql ("INSERT INTO Person (Name,number) VALUES (?,?) ", New Object[]{name,number}),//db.execsql (SQL, Bindargs) db.close (); public boolean find (String name) {Sqlitedatabase db = Helper.getreadabledatabase (); cursor cursor = db.rawquery ("select * from person where name =?", new String[]{name});//The default result set refers to the first row, which is empty, and Boolean result = Cursor.movetonext (); Cursor.close ();d b.close (); return result;} Modify numberpublic void Update (String name,string number) {Sqlitedatabase db = Helper.getreadabledatabase ();d b.execsql (" Update person Set number =? WHERE name =? ", new Object[]{number,name});d b.close ();} public void Delete (String name) {Sqlitedatabase db = Helper.getreadabledatabase ();d b.execsql ("Delete from the person where Name =? ", new Object[]{name});} Public List<person> FindAll () {Sqlitedatabase db = Helper.getreadabledatabase (); cursor cursor = db.rawquery ("SELECT * from person", NULL); list<person> persons = new Arraylist<person> (), while (Cursor.movetonext ()) {//0//int id = cursor.getint (0); int id = cursor.getint (cursor.getcolumnindex ("id")); String name = cursor.getstring (1); String number = cursor.getstring (2); Person p = new person (ID, name, number);p Ersons.add (p);} Cursor.close ();d b.close (); Return persons;}}

public class Persondaoapi {private Personsqliteopenhelper helper;public Persondaoapi (context context) {helper = new Personsqliteopenhelper (context);} Public long Add (String name,string number) {Sqlitedatabase db = Helper.getwritabledatabase (),//db.execsql ("INSERT INTO Person (Name,number) VALUES (?,?) ", New Object[]{name,number}); Contentvalues values = new Contentvalues (), Values.put ("name", name), Values.put ("number", number),//return result >-1, indicating success. Long result = Db.insert ("person", null, Values);d b.close (); return result;} public boolean find (String name) {Sqlitedatabase db = Helper.getreadabledatabase ();//cursor Cursor = Db.rawquery ("Select * from the person where name =? ", new String[]{name});//db.query (' person ', columns, selection, Selectionargs, groupBy, having ,//columes=null), back to all columns. cursor cursor = db.query ("person", NULL, "Name=?", New String[]{name}, NULL, NULL, NULL);//The default result set refers to the first row, which is empty, and the Boolean ResU lt = Cursor.movetonext (); Cursor.close ();d b.close (); return result;} Modify Numberpublic int UPdate (String name,string number) {Sqlitedatabase db = Helper.getreadabledatabase ();//db.execsql ("Update person set Number =? WHERE name =? ", new Object[]{number,name}); Contentvalues values = new Contentvalues (), Values.put (number),//How many rows are affected, int row = db.update ("Person", values, " Name= ", New String[]{name});d b.close (); return row;} public int Delete (String name) {Sqlitedatabase db = Helper.getreadabledatabase ();//db.execsql ("Delete from the person where Name =? ", new Object[]{name});//The number of rows affected," 0 and success int row = Db.delete ("Person", "Name=?", New String[]{name});d b.close (); retur n Row;} Public list<person> FindAll () {Sqlitedatabase db = Helper.getreadabledatabase ();//cursor Cursor = Db.rawquery (" SELECT * FROM person ", NULL); cursor cursor = db.query ("Person", new string[]{"name", "id", "number"}, NULL, NULL, null,null, NULL); list<person> persons = new Arraylist<person> (), while (Cursor.movetonext ()) {//0//int id = cursor.getint (0); int id = cursor.getint (cursor.getcolumnindex ("id")); String name = cursor.getstring (Cursor.getcolumnindex ("name")); String number = cursor.getstring (Cursor.getcolumnindex ("number")); Person p = new person (ID, name, number);p Ersons.add (p);} Cursor.close ();d b.close (); Return persons;}}

When the database is built, where is it, and how do we find it?

Database is built, stored in the Android directory /data/data/package name/databases directory, we opened, we can just built the database, To export this. db file, with Sqlitemanager (MAC), or Sqlitexploer (Windows), you can open this database for viewing,

But/data/data/package name/databases This directory because of the control of permissions, we can only be in the simulator to open, the real machine debugging, is not open, then how do we use it,

At this point we can use the ADB command, first in the terminal can open this directory, so we first enter this directory, such as we do

# cd/data/data/com.ftf.db/databses# Sqlite3 person.db

You will find that it is no longer an instruction in the ADB mode, but it becomes the form of a SQL command, and this is the same as MySQL, when we can write the relevant SQL statement.

Use of the SQLite database for Android

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.