下面是Cursor对象的常用方法:
c.move(int offset); //以当前位置为参考,移动到指定行
c.moveToFirst(); //移动到第一行
c.moveToLast(); //移动到最后一行
c.moveToPosition(int position); //移动到指定行
c.moveToPrevious(); //移动到前一行
c.moveToNext(); //移动到下一行
c.isFirst(); //是否指向第一条
c.isLast(); //是否指向最后一条
c.isBeforeFirst(); //是否指向第一条之前
c.isAfterLast(); //是否指向最后一条之后
c.isNull(int columnIndex); //指定列是否为空(列基数为0)
c.isClosed(); //游标是否已关闭
c.getCount(); //总数据项数
c.getPosition(); //返回当前游标所指向的行数
c.getColumnIndex(String columnName);//返回某列名对应的列索引值
c.getString(int columnIndex); //返回当前行指定列的值
//----------------------------------------------------------------------------------------------------------------------
public class DataDase extends SQLiteOpenHelper {
/*
super(context, "DB.db", null, 1);
上下文 数据库名称 一般为null 数据库版本
*/
public DataDase(Context context) {
super(context, "DB.db", null, 1);
}
//数据库第一次被创建时onCreate会被调用
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table TimeAxis(id integer primary key autoincrement,Time char(32),Title char(32))");
db.execSQL("create table Time(id integer primary key autoincrement,Time char(32))");
}
//如果DATABASE_VERSION值被改为2,系统发现现有数据库版本不同,即会调用onUpgrade
//可以进行修改操作 db.execSQL("ALTER TABLE person ADD COLUMN other STRING");
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
//--------------------------------对DataDase进行封装----------------------------------------------------------------------------------------------
增删改使用 new Object[]{}
db.execSQL("insert into TimeAxis(Time,Title) values(?,?)", new Object[]{Time, Title});
查使用 new String[]{}
Cursor c = db.rawQuery("select * from Time where Time=?", new String[]{Time});
public class DB {
private DataDase Helper;
public DB(Context context) {
Helper = new DataDase(context);
}
public void addTimeAxis(String Time, String Title) {
SQLiteDatabase db = Helper.getWritableDatabase();
db.execSQL("insert into TimeAxis(Time,Title) values(?,?)", new Object[]{Time, Title});
db.close();
}
public void addTime(String Time) {
SQLiteDatabase db = Helper.getWritableDatabase();
db.execSQL("insert into Time(Time) values(?)", new Object[]{Time});
db.close();
}
public void Delete() {
SQLiteDatabase db = Helper.getWritableDatabase();
db.execSQL("drop table TimeAxis");
db.execSQL("drop table Time");
db.close();
}
public void Update() {
}
public List<BeanTimeAxis> QueryTimeAxis(String Time) {
List<BeanTimeAxis> data = new ArrayList<>();
SQLiteDatabase db = Helper.getReadableDatabase();
Cursor c = db.rawQuery("select * from TimeAxis where Time=? order by id desc", new String[]{Time});
while (c.moveToNext()) {
String sTime = c.getString(c.getColumnIndex("Time"));
String sTitle = c.getString(c.getColumnIndex("Title"));
BeanTimeAxis bean = new BeanTimeAxis(sTime, sTitle);
data.add(bean);
}
c.close();
db.close();
return data;
}
public List<String> QueryTime() {
List<String> data = new ArrayList<>();
SQLiteDatabase db = Helper.getReadableDatabase();
Cursor c = db.rawQuery("select * from Time order by id desc", null);
while (c.moveToNext()) {
String sTime = c.getString(c.getColumnIndex("Time"));
data.add(sTime);
}
c.close();
db.close();
return data;
}
public boolean findTime(String Time) {
SQLiteDatabase db = Helper.getReadableDatabase();
Cursor c = db.rawQuery("select * from Time where Time=?", new String[]{Time});
boolean b = c.moveToNext();
c.close();
db.close();
return b;
}
public boolean findTimeAxis(String Time,String Title) {
SQLiteDatabase db = Helper.getReadableDatabase();
Cursor c = db.rawQuery("select * from TimeAxis where Time=? and Title=?", new String[]{Time,Title});
boolean b = c.moveToNext();
c.close();
db.close();
return b;
}
public int TimeAxisSum(String Time) {
SQLiteDatabase db = Helper.getReadableDatabase();
Cursor c = db.rawQuery("select * from TimeAxis where Time=?", new String[]{Time});
int i = c.getCount();
c.close();
db.close();
return i;
}
public int TimeSum() {
SQLiteDatabase db = Helper.getReadableDatabase();
Cursor c = db.rawQuery("select * from Time", null);
int i = c.getCount();
c.close();
db.close();
return i;
}
}