Android——另外一種增刪查改的方式(ContentProvider常用)

來源:互聯網
上載者:User

以下介紹另外一種增刪查改的方式

package com.njupt.sqllist;import java.util.ArrayList;import java.util.List;import android.content.ContentValues;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;public class PersonDao {DBOpenHelper helper;public PersonDao(Context context) {helper = new DBOpenHelper(context);}public void insert(Person p) {SQLiteDatabase db = helper.getWritableDatabase();ContentValues values = new ContentValues();values.put("name", p.getName());values.put("balance", p.getBalance());long row = db.insert("person", null, values);System.out.println("在第"+row+"插入");db.close();}public void delete(int id) {SQLiteDatabase db = helper.getWritableDatabase();int rows = db.delete("person", "id = ? ", new String[]{ id + ""});System.out.println("刪除了"+rows+"行");db.close();}public void update(Person p) {SQLiteDatabase db = helper.getWritableDatabase();ContentValues values = new ContentValues();values.put("name", p.getName());values.put("balance", p.getBalance());int rows = db.update("person", values,"id = ?",new String[]{p.getId() + ""});System.out.println("更新了"+rows+"行");db.close();}public Person query(int id) {SQLiteDatabase db = helper.getReadableDatabase();Cursor c = db.query("person", new String[]{"name,balance"}, "id = ? ", new String[]{id + ""}, null, null, null);Person p = null;if (c.moveToNext()) {String name = c.getString(c.getColumnIndex("name"));int balance = c.getInt(1);p = new Person(id, name, balance);}System.out.println("這是一種另一種方式進行查詢");return p;}public List<Person> queryAll() {SQLiteDatabase db = helper.getReadableDatabase();Cursor c = db.query("person", null, null, null, null, null, null);List<Person> persons = new ArrayList<Person>();while (c.moveToNext()) {Person p = new Person(c.getInt(0), c.getString(1), c.getInt(2));persons.add(p);}System.out.println("這時再用另外一種方式查詢所有");return persons;}public void remit(int from, int to, int count) {SQLiteDatabase db = helper.getWritableDatabase();try {db.beginTransaction();db.execSQL("update person set balance = balance - ? where id = ?",new Object[] { count, from });System.out.println(1 / 0);db.execSQL("update person set balance = balance + ? where id = ?",new Object[] { count, to });db.setTransactionSuccessful();} finally {db.endTransaction();db.close();}}public int queryCount() {SQLiteDatabase db = helper.getReadableDatabase();Cursor c = db.query("person", new String[]{"count(*)"}, null, null,null, null, null);c.moveToNext();int count = c.getInt(0);System.out.println("這時再用另外一種方式查詢總數");return count;}public List<Person> queryPage(int pageNum, int pageSize) {SQLiteDatabase db = helper.getReadableDatabase();Cursor c = db.query("person",null, null, null,null,null,null, (pageNum - 1) * pageSize + " , " + pageSize);List<Person> persons = new ArrayList<Person>();while (c.moveToNext()) {Person p = new Person(c.getInt(0), c.getString(1), c.getInt(2));persons.add(p);}System.out.println("這時再用另外一種方式在分頁");return persons;}}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.