共計 3099 個字符,預計需要花費 8 分鐘才能閱讀完成。
這篇文章給大家介紹如何正確的把數據插入到數據庫中,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
把數據放入數據庫
通過把 ContentValues 對象傳入 instert() 方法把數據插入數據庫:
// Gets the data repository in write mode
SQLiteDatabase db = mDbHelper.getWritableDatabase();
// Create a new map of values, where column names are the keys
ContentValues values = new ContentValues();
values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID, id);
values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE, title);
values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_CONTENT, content);
// Insert the new row, returning the primary key value of the new row
long newRowId;
newRowId = db.insert(
FeedReaderContract.FeedEntry.TABLE_NAME,
FeedReaderContract.FeedEntry.COLUMN_NAME_NULLABLE,
values);
insert() 方法的第一個參數是表名。第二個參數提供了框架中的一個列名,在 ContentValues 的值是空的時候,框架會向表中插入 NULL 值(如果這個參數是“null”,那么當沒有值時,框架不會向表中插入一行。
從數據庫中讀取數據
要從數據庫中讀取數據,就要使用 query() 方法,你需要給這個方法傳入選擇條件和你想要獲取數據的列。查詢結果會在 Cursor 對象中被返回。
SQLiteDatabase db = mDbHelper.getReadableDatabase();
// Define a projection that specifies which columns from the database
// you will actually use after this query.
String[] projection = {
FeedReaderContract.FeedEntry._ID,
FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE,
FeedReaderContract.FeedEntry.COLUMN_NAME_UPDATED,
…
};
// How you want the results sorted in the resulting Cursor
String sortOrder =
FeedReaderContract.FeedEntry.COLUMN_NAME_UPDATED + DESC
Cursor c = db.query(
FeedReaderContract.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 對象的移動方法來查看游標中的一行數據,在開始讀取數據之前必須先調用這個方法。通常,應該從調用 moveToFirst() 方法開始,它會把讀取數據的位置放到結果集中第一實體。對于每一行,你可以通過調用 Cursor 對象的相應的 get 方法來讀取列的值,如果 getString() 或 getLong() 方法。對于每個 get 方法,你必須把你希望的列的索引位置傳遞給它,你可以通過調用 getColumnIndex() 或 getColumnIndexOrThrow() 方法來獲取列的索引。例如:
cursor.moveToFirst();
long itemId = cursor.getLong(
cursor.getColumnIndexOrThrow(FeedReaderContract.FeedEntry._ID)
);
從數據庫中刪除數據
要從一個表中刪除行數據,你需要提供標識行的選擇條件。數據 API 為創建選擇條件提供了一種機制,它會防止 SQL 注入。這中機制把選擇條件分成了選擇條件和選擇參數。條件子句定義了要查看的列,并且還允許你使用組合列來進行篩選。參數是用于跟條件綁定的、用戶篩選數據的值。因為這樣不會導致像 SQL 語句一樣的處理,所以它避免了 SQL 注入。
// Define where part of query.
String selection = FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID + LIKE ?
// Specify arguments in placeholder order.
String[] selelectionArgs = { String.valueOf(rowId) };
// Issue SQL statement.
db.delete(table_name, selection, selectionArgs);
更新數據庫
當你需要編輯數據庫值的時候,請使用 update() 方法。
這個方法在更新數據時會把 insert() 方法中內容值的語法跟 delete() 方法中的 where 語法結合在一起。
SQLiteDatabase db = mDbHelper.getReadableDatabase();
// New value for one column
ContentValues values = new ContentValues();
values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE, title);
// Which row to update, based on the ID
String selection = FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID + LIKE ?
String[] selelectionArgs = { String.valueOf(rowId) };
int count = db.update(
FeedReaderDbHelper.FeedEntry.TABLE_NAME,
values,
selection,
selectionArgs);
關于如何正確的把數據插入到數據庫中就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。