How does Android connect to the SQLite database?

Source: Internet
Author: User
Tags sqlite database

SQLite database
first, a subclass of Sqliteopenhelper () is established to implement the OnCreate () method and the construction method in Sqliteopenhelper. This class takes care of opening the database if it exists, creating it if it does not, and upgrading it as necessary.

Sqliteopenhelper () is primarily the object that provides the database, where the Getwriteabledatabase

public class Personsqliteopenhelper extends Sqliteopenhelper
{
Public Personsqliteopenhelper (context context)
{
Super (context, "person.db", NULL, 1);
}

/**
* method to be called when the first time is created
*/
@Override
public void OnCreate (Sqlitedatabase db)
{
Db.execsql ("CREATE TABLE person (ID primary key autoincrement,name varchar (), number varchar (20)");
}

@Override
public void Onupgrade (sqlitedatabase arg0, int arg1, int arg2)
{

}

}

With the Sqliteopenhelper subclass, the reference Hrelper can be established, and the database Db=helper.getwritabledatabase () is obtained by reference;
Db.execsql ("Select * from table");
Establish classes related to database operations (selection)
public class Persondao
{
Private Personsqliteopenhelper Helper;

Public Persondao (Context context)
{
Helper = new Personsqliteopenhelper (context);
}
/**
*
* @param name
* @param number
*/
public void Add (string name, string number)
{
Sqlitedatabase db = Helper.getwritabledatabase ();
Db.execsql ("INSERT into person (name,number) VALUES (?,?)", new object[] {
Name, number});
Db.close ();
}

/**
*
* @param name
* @return
*/
public boolean find (String name)
{
Sqlitedatabase db = Helper.getreadabledatabase ();
cursor cursor = db.rawquery ("Select *from person where name=?",
New string[] {name});
Boolean result = Cursor.movetonext ();
Cursor.close ();
Db.close ();
return result;
}

/**
*
* @param name
* @param number
*/
public void Update (string name, string number)
{
Sqlitedatabase db = Helper.getwritabledatabase ();
Db.execsql ("Update person set number=?") Where Name=? ", new object[] {
Number, name});
Db.close ();
}

/**
*
* @param name
*/
public void Delete (String name)
{
Sqlitedatabase db = Helper.getwritabledatabase ();
Db.execsql ("Delete from person where name=?", new object[] {name});
Db.close ();
}
/**
*
* @return
*/
Public list<person> FindAll ()
{
Sqlitedatabase db = Helper.getreadabledatabase ();
cursor cursor = db.rawquery ("Select *from person", NULL);
arraylist<person> list = new arraylist<person> ();
while (Cursor.movetonext ())
{
int id = cursor.getint (cursor.getcolumnindex ("id"));
String name = cursor.getstring (Cursor.getcolumnindex ("name"));
String number = cursor
. GetString (Cursor.getcolumnindex ("number"));
person person = new person (ID, name, number);
List.add (person);
}
return list;
}
}

Things
Open Things
Db.begintransaction ();
Try
{
Mark Things executed successfully
Db.settransactionsuccessful ();
} finally
{
Db.endtransaction ();
}

How does Android connect to the SQLite database?

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.