TAILIEUCHUNG - Android SDK (phần 6)

Working with SQLite Databases ❘ 217 LISTING 7-5: Extracting values from a Cursor int GOLD_HOARDED_COLUMN = 2; Cursor myGold = ("GoldHoards", null, null, null, null, null, null); float totalHoard = 0f; // Make sure there is at least one row. if (()) { // Iterate over each cursor. do { float hoard = (GOLD_HOARDED_COLUMN); totalHoard += hoard; } while(()); } float averageHoard = totalHoard / (); Because SQLite database columns are loosely typed, you can cast individual values into valid types as required. For example, values stored as floats can be read back as strings. Adding, Updating, and Removing Rows The SQLiteDatabase class exposes insert, delete,. | Working with SQLite Databases 217 LISTING 7-5 Extracting values from a Cursor Available for download on int gold_hoarded_column 2 Cursor myGold GoldHoards null null null null null null float totalHoard 0f Make sure there is at least one row. if Iterate over each cursor. do float hoard GOLD_HOARDED_COLUMN totalHoard hoard while float averageHoard totalHoard Because SQLite database columns are loosely typed you can cast individual values into valid types as required. For example values stored as floats can be read back as strings. Adding Updating and Removing Rows The SQLiteDatabase class exposes insert delete and update methods that encapsulate the SQL statements required to perform these actions. Additionally the execSQL method lets you execute any valid SQL on your database tables should you want to execute these or any other operations manually. Any time you modify the underlying database values you should call refreshQuery on each Cursor that has a view on the affected table. Inserting New Rows To create a new row construct a Contentvalues object and use its put methods to provide a value for each column. Insert the new row by passing the Content Values object into the insert method called on the target database along with the table name as shown in Listing 7-6. LISTING 7-6 Inserting new rows into a database Available for _ . download on Create a new row of values to insert. Contentvalues newValues new ContentValues Assign values for each row. COLUMN_NAME newValue . Repeat for each column . Insert the row into your table DATABASE_TABLE null newValues 218 CHAPTER 7 DATABASES AND CONTENT PROVIDERS Updating a Row Updating rows is also done with Content Values. Create a new Contentvalues object using the put methods to assign new values to each column you want to update. Call update on the database passing in the table name the .

TỪ KHÓA LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.