Android Small Note database

Source: Internet
Author: User
Tags name database

Create a table

CREATE TABLE person (_id integer PRIMARY key autoincrement,name varchar (+), age integer)

Inserting data

INSERT into person values (1, "Bojie", 18)

INSERT into person values (2, "Pange", 18)

Delete data

Delete from person where name= "Pange"

Querying data

Select name from the person where _id=2

modifying data

Update person set name= "Pange+tanji" where name= "Pange"


Sqlite

public class Mysqlitehelper extends Sqliteopenhelper {

/**

* Version 1

* Factory data lookup cursors default null>>> cursor

* Name Database name

* @param context

*/

Public Mysqlitehelper (context context) {

Super (context, "my.db", NULL, 1);

//TODO auto-generated constructor stub

}

@Override

public void onCreate (Sqlitedatabase db) {

db.execsql ("CREATE TABLE Person" (_id integer PRIMARY key autoincrement,name varchar (+), age integer) ");


}

DAO layer


public class Personservice {


Private Mysqlitehelper Helper;


Public Personservice (Context context) {

Helper = new Mysqlitehelper (context);

}

/**

* Insert Data

* @param p

*/

public void Insert (person p) {

Sqlitedatabase db = Helper.getwritabledatabase ();

Db.execsql ("INSERT INTO person values" ("+p.getname () +", "+p.getage () +") ");

if (Db.isopen ()) {

Db.execsql ("INSERT into person (name,age) VALUES (?,?)", New Object[]{p.getname (), P.getage ()});

}

Db.close ();

}

/**

* Delete Data

* @param name

*/

public void del (String name) {

Sqlitedatabase db = Helper.getwritabledatabase ();

if (Db.isopen ()) {

Db.execsql ("Delete from person where name=?", New String[]{name});

}

Db.close ();

}

/**

* Modify Data

* @param oldname

* @param newName

*/

public void Update (String oldname,string newName) {

Sqlitedatabase db = Helper.getwritabledatabase ();

if (Db.isopen ()) {

Db.execsql ("Update person set name=?") Where Name=? ", New String[]{newname,oldname});

}

Db.close ();

}

/**

* Query all data

* @return

*/

Public list<person> GetAll () {

List<person> lists=new arraylist<person> ();

Sqlitedatabase db = Helper.getreadabledatabase ();


if (Db.isopen ()) {

cursor cursor = db.rawquery ("Select name, age from person", NULL);

if (Cursor!=null&&cursor.getcount () >0) {

while (Cursor.movetonext ()) {

String name = cursor.getstring (0);

int age = Cursor.getint (1);

Person Person=new person (name, age);

Lists.add (person);

}

}

Cursor.close ();

return lists;

}

Db.close ();

return null;

}

/**

* Query the corresponding data

* @param ID

* @return

*/

Public person Getpersonbyid (int id) {

Sqlitedatabase db = Helper.getreadabledatabase ();

Person Person=new person ();

if (Db.isopen ()) {

cursor cursor = db.rawquery ("Select Name,age from person where _id=?", new String[]{string.valueof (ID)});

if (Cursor!=null&&cursor.getcount () >0) {

if (Cursor.movetolast ()) {

String name = cursor.getstring (0);

Person.setname (name);

int age = Cursor.getint (1);

Person.setage (age);

}

}

Cursor.close ();

return person;

}

Db.close ();

return null;

}



This article is from the "Android Small Notes" blog, please be sure to keep this source http://dreamwing.blog.51cto.com/9872128/1612138

Android Small Note 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.