Android中数据库的工作方式是什么

Android中数据库的工作方式是什么,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

创新互联公司长期为上1000+客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为张家口企业提供专业的做网站、网站建设张家口网站改版等技术服务。拥有十载丰富建站经验和众多成功案例,为您定制开发。

下面的代码片段显示了一个标准数据库适配器类的框架。它包括一个SQLiteOpenHelper类的扩展类,用于简化打开、创建和更新Android数据库。

  1. import android.content.Context;  

  2. import android.database.*;  

  3. import android.database.sqlite.*;  

  4. import android.database.sqlite.SQLiteDatabase.CursorFactory;  

  5. import android.util.Log;  

  6. public class MyDBAdapter   

  7. {  

  8. private static final String DATABASE_NAME = “myDatabase.db”;  

  9. private static final String DATABASE_TABLE = “mainTable”;  

  10. private static final int DATABASE_VERSION = 1;  

  11. // The index (key) column name for use in where clauses.  

  12. public static final String KEY_ID=”_id”;  

  13. // The name and column index of each column in your database.  

  14. public static final String KEY_NAME=”name”;  

  15. public static final int NAME_COLUMN = 1;  

  16. // TODO: Create public field for each column in your table.  

  17. // SQL Statement to create a new database.  

  18. private static final String DATABASE_CREATE = “create table “ +  

  19. DATABASE_TABLE + “ (“ + KEY_ID + “ integer primary key autoincrement, “ +  

  20. KEY_NAME + “ text not null);”;  

  21. // Variable to hold the database instance  

  22. private SQLiteDatabase db;  

  23. // Context of the application using the database.  

  24. private final Context context;  

  25. // Database open/upgrade helpe  

  26. private myDbHelper dbHelper;  

  27. public MyDBAdapter(Context _context) {  

  28. context = _context;  

  29. dbHelper = new myDbHelper(context, DATABASE_NAME, null, DATABASE_VERSION);  

  30. }  

  31. public MyDBAdapter open() throws SQLException {  

  32. db = dbHelper.getWritableDatabase();  

  33. return this;  

  34. }  

  35. public void close() {  

  36. db.close();  

  37. }  

  38. public long insertEntry(MyObject _myObject) {  

  39. ContentValues contentValues = new ContentValues();  

  40. // TODO fill in ContentValues to represent the new row  

  41. return db.insert(DATABASE_TABLE, null, contentValues);  

  42. }  

  43. public boolean removeEntry(long _rowIndex) {  

  44. return db.delete(DATABASE_TABLE, KEY_ID + “=” + _rowIndex, null) > 0;  

  45. }  

  46. public Cursor getAllEntries () {  

  47. return db.query(DATABASE_TABLE, new String[] {KEY_ID, KEY_NAME},  

  48. null, null, null, null, null);  

  49. }  

  50. public MyObject getEntry(long _rowIndex) {  

  51. MyObject objectInstance = new MyObject();  

  52. // TODO Return a cursor to a row from the database and  

  53. // use the values to populate an instance of MyObject  

  54. return objectInstance;  

  55. }  

  56. public int updateEntry(long _rowIndex, MyObject _myObject) {  

  57. String where = KEY_ID + “=” + _rowIndex;  

  58. ContentValues contentValues = new ContentValues();  

  59. // TODO fill in the ContentValue based on the new object  

  60. return db.update(DATABASE_TABLE, contentValues, where, null);  

  61. }  

  62. private static class myDbHelper extends SQLiteOpenHelper   

  63. {  

  64. public myDbHelper(Context context, String name, CursorFactory factory, 
    int version) {  

  65. super(context, name, factory, version);  

  66. }  

  67. // Called when no database exists in  

  68. // disk and the helper class needs  

  69. // to create a new one.  

  70. @Override  

  71. public void onCreate(SQLiteDatabase _db) {  

  72. _db.execSQL(DATABASE_CREATE);  

  73. }  

  74. // Called when there is a database version mismatch meaning that  

  75. // the version of the database on disk needs to be upgraded to  

  76. // the current version.  

  77. @Override  

  78. public void onUpgrade(SQLiteDatabase _db, int _oldVersion, 
    int _newVersion) {  

  79. // Log the version upgrade.  

  80. Log.w(“TaskDBAdapter”, “Upgrading from version “ +  

  81. _oldVersion + “ to “ + _newVersion +  

  82. “, which will destroy all old data”);  

  83. // Upgrade the existing database to conform to the new version.  

  84. // Multiple previous versions can be handled by comparing  

  85. // _oldVersion and _newVersion values.  

  86. // The simplest case is to drop the old table and create a  

  87. // new one.  

  88. _db.execSQL(“DROP TABLE IF EXISTS “ + DATABASE_TABLE);  

  89. // Create a new one.  

  90. onCreate(_db);  

  91. }  

  92. }  

关于Android中数据库的工作方式是什么问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。

分享文章:Android中数据库的工作方式是什么
文章网址:https://www.cdcxhl.com/article42/joohec.html

成都网站建设公司_创新互联,为您提供企业建站企业网站制作网站建设关键词优化标签优化微信公众号

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

绵阳服务器托管