NOVOTS KMS 词汇表 Glossary    联系我们 Contact Us
查询 Search  
   
按类别浏览 Browse by Category
NOVOTS KMS .: 数据库 .: 实现一个简单的Sqlite数据库,存储人名和电话号码

实现一个简单的Sqlite数据库,存储人名和电话号码

主Activity 类SqliteSample.java 代码:
  1. package jtapp.sqlitesamples;

  2. import android.app.Activity;
  3. import android.database.Cursor;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. import android.widget.TextView;

  9. public class SqliteSample extends Activity {
  10. private DBHelper mDBHelper = null;
  11. private Button btCreateDb = null;
  12. private Button btInsertData = null;
  13. private Button btViewData = null;
  14. private Button btDelOne = null;
  15. private Button btClearAll = null;

  16. /** Called when the activity is first created. */
  17. @Override
  18. public void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.main);

  21. btCreateDb = (Button)findViewById(R.id.Button01);
  22. btCreateDb.setOnClickListener(new ClickViewHandler());
  23. btInsertData = (Button)findViewById(R.id.Button02);
  24. btInsertData.setOnClickListener(new ClickViewHandler());
  25. btInsertData.setEnabled(false);
  26. btViewData = (Button)findViewById(R.id.Button03);
  27. btViewData.setOnClickListener(new ClickViewHandler());
  28. btViewData.setEnabled(false);
  29. btDelOne = (Button)findViewById(R.id.Button04);
  30. btDelOne.setOnClickListener(new ClickViewHandler());
  31. btDelOne.setEnabled(false);
  32. btClearAll = (Button)findViewById(R.id.Button05);
  33. btClearAll.setOnClickListener(new ClickViewHandler());
  34. btClearAll.setEnabled(false);
  35. }

  36. public class ClickViewHandler implements OnClickListener {
  37. @Override
  38. public void onClick(View v) {
  39. if (v == btCreateDb) {
  40. createDB();
  41. } else if (v == btInsertData) {
  42. insertSomeRecords();
  43. } else if (v == btViewData) {
  44. ViewRecords();
  45. } else if (v == btDelOne) {
  46. DelOne();
  47. } else if (v == btClearAll) {
  48. mDBHelper.delAllPeople();
  49. }
  50. }
  51. }

  52. private void createDB() {
  53. String DB_NAME = "sqlitedb1";
  54. mDBHelper = new DBHelper(
  55. SqliteSample.this, DB_NAME, null, 1);
  56. assert(mDBHelper != null);
  57. btCreateDb.setEnabled(false);
  58. btInsertData.setEnabled(true);
  59. btViewData.setEnabled(true);
  60. btDelOne.setEnabled(true);
  61. btClearAll.setEnabled(true);
  62. }
  63. private void insertSomeRecords() {
  64. mDBHelper.addPeople("一休", "18602155856");
  65. mDBHelper.addPeople("巴巴", "13368565525");
  66. mDBHelper.addPeople("项羽", "057156856225");
  67. }
  68. private void ViewRecords() {
  69. // Make the query
  70. Cursor c = mDBHelper.getWritableDatabase().query(
  71. DBHelper.TB_NAME,null,null,null,null,null,
  72. DBHelper.NAME + " ASC");
  73. StringBuilder sbRecords = new StringBuilder("");
  74. if (c.moveToFirst()) {
  75. int idxID = c.getColumnIndex(DBHelper.ID);
  76. int idxName = c.getColumnIndex(DBHelper.NAME);
  77. int idxNumber = c.getColumnIndex(DBHelper.NUMBER1);
  78. // Iterator the records
  79. do {
  80. sbRecords.append(c.getInt(idxID));
  81. sbRecords.append(". ");
  82. sbRecords.append(c.getString(idxName));
  83. sbRecords.append(", ");
  84. sbRecords.append(c.getString(idxNumber));
  85. sbRecords.append("/n");
  86. } while (c.moveToNext());
  87. }
  88. c.close();
  89. // Refresh the content of TextView
  90. ((TextView)(findViewById(
  91. R.id.TextView01))).setText(sbRecords);
  92. }
  93. private void DelOne() {
  94. int id;
  95. Cursor c = mDBHelper.getWritableDatabase().query(
  96. DBHelper.TB_NAME,null,null,null,null,null,
  97. DBHelper.NAME + " ASC");
  98. if (c.moveToFirst()) {
  99. int idxID = c.getColumnIndex(DBHelper.ID);
  100. id = c.getInt(idxID);
  101. mDBHelper.delPeople(id);
  102. }
  103. }
  104. }

ui界面 main.xml 代码,主要是几个bt的放置:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical" android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <TextView android:layout_width="fill_parent"
  6. android:layout_height="wrap_content" android:text="@string/hello" />
  7. <TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content" android:layout_height="wrap_content">
  8. <Button android:text="create db" android:id="@+id/Button01"
  9. android:height="30px" android:layout_width="wrap_content"
  10. android:layout_height="wrap_content" />
  11. <Button android:text="insert some records" android:id="@+id/Button02"
  12. android:height="30px" android:layout_width="wrap_content"
  13. android:layout_height="wrap_content" />
  14. </TableRow>
  15. <TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content" android:layout_height="wrap_content">
  16. <Button android:text="delete one" android:id="@+id/Button04"
  17. android:height="30px" android:layout_width="wrap_content"
  18. android:layout_height="wrap_content" />
  19. <Button android:text="clear all" android:id="@+id/Button05"
  20. android:height="30px" android:layout_width="wrap_content"
  21. android:layout_height="wrap_content" />
  22. </TableRow>
  23. <Button android:text="view records" android:id="@+id/Button03"
  24. android:height="30px" android:layout_width="wrap_content"
  25. android:layout_height="wrap_content" android:layout_gravity="center"/>
  26. <TextView android:text="..." android:id="@+id/TextView01"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content" />
  29. </LinearLayout>

数据库操作类 DBHelper.java 代码:

  1. package jtapp.sqlitesamples;

  2. import android.content.ContentValues;
  3. import android.content.Context;
  4. import android.database.sqlite.SQLiteDatabase;
  5. import android.database.sqlite.SQLiteDatabase.CursorFactory;
  6. import android.database.sqlite.SQLiteOpenHelper;

  7. public class DBHelper extends SQLiteOpenHelper {

  8. public static final String TB_NAME = "people";
  9. public static final String ID = "_id";
  10. public static final String NAME = "name";
  11. public static final String NUMBER1 = "number1";

  12. public DBHelper(Context context, String name,
  13. CursorFactory factory, int version) {
  14. super(context, name, factory, version);
  15. this.getWritableDatabase();
  16. }

  17. /**
  18. * should be invoke when you never use DBhelper
  19. * To release the database and etc.
  20. */
  21. public void Close() {
  22. this.getWritableDatabase().close();
  23. }

  24. public void onCreate(SQLiteDatabase db) {
  25. db.execSQL("CREATE TABLE IF NOT EXISTS "
  26. + TB_NAME + " ("
  27. + ID + " INTEGER PRIMARY KEY,"
  28. + NAME + " VARCHAR,"
  29. + NUMBER1 + " VARCHAR)");
  30. }

  31. public void onUpgrade(SQLiteDatabase db,
  32. int oldVersion, int newVersion) {
  33. db.execSQL("DROP TABLE IF EXISTS "+TB_NAME);
  34. onCreate(db);
  35. }

  36. public void addPeople(String name, String number) {
  37. ContentValues values = new ContentValues();
  38. values.put(DBHelper.NAME, name);
  39. values.put(DBHelper.NUMBER1, number);
  40. this.getWritableDatabase().insert(
  41. DBHelper.TB_NAME, DBHelper.ID, values);
  42. }

  43. public void delPeople(int id) {
  44. this.getWritableDatabase().delete(
  45. DBHelper.TB_NAME, this.ID + " = " + id, null);
  46. }

  47. public void delAllPeople() {
  48. this.getWritableDatabase().delete(
  49. DBHelper.TB_NAME, null, null);
  50. }
  51. }
数据库操作主要实现了创建数据库,升级数据库的具体操作,封装了插入单条数据,删除单条数据,和删除全部数据的操作。


这篇文章对你多有用?

相关文章

article MSN登陆后部分联系人名称为乱码
解决方法如下: 关闭MSN Live Messenger ...

(No rating)  9-26-2011    Views: 1281   

用户评语

添加评语
当前还没有评语.


.: .: .: .: .:
[ 登陆 ]
北京护航科技有限公司 2006

Novots Technologies Limited