新建一个类继承接口类SQLiteOpenHelper,并实现其中的onCreate与onUpgrade方法
注意:
1、execSQL一次只能执行一种sql语句,不支持多表创建和插入等操作,建议将数据库创建语句分开写入,如下
2、数据库存在时不会执行,如果需修改数据库,建议清除手机端应用数据再重新打开应用,会重新运行onCreate;或者使用sql语句进行修改
public class SqlLite extends SQLiteOpenHelper {
private static final String db_name = "SQLite_db.db";//数据文件的名字
private static int NUMBER = 1;//当前数据库版本,用于升级
private static final String table_name = "students";//表名
private static String sql = null;//sql语句
public SqlLite(@Nullable Context context) {
super(context, db_name, null, NUMBER);
}
/**
*当数据库第一次创建时调用onCreate方法,并执行其中的sql语句
*注意:数据库存在时不会执行,如果需修改数据库,建议清除手机端应用数据再重新打开应用,会重新运行onCreate;
或者使用sql语句进行修改
*/
@Override
public void onCreate(SQLiteDatabase db) {
//创建表
sql = "CREATE TABLE ClassList ( \n" +
"CLid INTEGER PRIMARY KEY ,\n" +
"classname VARCHAR(50) NOT NULL ,\n" +
"weeksum VARCHAR(50) NOT NULL ,\n" +
"classroom VARCHAR(50) NOT NULL ,\n" +
"DayTime VARCHAR(50) NOT NULL\n" +
");";
db.execSQL(sql);
sql = "CREATE TABLE Curriculum ( \n"+
"DTid INTEGER PRIMARY KEY ,\n"+
"WeekTime1 INT,);";
db.execSQL(sql);
sql = "insert into ClassList(CLid,classname,weeksum,classroom,DayTime)\n"+
"values(1,'专业英语','1-12','多媒体101','8:00-9:40')";
db.execSQL(sql);
}
/**
* 当数据库进行升级是调用,这里通过NUMBER值来进行判断,数据库是否升级
* 可不写
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
例:
public class ClassList {
private int CLid;
private String ClassCame;
private String WeekSum;
private String ClassRoom;
private String DayTime;
public ClassList() {
}
public ClassList(int CLid, String classCame, String weekSum, String classRoom, String dayTime) {
this.CLid = CLid;
ClassCame = classCame;
WeekSum = weekSum;
ClassRoom = classRoom;
DayTime = dayTime;
}
public int getCLid() {
return CLid;
}
public void setCLid(int CLid) {
this.CLid = CLid;
}
public String getClassCame() {
return ClassCame;
}
public void setClassCame(String classCame) {
ClassCame = classCame;
}
public String getWeekSum() {
return WeekSum;
}
public void setWeekSum(String weekSum) {
WeekSum = weekSum;
}
public String getClassRoom() {
return ClassRoom;
}
public void setClassRoom(String classRoom) {
ClassRoom = classRoom;
}
public String getDayTime() {
return DayTime;
}
public void setDayTime(String dayTime) {
DayTime = dayTime;
}
@Override
public String toString() {
return "ClassList{" +
"CLid=" + CLid +
", ClassCame='" + ClassCame + '\'' +
", WeekSum='" + WeekSum + '\'' +
", ClassRoom='" + ClassRoom + '\'' +
", DayTime='" + DayTime + '\'' +
'}';
}
}
SQLiteDatabase的execSQL方法可执行任意SQL语句,包括带占位符的SQL语句。但由于该方法没有返回值,因此一般用于执行DDL(data definition language)语句或DML(data manipulation language)语句;如果需要执行査询语句,则可调用SQLiteDatabase的rawQuery(String sql, String[] selectionArgs)方法
例:增删改查:
public class tab_service {
private static final String TAG = "tab_service";
private SqlLite sqllite = null;//数据库对象
private static SQLiteDatabase db = null; //数据库类
private static String sql = null;
public tab_service(Context context) {
this.sqllite = new SqlLite(context);//获得数据库操作实例
}
//查询所有数据
public List<ClassList> selectClassList(){
List<ClassList> ClassList=new ArrayList<ClassList>();
db=sqllite.getReadableDatabase();
sql="select * from ClassList ";
Cursor cur = db.rawQuery(sql, null);
while (cur.moveToNext()) {
int CLid = cur.getInt(cur.getColumnIndex("CLid"));
String classname = cur.getString(cur.getColumnIndex("classname"));
String WeekSum = cur.getString(cur.getColumnIndex("weeksum"));
String classroom = cur.getString(cur.getColumnIndex("classroom"));
String DayTime = cur.getString(cur.getColumnIndex("DayTime"));
ClassList stu = new ClassList(CLid,classname,WeekSum,classroom,DayTime);
ClassList.add(stu);
}
cur.close();
db.close();
return ClassList;
}
}
例:
使用了工厂方法创建DAO对象
public class ServiceFactory {
public static tab_service Service(Context context){
return new tab_service(context);
}
}
在activity中获取数据
List <ClassList> classList=ServiceFactory.Service(getApplicationContext()).selectClassList();
Iterator<ClassList> iterator = classList.iterator();
while(iterator.hasNext()){
ClassList cl=iterator.next();
Log.d(TAG, "onCreate: ClassList:"+cl.toString());
}