Andorid sqlite 简单使用

本文旨在记录我的一次数据库使用实例,存取经纬度,需求简单,实现了简单的增删改查。
一,创建一个数据库帮助类DBOpenHelpe.class

public class DBOpenHelpe extends SQLiteOpenHelper {

public DBOpenHelpe(Context context) {
    super(context, "upload.db", null, 1);
}
//lat=纬度 lon=经度
@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE pointdb" +
            " (_id integer primary key autoincrement," +
            " lat varchar(100), lon varchar(100))");
}

@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
    db.execSQL("DROP TABLE IF EXISTS pointdb");
    onCreate(db);
}
}

二、就是简单的增删改查的方法封装了UploadLogService.class

public class UploadLogService {
private DBOpenHelpe dbOpenHelpe;

public UploadLogService(Context context) {
    this.dbOpenHelpe = new DBOpenHelpe(context);//初始化数据库
}

public void save(String lat, String lon) {//插入
    SQLiteDatabase db = dbOpenHelpe.getWritableDatabase();//打开数据库
    db.execSQL("insert into pointdb(lat,lon) " +
            "values(?,?)", new Object[]{lat, lon});
}
 public   void update(String lat ,String lon){//更新
     SQLiteDatabase db = dbOpenHelpe.getWritableDatabase();//打开数据库
     db.execSQL("update pointdb set lat =36.19 where id = 1");
 }

public void delete() {//删除
    SQLiteDatabase db = dbOpenHelpe.getWritableDatabase();
    db.execSQL("delete from pointdb");
}

public List getlist(){//获取
    List list=new ArrayList<>();
    SQLiteDatabase db = dbOpenHelpe.getWritableDatabase();
    Cursor cursor =db.rawQuery("select * from pointdb", null);
    while (cursor.moveToNext())
    {
        int id=cursor.getInt(cursor.getColumnIndex("_id"));
        String lat = cursor.getString(cursor.getColumnIndex("lat"));
        String lon =  cursor.getString(cursor.getColumnIndex("lon"));
        Log.i("查询的数据", "lat:" + lat + " lon:" + lon+"/"+id);
        Xzgjbean xzgjbean=new Xzgjbean();
        xzgjbean.setId(id);
        xzgjbean.setLat(lat);
        xzgjbean.setLon(lon);
        list.add(xzgjbean);
    }
    cursor.close();
    return list;
}
}

你可能感兴趣的:(Andorid sqlite 简单使用)