我正在实施Android应用程序。 我在应用程序中创建了一个数据库,我试着检查它DDMS和SQLite Database Browser 2.0 b1。
我创建的表有几行(我可以在我的程序中通过System.out.println检查),但只有第一行显示在SQLite数据库浏览器中。
DDMS中显示的数据库文件的最新更新时间是正确的。
请告诉我如何解决此问题。
public void onCreate(Bundle savedInstanceState) {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.permitAll().build());
super.onCreate(savedInstanceState);
setContentView(R.layout.twitterdata);
helper = new CreateTableHelper(TwitterData.this);
db = helper.getWritableDatabase();
mSharedPreferences = getSharedPreferences(Const.PREFERENCE_NAME,
MODE_PRIVATE);
/**
* Handle OAuth Callback
*/
Uri uri = getIntent().getData();
if (uri != null && uri.toString().startsWith(Const.CALLBACK_URL)) {
String verifier = uri
.getQueryParameter(Const.IEXTRA_OAUTH_VERIFIER);
try {
AccessToken accessToken = twitter.getOAuthAccessToken(
requestToken, verifier);
User user = twitter.verifyCredentials();
List<Status> statuses = twitter.getUserTimeline(user
.getScreenName());
for (Status status : statuses) {
String tweet = status.getText();
String[] words = tweet.split(" ");
for (int i = 0; i < words.length; i++) {
if (words[i].length() > 1) {
String requestURL = "http://en.wikipedia.org/w/api.php?action=query&prop=categories&format=json&clshow=!hidden&cllimit=10&titles="
+ words[i];
URL wikiRequest = new URL(requestURL);
URLConnection connection = wikiRequest
.openConnection();
connection.setDoOutput(true);
Scanner scanner = new Scanner(
wikiRequest.openStream());
String response = scanner.useDelimiter("\\Z")
.next();
JSONObject json = Util.parseJson(response);
JSONObject query = json.getJSONObject("query");
JSONObject pages_jsn = query.getJSONObject("pages");
String pages = query.getString("pages");
int dqm_cnt = 0;
int dqm_while = 0;
String page_id_str = "";
Character dq = pages.charAt(0);
while (dqm_cnt < 2) {
Character tmp = pages.charAt(dqm_while);
if (tmp.equals(dq)) {
dqm_cnt++;
}
try {
Integer.parseInt(tmp.toString());
page_id_str += tmp;
} catch (NumberFormatException e) {
}
dqm_while++;
}
int page_id = Integer.parseInt(page_id_str);
// The article is available.
if (page_id != 1) {
JSONObject pageidjson = pages_jsn
.getJSONObject(page_id_str);
db.beginTransaction();
try {
// category is available
String tmp_interest = pageidjson
.getString("title");
JSONArray categories_array = pageidjson
.getJSONArray("categories");
// interests
db = helper.getReadableDatabase();
String[] i_columns = { "id", "name",
"count" };
SQLiteCursor ic = (SQLiteCursor) db.query(
"interests", i_columns, "name = '"
+ tmp_interest + "'", null,
null, null, null);
db.setTransactionSuccessful();
db.endTransaction();
if (ic.getCount() == 0) {
db = helper.getWritableDatabase();
db.beginTransaction();
ContentValues val_interest = new ContentValues();
val_interest.put("name", tmp_interest);
val_interest.put("count", 1);
db.insert("interests", null,
val_interest);
System.out.println("successfully inserted " + tmp_interest);
db.setTransactionSuccessful();
db.endTransaction();
} else {
ic.moveToFirst();
int i_id = ic.getInt(0);
db.beginTransaction();
ContentValues db_i_val = new ContentValues();
db_i_val.put("count", ic.getInt(2) + 1);
db.update("interests", db_i_val, "id = "
+ i_id, null);
db.setTransactionSuccessful();
db.endTransaction();
}
} catch (Exception e) {
Log.e("ERROR", e.toString());
}
}
scanner.close();
}
}
System.out.println("@" + status.getUser().getScreenName()
+ " - " + status.getText());
}
db = helper.getReadableDatabase();
String[] i_columns = { "id", "name",
"count" };
SQLiteCursor test = (SQLiteCursor) db.query(
"interests", i_columns, null, null,
null, null, null);
System.out.println("Table Interests has " + test.getColumnCount() + "rows");
db.setTransactionSuccessful();
db.endTransaction();
db.close();
Editor e = mSharedPreferences.edit();
e.putString(Const.PREF_KEY_TOKEN, accessToken.getToken());
e.putString(Const.PREF_KEY_SECRET, accessToken.getTokenSecret());
e.commit();
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}
}
解决
System.out.println("Table Interests has " + test.getCount() + "rows");
代替System.out.println("Table Interests has " + test.getColumnCount() + "rows");
然后我可以获得正确的行数。
db.beginTransaction()
的位置。我的电话,try
和db.beginTransaction()
之间有db.endTransaction()
。这不能正常工作。 答案 0 :(得分:0)
这是不正确的:
System.out.println("Table Interests has " + test.getColumnCount() + "rows");
这将打印列数。
您应该使用:
System.out.println("Table Interests has " + test.getCount() + "rows");
<强>文档强> http://developer.android.com/reference/android/database/sqlite/SQLiteCursor.html#getCount()