您的位置:首页 > 移动开发 > Android开发

android-SQLiteOpenHelper又称本地库

2016-05-13 14:46 525 查看
SQliteOpenHelper是一个抽象类,需要继承该类。并且调用父类构造器如下代码

public SQLiteHelper (Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
}


其中name为数据库名称,factory,用于查询时返回Cursor的子类对象;或者传入null使用默认的factory构造,version为数据库的版本号

使用SQliteOpenHelper需要重写onCreate(SQLiteDatabase)的方法,也就是创建数据库表的方法,onCreate的方法中需要用到SQLiteDatabase这个累类,SQLiteDatabase.execSQL();来实现创建数据库表的方法,代码

public void onCreate(final SQLiteDatabase db) {
// System.out.println("创建数据表");
String sql = "create table ck_weizhi(x varchar,y varchar,time varchar,id varchar)";
db.execSQL(sql)
}


还要重写onUpgrade的方法,打开数据库时传入的版本号与当前的版本号不同时会调用该方法。

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}


接下来需要进行插入,查询,删除,的方法来进行数据的操作,其中用到了getWritableDatabase() 方法以读写方式打开数据库,一旦数据库的磁盘空间满了,数据库就只能读而不能写,倘若使用的是getWritableDatabase() 方法就会出错。

getReadableDatabase()方法则是先以读写方式打开数据库,如果数据库的磁盘空间满了,就会打开失败,当打开失败后会继续尝试以只读方式打开数据库。如果该问题成功解决,则只读数据库对象就会关闭,然后返回一个可读写的数据库对象。

下面贴出一个例子作为工具类来使用

package com.example.sqgl.util;

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

import com.example.popupwindow.Wezhi;
import com.example.sqgl.R.id;

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

public class SQLiteHelper extends SQLiteOpenHelper {

private final static String DB_NAME = "ck_db";

public SQLiteHelper(Context context) {
this(context, DB_NAME, null,3);
// System.out.println("创建数据库");

}

public SQLiteHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}

public SQLiteHelper(Context context, String string, int i) {
// TODO Auto-generated constructor stub
this(context, DB_NAME, null, 3);

}

@Override
public void onCreate(final SQLiteDatabase db) {
// System.out.println("创建数据表");
String sql = "create table ck_weizhi(x varchar,y varchar,time varchar,id varchar)";
db.execSQL(sql);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { }

/*
* 插入位置
*/
public static int Insertweizhi(SQLiteHelper msh, String x,
String y,String time,String id) {
SQLiteDatabase db = msh.getWritableDatabase();
db.beginTransaction();
// db.execSQL("Delete from ck_order_ip");

String sql = "insert into ck_weizhi(x,y,time,id) values ('"
+ x
+ "','"
+ y+ "','"+time+ "','"+id
+ "')";
// System.out.println("插入Ip---" + sql);
db.execSQL(sql);

db.setTransactionSuccessful();
db.endTransaction();
return 0;

}

/*
* 查询位置
*/
public static List<Wezhi> getweizhi(SQLiteHelper msh) {

List<Wezhi> list_weizhi = new ArrayList<Wezhi>();
SQLiteDatabase db = msh.getWritableDatabase();
Cursor cursor = db.rawQuery(
"select x, y,time,id from ck_weizhi order by time asc",null);
if (cursor.moveToFirst()) {
do {
Wezhi return_weizhi = new Wezhi();
return_weizhi.setX(cursor.getString(0));
return_weizhi.setY(cursor.getString(1));
return_weizhi.setTime(cursor.getString(2));
return_weizhi.setId(cursor.getString(3));

list_weizhi.add(return_weizhi);

} while (cursor.moveToNext());
}
cursor.close();
return list_weizhi;
}

/*
* 删除位置
*/
public static void deleteweizhi(SQLiteHelper msh) {
SQLiteDatabase db = msh.getWritableDatabase();
db.beginTransaction();
db.execSQL("Delete from ck_weizhi ");
db.setTransactionSuccessful();
db.endTransaction();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android