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

Android 数据库SQLite的使用简单Demo

?? 在手机开发中SQLite的使用demo

简单的bean类

package com.easyway.android.sql;
/**
 * 普通JavaBean
 * @author longgangbai
 *
 */
public class CityBean {
	public static final String ID = "_id";
	public static final String CITY = "city";
	public static final String CODE = "code";
	
	private String id;
	private String city;
	private String code;
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}

}

?

?

界面展现类:

package com.easyway.android.sql;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 
 * 
 * 本例实现SQLite数据库增加、删除、修改、模糊查询操作。这里不是最好的实现方法,
 * 如想研究SQL如何封装,请详细查看SQLiteDatebase类.
 * 查看SQL语句:String sql = SQLiteQueryBuilder.buildQueryString();
 * 
 * 
 * @author  longgangbai
 */
public class AndroidSQL extends Activity {
	private static String DB_NAME = "mycity.db";
	private static int DB_VERSION = 1;
	private static int POSTION;
	private ListView listview;
	private Cursor cursor;
	private SQLiteDatabase db;
	private SQLiteHelper dbHelper;
	private ListAdapter listAdapter;
	
	private EditText etCity;
	private EditText etCode;
	private Button bt_add;
	private Button bt_modify;
	private Button bt_query;
	
	private List<CityBean> cityList = new ArrayList<CityBean>();
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.main);
    	
    	etCity = (EditText) findViewById(R.id.etCity);
    	etCode = (EditText) findViewById(R.id.etCode);
    	bt_add = (Button) findViewById(R.id.bt_add);
    	bt_modify = (Button) findViewById(R.id.bt_modify);
    	bt_query = (Button) findViewById(R.id.bt_query);
    	
    	try{
    		/* 初始化并创建数据库 */
    		dbHelper = new SQLiteHelper(this, DB_NAME, null, DB_VERSION);
    		/* 创建表 */
    		db = dbHelper.getWritableDatabase();	//调用SQLiteHelper.OnCreate()        	
        	/* 查询表,得到cursor对象 */
        	cursor = db.query(SQLiteHelper.TB_NAME, null, null, null, null, null, CityBean.CODE + " DESC");
        	cursor.moveToFirst();
        	while(!cursor.isAfterLast() && (cursor.getString(1) != null)){    
        		CityBean city = new CityBean();
        		city.setId(cursor.getString(0));
        		city.setCity(cursor.getString(1));
        		city.setCode(cursor.getString(2));
        		cityList.add(city);
        		cursor.moveToNext();
        	}
    	}catch(IllegalArgumentException e){
    		//当用SimpleCursorAdapter装载数据时,表ID列必须是_id,否则报错column '_id' does not exist
    		e.printStackTrace();
    		//当版本变更时会调用SQLiteHelper.onUpgrade()方法重建表 注:表以前数据将丢失
    		++ DB_VERSION;
    		dbHelper.onUpgrade(db, --DB_VERSION, DB_VERSION);
//    		dbHelper.updateColumn(db, SQLiteHelper.ID, "_"+SQLiteHelper.ID, "integer");
    	}
    	listview = (ListView)findViewById(R.id.listView);
    	listAdapter = new ListAdapter();
    	listview.setAdapter(listAdapter);
    	listview.setOnItemClickListener(new ListView.OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int postion,
					long arg3) {
				setSelectedValues(postion);
			}    		
    	});
    	
    	/* 插入表数据并ListView显示更新 */
    	bt_add.setOnClickListener(new Button.OnClickListener(){