实现对数据库info.db的表signalinfo,resultsinfo的操作。
提示:这里可以添加本文要记录的大概内容:
编程参考: https://blog.csdn.net/sinat_38184748/article/details/88532631 https://blog.csdn.net/qq_19343089/article/details/72985675
提示:以下是本篇文章正文内容,下面案例可供参考
1.Dbhelper
代码如下(示例):
public class SignalDbHelper extends SQLiteOpenHelper {
public static final String DataBase_name = "info.db";
public static final String SignalTable_name = "signalinfo";
public static final String ResultsTable_name = "resultsinfo";
public static final int DataBase_version = 1;
public static final String longitude = "longitude", latgitude = "latgitude",distance="distance",sign="sign";
public static final String wname = "wname", wlongitude = "wlongitude",wlatgitude = "wlatgitude",wsign="wsign";
public SignalDbHelper( Context context) {
super(context, DataBase_name, null, DataBase_version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table if not exists "+ SignalTable_name +"(_id INTEGER PRIMARY KEY AUTOINCREMENT,longitude DOUBLE NOT NULL,latgitude DOUBLE NOT NULL,distance DOUBLE NOT NULL,sign TEXT);");
db.execSQL("create table if not exists "+ ResultsTable_name +"(_id INTEGER PRIMARY KEY AUTOINCREMENT,wname TEXT NOT NULL, wlongitude DOUBLE NOT NULL,wlatgitude DOUBLE NOT NULL,wsign TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists "+ SignalTable_name);
db.execSQL("drop table if exists "+ ResultsTable_name);
onCreate(db);
}
}
2.DAO 编写对两个表的CURD方法
代码如下(示例):
public class SignalDAO {
private SQLiteDatabase sch_db;
private SignalDbHelper sch_helper;
public String DataBase_name = "info.db";
public SignalDAO(Context contsxt) {
this.sch_helper = new SignalDbHelper(contsxt);
}
public void closeDatabase() {
sch_db.close();
}
public void insertSignalInfo(double lon, double lat,double dis,String sign) {
sch_db = sch_helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(SignalDbHelper.longitude, lon);
contentValues.put(SignalDbHelper.latgitude, lat);
contentValues.put(SignalDbHelper.distance, dis);
contentValues.put(SignalDbHelper.sign, sign);
long i = sch_db.insert(SignalDbHelper.SignalTable_name, null, contentValues);
Log.v("signal数据库测试", "插入");
}
public Cursor allQuerySignalInfo() {
sch_db = sch_helper.getWritableDatabase();
Cursor cursor=sch_db.rawQuery("select * from "+ SignalDbHelper.SignalTable_name, null);
Log.d("signal数据库查询所有结果", String.valueOf(cursor.getCount()));
return cursor;
}
public int getSignalRecordsNumber(){
sch_db = sch_helper.getReadableDatabase();
Cursor cursor= sch_db.rawQuery("select * from "+SignalDbHelper.SignalTable_name,null);
return cursor.getCount();
}
public void clearSignalTable() {
sch_db = sch_helper.getWritableDatabase();
sch_db.execSQL("delete from " + SignalDbHelper.SignalTable_name);
Log.d("signal清空数据表","....");
}
public void insertWifiInfo(String wname,double lon, double lat,String wsign) {
sch_db = sch_helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(SignalDbHelper.wname, wname);
contentValues.put(SignalDbHelper.longitude, lon);
contentValues.put(SignalDbHelper.latgitude, lat);
contentValues.put(SignalDbHelper.wsign,wsign);
long i = sch_db.insert(SignalDbHelper.ResultsTable_name, null, contentValues);
Log.v("wifi数据库测试", "插入");
}
public Cursor allQueryWifiInfo() {
sch_db = sch_helper.getWritableDatabase();
Cursor cursor=sch_db.rawQuery("select * from "+ SignalDbHelper.ResultsTable_name, null);
Log.d("wifi数据库查询所有结果", String.valueOf(cursor.getCount()));
return cursor;
}
private void updateWifiInfo(String wname,double lon, double lat,String wsign) {
sch_db = sch_helper.getReadableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(SignalDbHelper.wname, wname);
contentValues.put(SignalDbHelper.longitude, lon);
contentValues.put(SignalDbHelper.latgitude, lat);
contentValues.put(SignalDbHelper.wsign,wsign);
int i = sch_db.update(SignalDbHelper.ResultsTable_name, contentValues, SignalDbHelper.wsign + "=?", new String[]{wsign});
if (i < 1)
Log.v("wifi数据库测试", "更新失败");
}
public Cursor idWifiQuery(String id) {
sch_db = sch_helper.getReadableDatabase();
Cursor cursor = sch_db.query(SignalDbHelper.ResultsTable_name, null, SignalDbHelper.wsign + "=?", new String[]{String.valueOf(id)}, null, null, null);
Log.d("wifi数据库查询结果(是否存在)", String.valueOf(cursor.getCount()));
return cursor;
}
}
3.使用DAO对象进行数据库操作
省略......
|