数据存储(3)---数据库存储
来源:互联网 发布:公司网络监控聊天记录 编辑:程序博客网 时间:2024/06/08 11:55
定义一个模式和合同
合同是一个都是常数的类,它定义的是URI的名称,表名,列名。
比如这样:
public final class FeedReaderContract { // To prevent someone from accidentally instantiating the contract class, // give it an empty constructor. public FeedReaderContract() {} /* Inner class that defines the table contents */ public static abstract class FeedEntry implements BaseColumns { public static final String TABLE_NAME = "entry"; public static final String COLUMN_NAME_ENTRY_ID = "entryid"; public static final String COLUMN_NAME_TITLE = "title"; public static final String COLUMN_NAME_SUBTITLE = "subtitle"; ... }}
创建一个数据库使用SQLHelper
private static final String TEXT_TYPE = " TEXT";private static final String COMMA_SEP = ",";private static final String SQL_CREATE_ENTRIES = "CREATE TABLE " + FeedEntry.TABLE_NAME + " (" + FeedEntry._ID + " INTEGER PRIMARY KEY," + FeedEntry.COLUMN_NAME_ENTRY_ID + TEXT_TYPE + COMMA_SEP + FeedEntry.COLUMN_NAME_TITLE + TEXT_TYPE + COMMA_SEP + ... // Any other options for the CREATE command " )";private static final String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS " + FeedEntry.TABLE_NAME;SQLiteOpenHelper子类:
public class FeedReaderDbHelper extends SQLiteOpenHelper { // If you change the database schema, you must increment the database version. public static final int DATABASE_VERSION = 1; public static final String DATABASE_NAME = "FeedReader.db"; public FeedReaderDbHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } public void onCreate(SQLiteDatabase db) { db.execSQL(SQL_CREATE_ENTRIES); } public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // This database is only a cache for online data, so its upgrade policy is // to simply to discard the data and start over db.execSQL(SQL_DELETE_ENTRIES); onCreate(db); } public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { onUpgrade(db, oldVersion, newVersion); }}初始化数据库帮助类:
FeedReaderDbHelper mDbHelper = new FeedReaderDbHelper(getContext());
插入数据库:
// Gets the data repository in write modeSQLiteDatabase db = mDbHelper.getWritableDatabase();// Create a new map of values, where column names are the keysContentValues values = new ContentValues();values.put(FeedEntry.COLUMN_NAME_ENTRY_ID, id);values.put(FeedEntry.COLUMN_NAME_TITLE, title);values.put(FeedEntry.COLUMN_NAME_CONTENT, content);// Insert the new row, returning the primary key value of the new rowlong newRowId;newRowId = db.insert( FeedEntry.TABLE_NAME, FeedEntry.COLUMN_NAME_NULLABLE, values);读取数据库:
SQLiteDatabase db = mDbHelper.getReadableDatabase();// Define a projection that specifies which columns from the database// you will actually use after this query.String[] projection = { FeedEntry._ID, FeedEntry.COLUMN_NAME_TITLE, FeedEntry.COLUMN_NAME_UPDATED, ... };// How you want the results sorted in the resulting CursorString sortOrder = FeedEntry.COLUMN_NAME_UPDATED + " DESC";Cursor c = db.query( FeedEntry.TABLE_NAME, // The table to query projection, // The columns to return selection, // The columns for the WHERE clause selectionArgs, // The values for the WHERE clause null, // don't group the rows null, // don't filter by row groups sortOrder // The sort order );把Cursor移到第一个位置:
cursor.moveToFirst();long itemId = cursor.getLong( cursor.getColumnIndexOrThrow(FeedEntry._ID));删除:
// Define 'where' part of query.String selection = FeedEntry.COLUMN_NAME_ENTRY_ID + " LIKE ?";// Specify arguments in placeholder order.String[] selectionArgs = { String.valueOf(rowId) };// Issue SQL statement.db.delete(table_name, selection, selectionArgs);更改:
SQLiteDatabase db = mDbHelper.getReadableDatabase();// New value for one columnContentValues values = new ContentValues();values.put(FeedEntry.COLUMN_NAME_TITLE, title);// Which row to update, based on the IDString selection = FeedEntry.COLUMN_NAME_ENTRY_ID + " LIKE ?";String[] selectionArgs = { String.valueOf(rowId) };int count = db.update( FeedReaderDbHelper.FeedEntry.TABLE_NAME, values, selection, selectionArgs);
0 0
- 数据存储(3)---数据库存储
- 数据存储详解(三)---->数据库存储
- Android数据存储(SqLite数据库存储)
- SQLite数据库存储数据
- SQLite数据库存储数据
- SQLite数据库存储数据
- SQLite数据库存储数据
- Android数据存储:数据库
- SQLite数据库存储数据
- SQLite数据库存储数据
- SQLite数据库存储数据
- SQLite数据库存储数据
- SQLite数据库存储数据
- iOS数据存储--数据库
- Sqlite数据库存储数据
- 数据库-数据存储引擎
- 数据存储(sharedPreferences,内部存储,外部存储,SQLite数据库,网络存储)
- Android 存储数据之3 使用SQLite数据库存储
- Android学习笔记八.LauncherActvity 之LauncherActivity、PreferenceActivity、PreferenceFragment
- java nio学习(五)
- 将win32程序移植到linux注意事项
- 12款程序员们最爱的Bootstrap模板
- linux sync命令到底做了什么
- 数据存储(3)---数据库存储
- 程序员生存定律-打造属于自己的稀缺性
- Android:java.lang.UnsatisfiedLinkError: Couldn't load BaiduMapSDK_v3_1_0 from loader
- Intersection of Two Linked Lists
- 使用 SCons 轻松建造程序
- iOS控件使用之UIImageView
- 20140916 实验
- androidの读取短信内容
- 浏览器横向滚动条自动居中