我的应用程序中有一个 MAC 地址的嵌入式数据库文件(资产目录中的 Macs.db)。该数据库是一个非常简单的 sqlite 数据库,只有一个表。名为 mactoname 的表有两列:1. macaddress,2.name。此嵌入式数据库文件中有一些预加载的 mac 地址和名称。我有一个 wifi 分析器工具来检测某个地方的 mac 地址。该分析器的代码如下(来自 marakana.com):
public class WiFiScanner extends Activity {
TextView name;
TextView textView;
WifiManager wifi;
BroadcastReceiver receiver;
Button buttonScan,updatemacdb;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scannerwifi);
textView = (TextView) this.findViewById(R.id.wifitext);
buttonScan = (Button) findViewById(R.id.buttonScan);
updatemacdb=(Button) findViewById(R.id.buttonupdatezoneDB);
wifi = (WifiManager) this.getSystemService(WIFI_SERVICE);
name=(TextView) this.findViewById(R.id.edittextname);
buttonScan.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
wifi.startScan();
}
});
updatemacdb.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
}
});
if (receiver == null)
receiver = new WiFiScanReceiver(this);
registerReceiver(receiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
populate();
}
private void populate() {
String text = "";
List<ScanResult> access_points = wifi.getScanResults();
for(int i = 0 ; i < access_points.size() ; i++){
ScanResult ap = access_points.get(i);
text += "#SSID: " + ap.SSID + "/MAC: " + ap.BSSID + "\n\n";
}
textView.setText(text);
Log.d("Wifi Display", text);
}
class WiFiScanReceiver extends BroadcastReceiver {
WiFiScanner wifiScanner;
public WiFiScanReceiver(WiFiScanner wifisc) {
super();
this.wifiScanner = wifisc;
}
@Override
public void onReceive(Context c, Intent intent) {
wifiScanner.populate();
}
}
public void onBackPressed() {
unregisterReceiver(receiver);
}
}
现在,当我检测到 mac 地址时,我想通过将检测到的 mac 地址放入 Macs.db 文件来更新 Macs.db。更新应在以下按钮单击完成:
updatemacdb.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
}
});
如果一个 mac 地址已经存在,那么它就不会再次放入 Macs.db。连同在 macaddress 列中检测到的新 MAC 地址,用户在 edittext 视图中键入的名称也将在 Macs.db 的 mactoname 表的名称列中更新。我有一个 SQLiteOpenHelper 类的实现。我需要在这个助手类和 updatemacdb 按钮的 OnClickListener 中使用什么代码才能使用新的 mac 地址和名称更新数据库?
下面给出了我的 SQLiteOpenHelper 类。我需要在这个类中修改哪些方法?
public class DbAdapternew {
private static final String TAG = DbAdapter.class.getName();
private DatabaseHelper mDbHelper;
public static SQLiteDatabase myDb;
public static String DATABASE_NAME;
public static String DATABASE_TABLE;
public static String DATABASE_PATH;
public static String LOCAL_DATABASE_NAME;
private static final int DATABASE_VERSION = 2;
private static Context myContext;
private static class DatabaseHelper extends SQLiteOpenHelper {
Context helperContext;
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
helperContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database!!!!!");
onCreate(db);
}
public void createDataBase() throws IOException {
Log.i(TAG,"DB NAME : "+DATABASE_NAME);
Log.i(TAG,"DB PATH : "+DATABASE_PATH);
Log.i(TAG,"LOCAL DB NAME : "+LOCAL_DATABASE_NAME);
boolean dbExist = checkDataBase();
if (dbExist) {
} else {
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
//throw new Error("Error copying database");
}
}
}
public SQLiteDatabase getDatabase() {
String myPath = DATABASE_PATH + DATABASE_NAME;
return SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DATABASE_PATH + DATABASE_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException {
Log.i(TAG,"LOCAL DB NAME : "+LOCAL_DATABASE_NAME);
// Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(LOCAL_DATABASE_NAME);
// Path to the just created empty db
String outFileName = DATABASE_PATH + DATABASE_NAME;
// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException {
// Open the database
String myPath = DATABASE_PATH + DATABASE_NAME;
myDb = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
}
@Override
public synchronized void close() {
if (myDb != null)
myDb.close();
super.close();
}
}
public DbAdapternew(Context ctx) {
this.myContext = ctx;
}
public DbAdapternew open() throws SQLException {
mDbHelper = new DatabaseHelper(myContext);
try {
mDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
mDbHelper.openDataBase();
mDbHelper.copyDataBase();
} catch (SQLException sqle) {
throw sqle;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return this;
}
public void close() {
mDbHelper.close();
}
public Cursor fetchAllData(String table) {
try {
myDb.rawQuery("SELECT * FROM " + table, null);
Log.i(TAG, "Row Collected");
} catch (SQLiteException e) {
myDb.execSQL("CREATE TABLE IF NOT EXISTS " + table
+ " (mac_add VARCHAR)");
Log.i(TAG, "TABLE Not Found");
}
return myDb.rawQuery("SELECT * FROM " + table, null);
}
public Cursor fetchData(String sqlQuery) {
try {
myDb.rawQuery(sqlQuery, null);
Log.i(TAG, "Query Executed");
} catch (SQLiteException e) {
Log.i(TAG, e.toString());
}
return myDb.rawQuery(sqlQuery, null);
}
public void executeQuery(String sql) {
myDb.execSQL(sql);
}
public static SQLiteDatabase getWritableDatabase() {
String myPath = DATABASE_PATH + DATABASE_NAME;
return SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
}
}