Android SQLite Data Operations

Source: Internet
Author: User

Main. xml file:


01 <? Xml version = "1.0" encoding = "UTF-8"?>

02 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"

03 android: orientation = "vertical"

04 android: layout_width = "fill_parent"

05 android: layout_height = "fill_parent"

06>

07 <Button

08 android: layout_width = "fill_parent"

09 android: layout_height = "wrap_content"

10 android: id = "@ + id/create"

11 android: text = "creating a database"

12/>

13 <Button

14 android: layout_width = "fill_parent"

15 android: layout_height = "wrap_content"

16 android: id = "@ + id/upgrade"

17 android: text = "Update Database"

18/>

19 <Button

20 android: layout_width = "fill_parent"

21 android: layout_height = "wrap_content"

22 android: id = "@ + id/insert"

23 android: text = "inserting data"

24/>

25 <Button

26 android: layout_width = "fill_parent"

27 android: layout_height = "wrap_content"

28 android: id = "@ + id/update"

29 android: text = "modify data"

30/>

31 <Button

32 android: layout_width = "fill_parent"

33 android: layout_height = "wrap_content"

34 android: id = "@ + id/select"

35 android: text = "querying data"

36/>

37 </LinearLayout>


The MyOpenHleper class inherited from SQLiteOpenHelper:
01 package com. android. danny. hleper;

02

03 import android. content. Context;

04 import android. database. sqlite. SQLiteDatabase;

05 import android. database. sqlite. SQLiteOpenHelper;

06

07 public class MyOpenHleper extends SQLiteOpenHelper {

08

09 public static final int VERSION = 1;

10

11 public MyOpenHleper (Context context, String name ){

12 super (context, name, null, VERSION );

13 // TODO Auto-generated constructor stub

14}

15

16 public MyOpenHleper (Context context, String name, int ver ){

17 super (context, name, null, ver );

18 // TODO Auto-generated constructor stub

19}

20 @ Override

21 public void onCreate (SQLiteDatabase db ){

22 System. out. println ("------ Create Database ----");

23 db.exe cSQL ("create table person (id int, name varchar (20 ))");

24}

25

26 @ Override

27 public void onUpgrade (SQLiteDatabase arg0, int arg1, int arg2 ){

28 // TODO Auto-generated method stub

29 System. out. println ("------ Upgrade Database ----");

30}

31

32}


SQLite data operation class:

01 package com. android. danny. db;

02

03 import com. android. danny. hleper. MyOpenHleper;

04

05 import android. app. Activity;

06 import android. content. ContentValues;

07 import android. database. Cursor;

08 import android. database. sqlite. SQLiteDatabase;

09 import android. OS. Bundle;

10 import android. view. View;

11 import android. view. View. OnClickListener;

12 import android. widget. Button;

13

14 public class Sqlite3 extends Activity implements OnClickListener {

15

16 Button create;

17 Button upgrade;

18 Button insert;

19 Button update;

20 Button select;

21

22 @ Override

23 public void onCreate (Bundle savedInstanceState ){

24 super. onCreate (savedInstanceState );

25 setContentView (R. layout. main );

26

27 create = (Button) findViewById (R. id. create );

28 upgrade = (Button) findViewById (R. id. upgrade );

29 insert = (Button) findViewById (R. id. insert );

30 update = (Button) findViewById (R. id. update );

31 select = (Button) findViewById (R. id. select );

32

33 create. setOnClickListener (this );

34 upgrade. setOnClickListener (this );

35 insert. setOnClickListener (this );

36 update. setOnClickListener (this );

37 select. setOnClickListener (this );

38}

39

40 @ Override

41 public void onClick (View v ){

42 MyOpenHleper dbDatabase;

43 if (v = create ){

44 dbDatabase = new MyOpenHleper (this, "test_db.db ");

45 // create a database

46 dbDatabase. getWritableDatabase ();

47

48} else if (v = upgrade ){

49 // update the database

50 dbDatabase = new MyOpenHleper (this, "test_db.db", 2 );

51 dbDatabase. getReadableDatabase ();

52

53} else if (v = insert ){

54 // insert data

55 ContentValues values = new ContentValues ();

56 values. put ("id", 1 );

57 values. put ("name", "danny ");

58 dbDatabase = new MyOpenHleper (this, "test_db.db ");

59 SQLiteDatabase database = dbDatabase. getWritableDatabase ();

60 database. insert ("person", null, values );

61

62} else if (v = update ){

63 // modify data

64 ContentValues values = new ContentValues ();

65 values. put ("name", "danny & kiki ");

66 dbDatabase = new MyOpenHleper (this, "test_db.db ");

67 SQLiteDatabase database = dbDatabase. getWritableDatabase ();

68 database. update ("person", values, "id =? ", New String [] {" 1 "});

69

70} else if (v = select ){

71 // data query operation

72 dbDatabase = new MyOpenHleper (this, "test_db.db ");

73 SQLiteDatabase database = dbDatabase. getReadableDatabase ();

74 Cursor cursor = database. query ("person", new String [] {"id", "name "},

75 "id =? ", New String [] {" 1 "},

76 null, null, null );

77 while (cursor. moveToNext ()){

78 String name = cursor. getString (cursor. getColumnIndex ("name "));

79 System. out. println ("query: ----->" + name );

80}

81}

82

83}

84}

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.