创建的数据库:
class SQLiteDB extends SQLiteOpenHelper {
private static final String DATTBASE_NAME = "gpsdatasssss.db";
private static final int DATABASE_VERSION = 1;
public SQLiteDB(Context context) {
super(context, DATTBASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sdb) {
// 首次创建数据库,该方法被调用
sdb.execSQL("create table tab_gps (id integer PRIMARY KEY ,infotype integer,latitude integer,longitude integer,high double,direct double,speed double,gpstime varchar,flag integer);");
}
@Override
public void onUpgrade(SQLiteDatabase sdb, int oldVersion, int newVersion) {
sdb.execSQL("drop table if exists tab_gps");
onCreate(sdb);
}
}
查询一段时间的数据:
public List<GpsData> getMyTrace() {
List<GpsData> myTraces = new ArrayList<GpsData>();
Date d = new Date();
d.setTime(System.currentTimeMillis());
String mCurrent = DateFormat.format("yyyy-MM-dd kk:mm:ss", d).toString();
Date d1 = new Date();
d1.setTime(d.getTime() - 14400000);
String mFrom = DateFormat.format("yyyy-MM-dd kk:mm:ss", d1).toString();
String sql = "select * from tab_gps where gpstime Between '" +mFrom +"' and '"+mCurrent + "'";
Cursor cursor = db.rawQuery(sql, null);
while (cursor.moveToNext()) {
GpsData gps = new GpsData();
gps.id = cursor.getInt(cursor.getColumnIndex("id"));
gps.InfoType = cursor.getInt(cursor.getColumnIndex("infotype"));
gps.Latitude = cursor.getInt(cursor.getColumnIndex("latitude")); // 获得纬度
gps.Longitude = cursor.getInt(cursor.getColumnIndex("longitude"));// 获得经度
gps.High = cursor.getDouble(cursor.getColumnIndex("high"));
gps.Direct = cursor.getDouble(cursor.getColumnIndex("direct"));
gps.Speed = cursor.getDouble(cursor.getColumnIndex("speed"));
gps.GpsTime = cursor.getString(cursor.getColumnIndex("gpstime"));
myTraces.add(gps);
}
return myTraces;
}