Android—把資料儲存到資料庫中(二)

來源:互聯網
上載者:User

把資料放入資料庫

通過把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);

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.