日期:2014-05-16  浏览次数:20500 次

SQLite数据库操作详细示例

 

MainActivity如下:

package cc.testdb;

import java.util.List;
import cc.database.DBUtils;
import cc.domain.Person;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.app.Activity;
/**
 * Demo描述:
 * SQLite数据操作
 * 
 * 参考资料:
 * http://blog.csdn.net/lfdfhl/article/details/8195378
 * 
 */
public class MainActivity extends Activity {
    private DBUtils mDBUtils;
    private Button mAddButton;
    private Button mQueryButton;
    private Button mUpdateButton;
    private Button mDeleteButton;
    private Button mCountButton;
    private Button mPageButton;
    private Button mTransactionButton;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				             WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.main);
		init();
	}
	
	private void init(){
		
		mAddButton=(Button) findViewById(R.id.addButton);
		mAddButton.setOnClickListener(new ClickListenerImpl());
		
		mQueryButton=(Button) findViewById(R.id.queryButton);
		mQueryButton.setOnClickListener(new ClickListenerImpl());
		
		mUpdateButton=(Button) findViewById(R.id.updateButton);
		mUpdateButton.setOnClickListener(new ClickListenerImpl());
		
		mDeleteButton=(Button) findViewById(R.id.deleteButton);
		mDeleteButton.setOnClickListener(new ClickListenerImpl());
		
		mCountButton=(Button) findViewById(R.id.countButton);
		mCountButton.setOnClickListener(new ClickListenerImpl());
		
		mPageButton=(Button) findViewById(R.id.pageButton);
		mPageButton.setOnClickListener(new ClickListenerImpl());
		
		mTransactionButton=(Button) findViewById(R.id.transactionButton);
		mTransactionButton.setOnClickListener(new ClickListenerImpl());
		
	}
	
	//注意:
	//每次都生成新的mDBUtils,
	//因为每次数据库操作都需要一个新的openHelper
	//参见DBUtils类构造方法便知
	private class ClickListenerImpl implements OnClickListener {
		Person person=null;
		@Override
		public void onClick(View v) {
			mDBUtils=new DBUtils(MainActivity.this);
			switch (v.getId()) {
			case R.id.addButton:
				for (int i = 0; i < 15; i++) {
					person=new Person("xiaoming"+i, "9527"+i);
					mDBUtils.add(person);
				}
				break;
			case R.id.queryButton:
				person=mDBUtils.query(5);
				System.out.println(person);
				break;
			case R.id.updateButton:
				person=mDBUtils.query(1);
				System.out.println("修改前:"+person);
				person=new Person(1, "xx", "1234");
				mDBUtils.update(person);
				person=mDBUtils.query(1);
				System.out.println("修改后:"+person);
				break;
			case R.id.deleteButton:
				mDBUtils.delete(2);
				break;
			case R.id.countButton:
				int count=mDBUtils.count();
				System.out.println("数据总量为:"+count);
				break;
			case R.id.pageButton:
				List<Person> list=mDBUtils.page(4, 9);
				for (int i = 0; i < list.size(); i++) {
					 person=list.get(i);
					 System.out.println("分页的数据:"+person);
				}
				break;
			case R.id.transactionButton:
				person=new Person(1, "ccc", "8888");
				mDBUtils.transaction(person);
				person=mDBUtils.query(1);
				System.out.println("事务操作后:"+person);
				break;
			default:
				break;
			}
		}

	}
}


DBUtils如下:

package cc.database;
import java.util.ArrayList;
import java.util.List;
import cc.domain.Person;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DBUtils {

	private DataBaseOpenHelper openHelper;

	public DBUtils(Context co