×SQLiteOpenHelper
*SQLiteDataBase
*SQLiteQueryBuilder
1、SQLiteOpenHelper
引用包:android.database.sqlite.SQLiteOpenHelper
作用:创建数据库、数据库版本控制 opening the database if it exists, creating it if it does not, and upgrading it as necessary
用法:创建继承于SQLiteOpenHelper的一个子类,实现三个方法:
onCreate(SQLiteDatabase)
:数据库第一次生成时自动调用,一般写创建数据库的语句
onUpgrade(SQLiteDatabase, int, int)
: 数据库需要升级时自动调用,一般写删除原有数据表,创建新数据表语句
onOpen(SQLiteDatabase)
: 可选,当数据库打开时调用
例:
public class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
super(context, MyDB.DBNAME, null, MyDB.VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table "+MyDB.TNAME+"(" +
MyDB.TID+" integer primary key autoincrement not null,"+
MyDB.EMAIL+" text not null," +
MyDB.USERNAME+" text not null," +
MyDB.DATE+" interger not null,"+
MyDB.SEX+" text not null);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
MyDB 是一个数据库工具类,用于定义数据库名,表名等静态量
public class MyDB { public static final String DBNAME = "ruixinonlinedb"; //数据库名 public static final String TNAME = "ruixinonline"; //表名 public static final int VERSION = 3; //数据库版本号 public static String TID = "tid"; public static final String EMAIL = "email"; public static final String USERNAME = "username"; public static final String DATE = "date"; public static final String SEX = "sex"; public static final String AUTOHORITY = "com.MyDB.login"; public static final int ITEM = 1; public static final int ITEM_ID = 2; public static final Uri CONTENT_URI = Uri.parse("content://" + AUTOHORITY + "/" + TNAME); }