I'm implementing an Android Application now. I made a database in the application, and I tried to check it DDMS and SQLite Database Browser 2.0 b1.
The table I created has several rows (I could checked by System.out.println in my program), but only the 1st row is showed in SQLite Database Browser.
The latest updated time of database file which is showed in DDMS is correct.
Please let me know how I can resolve this problem.
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());
}
}
}
Solve
- Use
System.out.println("Table Interests has " + test.getCount() + "rows");
instead ofSystem.out.println("Table Interests has " + test.getColumnCount() + "rows");
Then I can get a correct number of rows.
- I should to be careful the position of
db.beginTransaction()
. Im my cord, there istry
betweendb.beginTransaction()
anddb.endTransaction()
. That did not work correctly.