1、创建一个类继承SQLiteOpenHelper
创建数据库需要使用的api:SQLiteOpenHelper
* 必须定义一个构造方法:
package com.subzero.dictionary.db;
import com.subzero.dictionary.config.Constant;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* 创建数据库和表
*/
public class DBOpenHelper extends SQLiteOpenHelper {
/**
* 调用父类构造器
* context 上下文对象
* dbName 数据库的名称
* factory CursorFactory类型
* version 数据库版本
*/
public DBOpenHelper(Context context,String dbName,CursorFactory factory, int version) {
super(context, dbName, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
// 创建数据库时调用
db.execSQL("create table course (_id integer primary key autoincrement, name varchar(20))");
}
//当传入的数据库版本号与当前的版本号不同时会调用该方法
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
2、通过CourseDao 操作数据库
getWritableDatabase():打开可读写的数据库
getReadableDatabase():在磁盘空间不足时打开只读数据库,否则打开可读写数据库
package com.subzero.dictionary.db;
import java.util.ArrayList;
import java.util.List;
import com.subzero.dictionary.bean.CourseBean;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
/**
* 对数据库进行增删改查操作
*/
public class CourseDao {
private DBOpenHelper dbOpenHelper;
public CourseDao(Context context) {
this.dbOpenHelper = new DBOpenHelper(context, "my.db", null, 1);
}
/**
* 添加课程信息
*
* @param courseBean
*/
public void insert(CourseBean courseBean) {
//获取数据库操作对象
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
db.execSQL(
"insert into course(id,name,introduction,price) values(?,?,?,?)",
new Object[] { courseBean.getId(), courseBean.getName(),
courseBean.getIntroduction(), courseBean.getPrice() });
db.close();
}
/**
* 获取表中的所有记录
*/
public List<CourseBean> findAll() {
List<CourseBean> listCourseBean = new ArrayList<CourseBean>();
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
//获取数据集合
Cursor cursor = db.rawQuery("select * from course", null);
//遍历集合 moveToNext移动到下一行
while (cursor.moveToNext()) {
//从Cursor中获得数据
int id = cursor.getInt(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String introduction = cursor.getString(cursor
.getColumnIndex("introduction"));
int price = cursor.getInt(cursor.getColumnIndex("price"));
listCourseBean.add(new CourseBean(id, name, introduction, price));
}
cursor.close();
db.close();
return listCourseBean;
}
/**
* 查找是否存在该课程ID
*/
public CourseBean findId(int id) {
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
CourseBean bean=null;
Cursor cursor = db.rawQuery("select * from course where id=?",
new String[] { id + "" });
if (cursor.moveToFirst()) {
String name = cursor.getString(cursor.getColumnIndex("name"));
String introduction = cursor.getString(cursor
.getColumnIndex("introduction"));
int price = cursor.getInt(cursor.getColumnIndex("price"));
bean= new CourseBean(id, name, introduction, price);
}
cursor.close();
db.close();
return bean;
}
/**
* 获取记录总数
*
* @return 记录总数
*/
public long getCount() {
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
Cursor cursor = db.rawQuery("select count(*) from course", null);
cursor.moveToFirst();
long result = cursor.getLong(0);
cursor.close();
db.close();
return result;
}
}
3、事务
开启事务可以提高数据库的读写速度。
try {
//开启事务
db.beginTransaction();
...........
//设置事务执行成功
db.setTransactionSuccessful();
} finally{
//关闭事务
//如果此时已经设置事务执行成功,则sql语句生效,否则不生效
db.endTransaction();
}