SQLite数据库学习笔记

     这两天学习了 SQLite数据库,做下学习笔记。SQLite数据库为嵌入型数据库。

1、抽象类SQLiteOpenHelper为数据库建立连接,其子类DBOpenHelper实现父类onCreate、和onUpgrade并调用父类的构造方法

/**
 * 
 */
package edu.fjnu.sqlite.service;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * @author Kysann
 *
 */
public class DBOpenHelper extends SQLiteOpenHelper {
	
    private static final String DB_NAME = "stumgr.db";
    private static final Integer DB_VERSION = 1;
    
	public DBOpenHelper(Context context) {
		
		super(context, DB_NAME, null, DB_VERSION);
		
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		
		db.execSQL("create table student(_id Integer primary key autoincrement, stu_name varchar(30) ,stu_pwd varchar(30))");
        
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub

	}

}

2、通过 DBOpenHelper  的 dbHelper.getWritableDatabase(); / dbHelper.getReadableDatabase();获取SQLiteDatabase实例进行对数据库的操作。


/**
 * 
 */
package edu.fjnu.sqlite.service;

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

import edu.fjnu.sqlite.domain.Student;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * @author Kysann
 *
 */
public class StudentService {
	
	private static final String SQL_INSERT = "insert into student values(null,?,?)";
	private static final String SQL_UPDATE = "update student set stu_name=? ,stu_pwd=? where _id=?";
	private static final String SQL_DELETE = "delete from student where _id=?";
	private static final String SQL_LOAD   = "select * from student limit ?,?";
	private static final String SQL_SELECT = "select * from student where _id=?";
	private static final String SQL_COUNT  = "select count(*) from student";
	
	private DBOpenHelper dbHelper;
	private SQLiteDatabase db;

	public StudentService(Context context) {
		
		this.dbHelper = new DBOpenHelper(context);
		
	}
	
	public void doInsert(Student stu){
		
		db = dbHelper.getWritableDatabase();
		db.execSQL(SQL_INSERT, new Object[]{stu.getStuName(),stu.getStuPwd()});
		
	}
	
	public void updateStu(Student stu){
		
		db = dbHelper.getWritableDatabase();
		db.execSQL(SQL_UPDATE, new Object[]{stu.getStuName(),stu.getStuPwd(),stu.getStuId()});
		
	}
	
	public void delStu(Integer id){
		
		db = dbHelper.getWritableDatabase();
		db.execSQL(SQL_DELETE, new Object[]{id});
		
	}
	
	public Student LoadById(Integer id){
		
		Student stu = new Student();
		db = dbHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery(SQL_SELECT, new String[]{id.toString()});
		if(cursor.moveToNext()){
			stu.setStuId(cursor.getInt(cursor.getColumnIndex("_id")));
			stu.setStuName(cursor.getString(cursor.getColumnIndex("stu_name")));
			stu.setStuPwd(cursor.getString(cursor.getColumnIndex("stu_pwd")));
		}
		return stu;
	}
	
	public List LoadAll(Integer start, Integer end){
		
		List stuList = new ArrayList();
		
		db = dbHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery(SQL_LOAD, new String[]{start.toString(),end.toString()});
		
		while(cursor.moveToNext()){
			
			Student stu = new Student();
			stu.setStuId(cursor.getInt(cursor.getColumnIndex("_id")));
			stu.setStuName(cursor.getString(cursor.getColumnIndex("stu_name")));
			stu.setStuPwd(cursor.getString(cursor.getColumnIndex("stu_pwd")));
			stuList.add(stu);
			
		}
		
		return stuList;
		
	}
	
	public Integer count(){
		
		Integer count = 0;
		db = dbHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery(SQL_COUNT, null);
		cursor.moveToFirst();
		count = cursor.getInt(0);
		return count;
		
	}
}

3、SQLite事务操作,通过db.beginTransaction();开启事务,db.setTransactionSuccessful();设置事务执行成功标志,最后再执行db.endTransaction();提交事务。

你可能感兴趣的:(Android)