Android:sqlitedatabase Study Summary

Source: Internet
Author: User
Tags sqlite database

Today, just after learning the basics of SQLite database, and then the learned things recorded, so that later review, the following is their own summary of the SQLite database:
1.Sqlite Introduction
SQLite is a lightweight database, it is contained in a relatively small C library, it is designed to be embedded, because it occupies very little resources, it may only need hundreds of K of memory, and support Windows/linux/unix and so on the mainstream operating system, At the same time can be combined with a lot of programming languages, such as: c#/java/php, etc., so in the embedded device is particularly popular, which is precisely in line with the development requirements of Android, so in Android development is often used in the database.
2.Sqlite internal structure
Internally, SQLite consists of several components: The SQL compiler, the kernel, the backend, and the attachments. SQLite makes it easier to debug, modify, and extend the kernel of SQLite by leveraging virtual machines and virtual database engines, all of which are compiled into easy-to-read assemblies that can be executed in an SQLite virtual machine. Its structure is as follows:

SQLite internal structure diagram
How to use SQLite in 3.android
To use the SQLite database in Android, you should first create a class that inherits the Sqliteopenhelper class, and we name the class Databasehelper as a helper class that accesses SQLite and provides two functions:
The first getreadabledatabase ()/getwritabledatabase () can obtain the Sqlitedatabase object, which can manipulate the database;
The second provides oncreate () and Onupgrade () two callback functions, allowing us to do our own work while creating and upgrading the database;
The instance code is as follows:
[Java]
public class Databasehelper extends Sqliteopenhelper {

private static final int VERSION = 1;
In Sqliteoepnhelper subclasses, you must have this constructor
Public Databasehelper (Context context, String name, Cursorfactory factory,
int version) {
Constructors in the parent class must be called through Super
Super (context, name, Factory, version);
TODO auto-generated Constructor stub
}
Public Databasehelper (Context context,string name) {
This (context,name,version);
}
Public Databasehelper (Context context,string name,int version) {
This (context, name,null,version);
}

This function is executed when the database is first created and is actually called when the first time the Sqlitedatabse object is obtained.
@Override
public void OnCreate (Sqlitedatabase db) {
TODO auto-generated Method Stub
System.out.println ("Create a Database");
The Execsql function is used to execute SQL statements
Db.execsql ("CREATE table user (ID int,name varchar (20))");
}

@Override
public void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {
TODO auto-generated Method Stub
SYSTEM.OUT.PRINTLN ("Update a Database");
}
}
Note: When inheriting the Sqliteopenhelper class, you must have public databasehelper (context context, String name, cursorfactory factory,int version) This construction method.
After defining the Databasehelper helper class, you can use this class to create a SQLite database and manipulate the database, which is illustrated by a well-defined activity, as follows:
[Java]
public class Sqliteactivity extends Activity {
/** called when the activity is first created. */
Private Button Createbutton;
Private Button Insertbutton;
Private Button UpdateButton;
Private Button Updaterecordbutton;
Private Button Querybutton;
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Createbutton = (Button) Findviewbyid (r.id.createdatabase);
UpdateButton = (Button) Findviewbyid (r.id.updatedatabase);
Insertbutton = (Button) Findviewbyid (R.id.insert);
Updaterecordbutton = (Button) Findviewbyid (r.id.update);
Querybutton = (Button) Findviewbyid (r.id.query);
Createbutton.setonclicklistener (New Createlistener ());
Updatebutton.setonclicklistener (New Updatelistener ());
Insertbutton.setonclicklistener (New Insertlistener ());
Updaterecordbutton.setonclicklistener (New Updaterecordlistener ());
Querybutton.setonclicklistener (New Querylistener ());
}
Class Createlistener implements onclicklistener{
@Override
public void OnClick (View v) {
Create a Databasehelper object
Databasehelper dbhelper = new Databasehelper (sqliteactivity.this, "test_mars_db");
Only the Getreadabledatabase () method of the Databasehelper object is called, or after the Getwritabledatabase () method is created, or a database is opened
Sqlitedatabase db = Dbhelper.getreadabledatabase ();
}
}
Class Updatelistener implements onclicklistener{

@Override
public void OnClick (View v) {
Databasehelper dbhelper = new Databasehelper (sqliteactivity.this, "test_mars_db", 2);
Sqlitedatabase db = Dbhelper.getreadabledatabase ();
}

}
Class Insertlistener implements onclicklistener{

@Override
public void OnClick (View v) {
Generating Contentvalues objects
Contentvalues values = new Contentvalues ();
You want to insert a key-value pair in the object, where the key is the column name, and the value is the value that you want to insert into the column, and the value must match the data type in the database
Values.put ("id", 1);
Values.put ("name", "Zhangsan");
Databasehelper dbhelper = new Databasehelper (sqliteactivity.this, "test_mars_db", 2);
Sqlitedatabase db = Dbhelper.getwritabledatabase ();
You can insert data into the database by calling the Insert method
Db.insert ("user", null, values);
}
}
The update operation is equivalent to executing the UPDATE statement in the SQL statement
UPDATE table_name SET xxcol=xxx WHERE xxcol=xx ...
Class Updaterecordlistener implements onclicklistener{

@Override
public void OnClick (View arg0) {
TODO auto-generated Method Stub
Get a writable Sqlitedatabase object
Databasehelper dbhelper = new Databasehelper (sqliteactivity.this, "test_mars_db");
Sqlitedatabase db = Dbhelper.getwritabledatabase ();
Contentvalues values = new Contentvalues ();
Values.put ("name", "Zhangsanfeng");
The first parameter is the name of the table to update
The second parameter is a Contentvaleus object
The third parameter is a WHERE clause
Db.update ("User", Values, "id=", New string[]{"1"});
}
}
Class Querylistener implements onclicklistener{

@Override
public void OnClick (View v) {
System.out.println ("AAA------------------");
LOG.D ("Mydebug", "myfirstdebugmsg");

Databasehelper dbhelper = new Databasehelper (sqliteactivity.this, "test_mars_db");
Sqlitedatabase db = Dbhelper.getreadabledatabase ();
cursor cursor = db.query ("User", new string[]{"id", "name"}, "id=", New string[]{"1"}, NULL, NULL, NULL);
while (Cursor.movetonext ()) {
String name = cursor.getstring (Cursor.getcolumnindex ("name"));
SYSTEM.OUT.PRINTLN ("Query--->" + name);
}
}
}

}
As can be seen in the above example, before creating or manipulating a database, you need to create a read-only database by creating the required database by using the Getreadbledatabase () and Getwritbledatabase () two methods in the Databasehelpe class. The latter is to create a writable database.

Android:sqlitedatabase Learning Summary

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.