我試圖簡單地抓住當前在我的Android上的SQLite數據庫中的每一行,並從每行抓取每個單獨的列。我相信這些數據實際上是在數據庫中,因爲我調用一個單獨的函數DataHelper.selectAll()來打印出來記錄所有的數據。我期望的數據可以在Eclipse中的LogCat中正確顯示。如果它很重要,我基本上使用的DataHelper.java程序可以在這裏找到:http://www.screaming-penguin.com/node/7742。我將在下面發佈相關代碼。重要的東西是在doPost()中。無法使用Android光標從數據庫中選擇列
// from inside onCreate of Main (which extends activity)...
Button buttonPost = (Button) findViewById(R.id.buttonPost);
buttonPost.setOnClickListener(new OnClickListener() {
// doesn't actually post to website yet
public void onClick(View v) {
Log.d(TAG, "In onclicklistener.");
ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWifi.isConnected()) {
Log.d(TAG, "Connected, calling doPost()!");
if (doPost()) {
// ...
}
}
}
});
public boolean doPost() {
boolean outcome = false;
long ret = dh.insert(main.id++ + "," + "my test2" + "," + "5:4:2:3" + "," + -300 + "," + -300 + "," + -1 + "," +
-70 + "," + 3 + "," + -3 + "," + main.device_id);
Log.d(TAG, "ret from static insert:" + ret);
Cursor cursor = this.dh.db.query(DataHelper.TABLE_NAME,
new String[] { "id", "rssi", "wap_id", "lat" },
null, null, null, null, "id desc");
Log.d(TAG, "Cursor column count: " + cursor.getColumnCount());
if (cursor.moveToFirst()) {
do {
HttpPost post = new HttpPost(OUR_SERVER);
Log.d(TAG, "rssi index:" + cursor.getColumnIndex("rssi"));
Log.d(TAG, "the rssi: " + cursor.getInt(cursor.getColumnIndex("rssi")));
} while (cursor.moveToNext());
}
// release connection here?
cm.shutdown();
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
return outcome;
}
現在有什麼奇怪的,這是在我的logcat它成功地打印出完整的行(就像一個字符串),所以我知道我能夠訪問數據庫。的RSSI值,我希望不會被記錄,而不是我得到:
Cursor column count: 4
id value: 6
id index: 0
rssi index:1
the rssi: 0
id value: 5
id index: 0
rssi index:1
the rssi: 0
我的,因爲很多網上的例子都訪問數據以同樣的方式在虧損。這是否與我在OnClickListener中的事實有關?當我這樣做時沒有調試器警告。
謝謝!