新聞中心
這篇文章給大家介紹如何正確的把數(shù)據(jù)插入到數(shù)據(jù)庫中,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

把數(shù)據(jù)放入數(shù)據(jù)庫
通過把ContentValues對象傳入instert()方法把數(shù)據(jù)插入數(shù)據(jù)庫:
// 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()方法的第一個參數(shù)是表名。第二個參數(shù)提供了框架中的一個列名,在ContentValues的值是空的時候,框架會向表中插入NULL值(如果這個參數(shù)是“null”,那么當(dāng)沒有值時,框架不會向表中插入一行。
從數(shù)據(jù)庫中讀取數(shù)據(jù)
要從數(shù)據(jù)庫中讀取數(shù)據(jù),就要使用query()方法,你需要給這個方法傳入選擇條件和你想要獲取數(shù)據(jù)的列。查詢結(jié)果會在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對象的移動方法來查看游標(biāo)中的一行數(shù)據(jù),在開始讀取數(shù)據(jù)之前必須先調(diào)用這個方法。通常,應(yīng)該從調(diào)用moveToFirst()方法開始,它會把讀取數(shù)據(jù)的位置放到結(jié)果集中第一實(shí)體。對于每一行,你可以通過調(diào)用Cursor對象的相應(yīng)的get方法來讀取列的值,如果getString()或getLong()方法。對于每個get方法,你必須把你希望的列的索引位置傳遞給它,你可以通過調(diào)用getColumnIndex()或getColumnIndexOrThrow()方法來獲取列的索引。例如:
cursor.moveToFirst();
long itemId = cursor.getLong(
cursor.getColumnIndexOrThrow(FeedReaderContract.FeedEntry._ID)
);
從數(shù)據(jù)庫中刪除數(shù)據(jù)
要從一個表中刪除行數(shù)據(jù),你需要提供標(biāo)識行的選擇條件。數(shù)據(jù)API為創(chuàng)建選擇條件提供了一種機(jī)制,它會防止SQL注入。這中機(jī)制把選擇條件分成了選擇條件和選擇參數(shù)。條件子句定義了要查看的列,并且還允許你使用組合列來進(jìn)行篩選。參數(shù)是用于跟條件綁定的、用戶篩選數(shù)據(jù)的值。因?yàn)檫@樣不會導(dǎo)致像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);
更新數(shù)據(jù)庫
當(dāng)你需要編輯數(shù)據(jù)庫值的時候,請使用update()方法。
這個方法在更新數(shù)據(jù)時會把insert()方法中內(nèi)容值的語法跟delete()方法中的where語法結(jié)合在一起。
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);
關(guān)于如何正確的把數(shù)據(jù)插入到數(shù)據(jù)庫中就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
本文題目:如何正確的把數(shù)據(jù)插入到數(shù)據(jù)庫中-創(chuàng)新互聯(lián)
本文來源:http://www.dlmjj.cn/article/dijepj.html


咨詢
建站咨詢
