Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 高級開發 >> Android數據庫相關代碼解讀(1)

Android數據庫相關代碼解讀(1)

編輯:高級開發

android 手機操作系統進行實際開發中,進場會應用數據庫。而且在這一平台中對數據庫的應用方法比較簡單靈活。我們在這裡就為大家詳細介紹了相關方法,希望可以給大家帶來一些幫助。

昨天進行了GUI界面設計,感受了一下android初次設計的愉悅,今天接著學習其SQLite數據庫試用,將昨天的例子中數據存到數庫中,並讀取查看一下。 具體看代碼(原寫的有點問題,再改寫如下):

1) android數據庫之庫操作類:

  1. package com.topsun;
  2. import android.content.Context;
  3. import android.database.Cursor;
  4. import android.database.sqlite.SQLiteDatabase;
  5. import android.util.Log;
  6. public class DBHelper {
  7. private static final String TAG = "UserDB_DBHelper.Java";
  8. private static final String DataBaseName = "UserDB";
  9. SQLiteDatabase db;
  10. Context context;
  11. public DBHelper(Context context) {
  12. this.open(context);
  13. }
  14. private void createTabel() {
  15. // TODO Auto-generated method stub
  16. String sql = "";
  17. try {
  18. sql = "CREATE TABLE IF NOT EXISTS TestUser (ID INTEGER
    PRIMARY KEY autoincrement, NAME TEXT, SEX TEXT, AGES INTEGER)";
  19. this.db.execSQL(sql);
  20. Log.v(TAG, "Create Table TestUser ok");
  21. } catch (Exception e) {
  22. Log.v(TAG, "Create Table TestUser fail");
  23. } finally {
  24. //this.db.close();
  25. Log.v(TAG, "Create Table TestUser ");
  26. }
  27. }
  28. public boolean save(String name, String sex, Integer ages) {
  29. String sql = "insert into TestUser values
    (null,'" + name + "','" + sex
  30. + "'," + ages + ")";
  31. try {
  32. this.db.execSQL(sql);
  33. Log.v(TAG, "insert Table TestUser 1 record ok");
  34. return true;
  35. } catch (Exception e) {
  36. Log.v(TAG, "insert Table TestUser 1 record fail");
  37. return false;
  38. } finally {
  39. //this.db.close();
  40. Log.v(TAG, "insert Table TestUser ");
  41. }
  42. }
  43. public Cursor loadAll() {
  44. Cursor cur = db.query("TestUser", new String[]
    { "ID", "NAME","SEX","AGES"}, null,
  45. null, null, null, null);
  46. return cur;
  47. }
  48. public void open(Context context){
  49. if (null == db || !this.db.isOpen()){
  50. this.context = context;
  51. this.db = context.openOrCreateDatabase(this.DataBaseName,
  52. context.MODE_PRIVATE, null);
  53. createTabel();
  54. Log.v(this.TAG, "create or Open DataBase。。。");
  55. }
  56. }
  57. public void close() {
  58. db.close();
  59. }
  60. }
  61. package com.topsun;
  62. import android.content.Context;
  63. import android.database.Cursor;
  64. import android.database.sqlite.SQLiteDatabase;
  65. import android.util.Log;
  66. public class DBHelper {
  67. private static final String TAG = "UserDB_DBHelper.Java";
  68. private static final String DataBaseName = "UserDB";
  69. SQLiteDatabase db;
  70. Context context;
  71. public DBHelper(Context context) {
  72. this.open(context);
  73. }
  74. private void createTabel() {
  75. // TODO Auto-generated method stub
  76. String sql = "";
  77. try {
  78. sql = "CREATE TABLE IF NOT EXISTS TestUser
    (ID INTEGER PRIMARY KEY autoincrement,
    NAME TEXT, SEX TEXT, AGES INTEGER)";
  79. this.db.execSQL(sql);
  80. Log.v(TAG, "Create Table TestUser ok");
  81. } catch (Exception e) {
  82. Log.v(TAG, "Create Table TestUser fail");
  83. } finally {
  84. //this.db.close();
  85. Log.v(TAG, "Create Table TestUser ");
  86. }
  87. }
  88. public boolean save(String name, String sex, Integer ages) {
  89. String sql = "insert into TestUser values
    (null,'" + name + "','" + sex
  90. + "'," + ages + ")";
  91. try {
  92. this.db.execSQL(sql);
  93. Log.v(TAG, "insert Table TestUser 1 record ok");
  94. return true;
  95. } catch (Exception e) {
  96. Log.v(TAG, "insert Table TestUser 1 record fail");
  97. return false;
  98. } finally {
  99. //this.db.close();
  100. Log.v(TAG, "insert Table TestUser ");
  101. }
  102. }
  103. public Cursor loadAll() {
  104. Cursor cur = db.query("TestUser", new String[]
    { "ID", "NAME","SEX","AGES"}, null,
  105. null, null, null, null);
  106. return cur;
  107. }
  108. public void open(Context context){
  109. if (null == db || !this.db.isOpen()){
  110. this.context = context;
  111. this.db = context.openOrCreateDatabase(this.DataBaseName,
  112. context.MODE_PRIVATE, null);
  113. createTabel();
  114. Log.v(this.TAG, "create or Open DataBase。。。");
  115. }
  116. }
  117. public void close() {
  118. db.close();
  119. }
  120. }


2) android數據庫交互代碼

  1. package com.topsun;
  2. import android.app.Activity;
  3. import android.database.Cursor;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6. import android.view.VIEw;
  7. import android.view.VIEw.OnClickListener;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. public class guiWindows extends Activity
    implements OnClickListener {
  11. EditText TEditname;
  12. EditText TEditsex;
  13. EditText TEditages;
  14. EditText TEditmerge;
  15. Button TSavebutton;
  16. Button TVIEwbutton;
  17. DBHelper db;
  18. /** Called when the activity is first created. */
  19. @Override
  20. public void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentVIEw(R.layout.main);
  23. this.TEditname = (EditText) this.findVIEwById
    (R.id.widgeteditname);
  24. this.TEditsex = (EditText) this.findVIEwById
    (R.id.widgeteditsex);
  25. this.TEditages = (EditText) this.findVIEwById
    (R.id.widgeteditages);
  26. TEditmerge = (EditText) this.findVIEwById
    (R.id.widgeteditmerge);
  27. this.TSavebutton = (Button) this.findVIEwById
    (R.id.widgetSavebutton);
  28. TVIEwbutton = (Button) this.findVIEwById
    (R.id.widgetVIEwbutton);
  29. this.db = new DBHelper(this);
  30. this.TSavebutton.setOnClickListener(this);
  31. this.TVIEwbutton.setOnClickListener(this);
  32. }
  33. @Override
  34. public void onClick(VIEw v) {
  35. // TODO Auto-generated method stub
  36. // this.TEditages.setText(this.TEditname.getText().
    toString()+this.TEditsex.getText().toString());
  37. if (v.getId() == R.id.widgetSavebutton) {
  38. try {
  39. this.db.open(this);
  40. this.db.save(this.TEditname.getText().toString(), this.TEditsex
  41. .getText().toString(), Integer.valueOf(this.TEditages
  42. .getText().toString()));
  43. } catch (Exception e) {
  44. Log.v("save data", "save data fail");
  45. } finally {
  46. this.db.close();
  47. }
  48. } else if (v.getId() == R.id.widgetVIEwbutton && null != db) {
  49. this.db.open(this);
  50. // 浏覽所有數據
  51. Cursor cur = db.loadAll();
  52. StringBuffer sf = new StringBuffer();
  53. cur.moveToFirst();
  54. while (!cur.isAfterLast()) {
  55. sf.append(cur.getInt(0)).append(" : ").append(cur.getString(1))
  56. .append(" : ").append(cur.getString(2)).append(" : ")
  57. .append(cur.getInt(3)).append("\n");
  58. cur.moveToNext();
  59. }
  60. db.close();
  61. this.TEditmerge.setText(sf.toString());
  62. }
  63. }
  64. }
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved