0

我现在正在实现一个 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()。那没有正常工作。
4

1 回答 1

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 ()

于 2012-08-11T18:25:52.970 回答