sqlite simple demo

数据库操作类:
package com.amaker.sqlite;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
/**
 * 数据库操作Demo
 * @author zzl
 *
 */
public class MyDbAdapter {
	//创建数据库
	public static final String DB_NAME = "per.db";
	//创建表
	public static final String TABLE_NAME = "pertbl";
	//表的一些字段
	public static final String ID = "_id";
	public static final String NAME = "name";
	public static final String AGE = "age";
	//数据库版本号
	public static final int VERSION = 1;
	private Context context;

	private SQLiteDatabase db;
	private MyDbHelper helper;
	//无参构造方法
	public MyDbAdapter(){
		
	}
	//需要传入上下文的构造方法
	public MyDbAdapter(Context context) {
		this.context = context;
	}
	
	class MyDbHelper extends SQLiteOpenHelper {

		public MyDbHelper() {
			super(context, DB_NAME, null, VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			String sql = "create table PerTbl(_id integer" +
					" primary key autoincrement, name text,age integer) ";
			db.execSQL(sql);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL("drop table if exists PerTbl");
			onCreate(db);
		}
	}
	public MyDbHelper open(){
		helper = new MyDbHelper();
		db = helper.getWritableDatabase();
		return helper;
	}
	
	public void close(){
		if(helper!=null){
			helper.close();
		}
	}
	
	/**
	 * 根据sql语句查询
	 * @param sql 
	 * @return
	 */
	public Cursor getCursorBysql(String sql){
		 Log.e("select", "getCursorBysql==========dbs");
		return db.rawQuery(sql, null);
		
	}
	
	/**
	 * 添加
	 * @param table
	 * @param contvls
	 * @return
	 */
    public long insert(String table,ContentValues contvls){
        long row=db.insert(table, null, contvls);
        return row;
    }
    
    /**
     * 更新
     * @param id
     * @param table
     * @param contvls
     */
    public int update(String table,ContentValues contvls,String where,String[] whereValue){
        int result=db.update(table, contvls, where, whereValue);
        return result;
    }
    /**
     * 根据_Id删除
     * @param table
     */
    public void delete(int id){
    	String[] args = {String.valueOf(id)};
		db.delete(TABLE_NAME, "_id=?", args);
    }
	
}


Activity中的调用:
package com.amaker.sqlite;

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

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
    private MyDbAdapter dbAdapter;
    private Button btn_add;
    private Button btn_delete;
    private Button btn_update;
    private Button btn_find;
    private TextView tv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        init();
        dbAdapter = new MyDbAdapter(this);
        dbAdapter.open();
        
    }
    
    void init(){
    	tv = (TextView) findViewById(R.id.tv);
    	btn_add = (Button) findViewById(R.id.button1);
    	btn_delete = (Button) findViewById(R.id.button2);
    	btn_update = (Button) findViewById(R.id.button3);
    	btn_find = (Button) findViewById(R.id.button4);
    	
    	btn_add.setOnClickListener(this);
    	btn_delete.setOnClickListener(this);
    	btn_update.setOnClickListener(this);
    	btn_find.setOnClickListener(this);
    }
    /**
     * 添加按钮点击事件
     */
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		//添加按鈕
		case R.id.button1:
			add();
			Toast.makeText(getApplicationContext(), "添加成功!", Toast.LENGTH_LONG).show();
			break;
		//刪除按鈕
		case R.id.button2:
			delete();
			break;
		//修改按鈕
		case R.id.button3:
			update();
			break;
		//查找按鈕
		case R.id.button4:
			query();
			break;

		default:
			break;
		}
		
	}
	//数据库的添加方法:
	public void add(){
		ContentValues cv = new ContentValues();
		cv.put("name", "zzl");
		cv.put("age", 25);
		dbAdapter.insert(MyDbAdapter.TABLE_NAME, cv);
	}
	//数据库更新方法:
	public void update(){
		try {
			ContentValues cv = new ContentValues();	
			cv.put("name", "li_cai_na");
			String[] values ={"1"};
			dbAdapter.update(MyDbAdapter.TABLE_NAME, cv, "_id=?", values);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//数据库查找方法:
	public void query(){
		List<String> list_name = new ArrayList<String>();
		try {
			
			String sql = "select name from pertbl";
			Cursor myCursor = dbAdapter.getCursorBysql(sql);
			while (myCursor.moveToNext()) {
				String str = myCursor.getString(0);
				list_name.add(str);
			}
			myCursor.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(list_name.size()>0){
			tv.setText(list_name.get(0));
			Iterator<String> iterator = list_name.iterator();
			while(iterator.hasNext()){
				System.out.println(iterator.next()+"、");
			}
		}
	}
	//数据库删除方法:
	public void delete(){
		try {
			dbAdapter.delete(3);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


未封装,只是简单学习下,因好久没看代码,都忘了,最近因要用到数据库就看下。

你可能感兴趣的:(sqlite)