package com.bill.studentMng;
import Java.util.ArrayList;
import java.util.List;
import Android.content.Context;
import android.database.Cursor;
import android.util.Log;
public class Student
{
public static final String TableName = "STUDENT";
public static final String DBName = "STUDENT";
private String Id;
private String Name;
private String Sex;
private String sClass;
private SQLiteAdapter SqlDB;
private Cursor cursor;
private List<Student> list = new ArrayList<Student>();
public Student(Context context, String id, String name, String sex, String sclass)
{
Id = id;
Name = name;
Sex = sex;
sClass = sclass;
SqlDB = new SQLiteAdapter(context, DBName,TableName, null, 1);
}
public Student(Context context)
{
SqlDB = new SQLiteAdapter(context, DBName,TableName, null, 1);
}
public void setDB(SQLiteAdapter db)
{
this.SqlDB = db;
}
public String getId()
{
return Id;
}
public Student setId(String id)
{
Id = id;
return this;
}
public String getName()
{
return Name;
}
public Student setName(String name)
{
Name = name;
return this;
}
public String getSex()
{
return Sex;
}
public Student setSex(String sex)
{
Sex = sex;
return this;
}
public String getsClass()
{
return sClass;
}
public Student setClass(String sclass)
{
sClass = sclass;
return this;
}
public long AddToDB()
{
Log.i("TAG","WRITE");
SqlDB.open("w");
long result = SqlDB.insert(Id, Name, Sex, sClass);
SqlDB.close();
return result;
}
public void RemoveFromDB()
{
SqlDB.open("w");
SqlDB.remove("ID",Id);
SqlDB.close();
}
public List<Student> Qurey(Context context)
{
Log.i("TAG","READ");
SqlDB.open("w");
cursor = SqlDB.select();
while(cursor.moveToNext())
{
Student student = new Student(context,cursor.getString(0),cursor.getString(1), cursor.getString(2), cursor.getString(3));
list.add(student);
}
SqlDB.close();
return list;
}
}
对于构造函数SQLiteOpenHelper(Context context,String name, SQLiteDatabase.CursorFactory factory, int version),官方是这样介绍的:
Create a helper object to create, open, and/or manage a database. This method always returns very quickly. The database is not actually created or opened until one ofgetWritableDatabase() or getReadableDatabase() is called.
再看看getWritableDatabase()的官方介绍:
Create and/or open a database that will beused for reading and writing. The first time this is called, the database will be opened andonCreate(SQLiteDatabase), onUpgrade(SQLiteDatabase, int, int) and/oronOpen(SQLiteDatabase) will be called.
Once opened successfully, the database is cached, so you can call this method every time you need to write to the database. (Make sure to callclose() when you no longer need the database.) Errors such as bad permissions or a full disk may cause this method to fail, but future attempts may succeed if the problem is fixed.
getReadableDatabase():
Create and/or open a database. This will be the same object returned bygetWritableDatabase() unless some problem, such as a full disk, requires the database to be opened read-only. In that case, a read-only database object will be returned. If the problem is fixed, a future call togetWritableDatabase() may succeed, in which case the read-only database object will be closed and the read/write object will be returned in the future.
综合构造函数SQLiteOpenHelper和getWritableDatabase的解释,可以看出SQLiteOpenHelper并不会正真创建一个数据库,只有当数据库不存在时且第一次调用getWritableDatabase或者getReadableDatabase时才会在外存上程序的数据区data/data/com.bill.studentMng/database下建立数据库,经我测试也确实如此。
还有一般不出意外的话getReadableDatabase()与 getWritableDatabase() 所返回的的数据库都可以进行读写操作,而不像其名字所说的,不过我一般还是在只进行读操作时用getReadableDatabase(),进行写操作时用getWritableDatabase()。
最后要注意的是要及时用close()关闭数据库,不过当你使用query进行查询时一定要先用获得的Cursor将所需要的数据都读出后再关闭数据库,不能获得Cursor后立刻关闭数据库,否则无法读取数据。
