我正在尝试简单地抓取Android上SQLite数据库中当前的每一行,并从每一行中获取每个列。我相信数据实际上是在数据库中,因为我调用了一个单独的函数DataHelper.selectAll()来打印出来记录所有数据。我期望的数据正确地显示在Eclipse中的LogCat中。如果它很重要,我基本上使用的DataHelper.java程序可以在这里找到:http://www.screaming-penguin.com/node/7742。我将在下面发布相关代码。重要的是在doPost()。
// 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中的事实有关?我这样做时没有调试器警告。
谢谢!
答案 0 :(得分:0)
没关系。事实证明我正在使用的DataHelper.java类只是将所有内容插入第一列。这非常违反直觉。这就是我在线查找代码并假设它做正确的事情。
为了解决这个问题,我手动编写了一个完整的SQL语句,编译了该语句并运行它。