如何进行Android数据库操作
展开全部
在自己Android数据库接收或发出一个系统action的时候,要名副其实。比如你响应一个view动作,做的确实edit的勾当,你发送一个pick消息,其实你想让别人做edit的事,这样都会造成混乱。
一个好的习惯是创建一个辅助类来简化你的Android数据库交互。考虑创建一个数据库适配器,来添加一个与数据库交互的包装层。它应该提供直观的、强类型的方法,如添加、删除和更新项目。数据库适配器还应该处理查询和对创建、打开和关闭数据库的包装。
它还常用静态的Android数据库常量来定义表的名字、列的名字和列的索引。下面的代码片段显示了一个标准数据库适配器类的框架。它包括一个SQLiteOpenHelper类的扩展类,用于简化打开、创建和更新数据库。
import android.content.Context; import android.database.*; import android.database.sqlite.*; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.util.Log; public class MyDBAdapter { // The name and column index of each column in your database. public static final String KEY_NAME=”name”; public static final int NAME_COLUMN = 1; // TODO: Create public field for each column in your table. // SQL Statement to create a new database. private static final String DATABASE_CREATE = “create table “ + DATABASE_TABLE + “ (“ + KEY_ID + “ integer primary key autoincrement, “ + KEY_NAME + “ text not null);”; // Variable to hold the database instance private SQLiteDatabase db; // Context of the application using the database. private final Context context; // Database open/upgrade helper private myDbHelper dbHelper; public MyDBAdapter(Context _context) { context = _context; dbHelper = new myDbHelper(context, DATABASE_NAME, null, DATABASE_VERSION); } public MyDBAdapter open() throws SQLException { db = dbHelper.getWritableDatabase(); return this; } public void close() { db.close(); } public long insertEntry(MyObject _myObject) { ContentValues contentValues = new ContentValues(); // TODO fill in ContentValues to represent the new row return db.insert(DATABASE_TABLE, null, contentValues); } public boolean removeEntry(long _rowIndex) { return db.delete(DATABASE_TABLE, KEY_ID + “=” + _rowIndex, null) > 0; } public Cursor getAllEntries () { return db.query(DATABASE_TABLE, new String[] {KEY_ID, KEY_NAME}, null, null, null, null, null); } public MyObject getEntry(long _rowIndex) { MyObject objectInstance = new MyObject(); // TODO Return a cursor to a row from the database and // use the values to populate an instance of MyObject return objectInstance; } public int updateEntry(long _rowIndex, MyObject _myObject) { String where = KEY_ID + “=” + _rowIndex; ContentValues contentValues = new ContentValues(); // TODO fill in the ContentValue based on the new object return db.update(DATABASE_TABLE, contentValues, where, null); } private static class myDbHelper extends SQLiteOpenHelper { public myDbHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); } // Called when no database exists in // disk and the helper class needs // to create a new one. @Override public void onCreate(SQLiteDatabase _db) { _db.execSQL(DATABASE_CREATE); }
一个好的习惯是创建一个辅助类来简化你的Android数据库交互。考虑创建一个数据库适配器,来添加一个与数据库交互的包装层。它应该提供直观的、强类型的方法,如添加、删除和更新项目。数据库适配器还应该处理查询和对创建、打开和关闭数据库的包装。
它还常用静态的Android数据库常量来定义表的名字、列的名字和列的索引。下面的代码片段显示了一个标准数据库适配器类的框架。它包括一个SQLiteOpenHelper类的扩展类,用于简化打开、创建和更新数据库。
import android.content.Context; import android.database.*; import android.database.sqlite.*; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.util.Log; public class MyDBAdapter { // The name and column index of each column in your database. public static final String KEY_NAME=”name”; public static final int NAME_COLUMN = 1; // TODO: Create public field for each column in your table. // SQL Statement to create a new database. private static final String DATABASE_CREATE = “create table “ + DATABASE_TABLE + “ (“ + KEY_ID + “ integer primary key autoincrement, “ + KEY_NAME + “ text not null);”; // Variable to hold the database instance private SQLiteDatabase db; // Context of the application using the database. private final Context context; // Database open/upgrade helper private myDbHelper dbHelper; public MyDBAdapter(Context _context) { context = _context; dbHelper = new myDbHelper(context, DATABASE_NAME, null, DATABASE_VERSION); } public MyDBAdapter open() throws SQLException { db = dbHelper.getWritableDatabase(); return this; } public void close() { db.close(); } public long insertEntry(MyObject _myObject) { ContentValues contentValues = new ContentValues(); // TODO fill in ContentValues to represent the new row return db.insert(DATABASE_TABLE, null, contentValues); } public boolean removeEntry(long _rowIndex) { return db.delete(DATABASE_TABLE, KEY_ID + “=” + _rowIndex, null) > 0; } public Cursor getAllEntries () { return db.query(DATABASE_TABLE, new String[] {KEY_ID, KEY_NAME}, null, null, null, null, null); } public MyObject getEntry(long _rowIndex) { MyObject objectInstance = new MyObject(); // TODO Return a cursor to a row from the database and // use the values to populate an instance of MyObject return objectInstance; } public int updateEntry(long _rowIndex, MyObject _myObject) { String where = KEY_ID + “=” + _rowIndex; ContentValues contentValues = new ContentValues(); // TODO fill in the ContentValue based on the new object return db.update(DATABASE_TABLE, contentValues, where, null); } private static class myDbHelper extends SQLiteOpenHelper { public myDbHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); } // Called when no database exists in // disk and the helper class needs // to create a new one. @Override public void onCreate(SQLiteDatabase _db) { _db.execSQL(DATABASE_CREATE); }
浙江启扬智能科技有限公司
2023-06-12 广告
2023-06-12 广告
Android和ARM、Linux之间存在密切的联系。Android是一种基于Linux内核的嵌入式智能操作系统,它采用了ARM处理器作为其主要处理器架构。Android的内核和许多应用程序都是基于ARM架构编写的,包括处理器和内存管理器。...
点击进入详情页
本回答由浙江启扬智能科技有限公司提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询