Android数据库相关代码解读(1)

来源:互联网 发布:xd苹果软件 编辑:程序博客网 时间:2024/05/20 05:06

Android数据库的操作方法多样化,掌握这些应用技巧对于我们的实际开发起着非常重要的作用。我们在这里就为大家介绍了一下相关操作技巧。

AD: 51CTO云计算架构师峰会 抢票进行中!

在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. }  
原创粉丝点击