尝试从数据库中检索时,应用程序进入无限循环

时间:2013-12-04 06:54:12

标签: android android-sqlite

DBAdapter.java:

    public DBAdapter open() throws SQLException {

                    mDBHelper = new DBHelper(mctx,DATABASE_NAME,null,DATABASE_VERSION);
                    mDB = mDBHelper.getWritableDatabase();
                    return this;
                }

                public synchronized void close() {

                    mDBHelper.close();
                }

        public long insertRecord(String firstname, String middlename,
                        String lastname) {
            ContentValues cv = new ContentValues();
                    cv.put(KEY_FIRSTNAME, firstname);
                    cv.put(KEY_MIDDLENAME, middlename);
                    cv.put(KEY_LASTNAME, lastname);

                    return mDB.insert(DATABASE_TABLE, null, cv);

                }



                public int deleteAll(){
                    return mDB.delete(DATABASE_TABLE, null, null);
                }


                public String getName() {
                    String column[] = new String[] { KEY_FIRSTNAME, KEY_MIDDLENAME,
                            KEY_LASTNAME };
                    Cursor c = mDB.query(DATABASE_TABLE, column, null, null, null, null,
                            null);
                    String name = "";
                    int iFirst = c.getColumnIndex(KEY_FIRSTNAME);
                    int iMiddle = c.getColumnIndex(KEY_MIDDLENAME);
                    int iLast = c.getColumnIndex(KEY_LASTNAME);
                    for (c.moveToFirst(); !c.isAfterLast(); c.moveToLast()) {
                        name = name + c.getString(iFirst) + "\t" + c.getString(iMiddle)
                                + "\t" + c.getString(iLast) + "\t";
                    }
                    c.close();
                    return name;

                }

Profile_database_view.java:

        db = new DBAdapter(Profile_Database_View.this);
                db.open();
                full_name.setText(db.getName());

MainActivity.java:

        dbadapter = new DBAdapter(MainActivity.this);
                        dbadapter.open();
                        dbadapter.insertRecord(first_name, middle_name, last_name);
                        dbadapter.close();
                    }

Logcat:

    12-04 12:10:21.273: I/ActivityManager(60): Starting: Intent { cmp=com.example.display_from_database/.Profile_Database_View } from pid 364
    12-04 12:10:21.802: D/dalvikvm(364): GC_EXTERNAL_ALLOC freed 704K, 56% free 2693K/6023K, external 2032K/2137K, paused 193ms
    12-04 12:10:22.392: D/dalvikvm(364): GC_FOR_MALLOC freed 971K, 61% free 2656K/6727K, external 2379K/2971K, paused 118ms
    12-04 12:10:22.853: D/dalvikvm(364): GC_CONCURRENT freed 793K, 60% free 2697K/6727K, external 2379K/2971K, paused 14ms+18ms
    12-04 12:10:23.232: D/dalvikvm(364): GC_CONCURRENT freed 837K, 60% free 2703K/6727K, external 2379K/2971K, paused 26ms+7ms
    12-04 12:10:23.482: D/dalvikvm(364): GC_CONCURRENT freed 854K, 60% free 2699K/6727K, external 2379K/2971K, paused 10ms+10ms
    12-04 12:10:23.714: D/dalvikvm(364): GC_CONCURRENT freed 842K, 60% free 2706K/6727K, external 2379K/2971K, paused 9ms+7ms
    12-04 12:10:23.952: D/dalvikvm(364): GC_CONCURRENT freed 838K, 60% free 2711K/6727K, external 2379K/2971K, paused 11ms+7ms
    12-04 12:10:24.192: D/dalvikvm(364): GC_CONCURRENT freed 823K, 60% free 2739K/6727K, external 2379K/2971K, paused 10ms+9ms
    12-04 12:10:24.432: D/dalvikvm(364): GC_CONCURRENT freed 852K, 60% free 2737K/6727K, external 2379K/2971K, paused 13ms+9ms
    12-04 12:10:24.662: D/dalvikvm(364): GC_CONCURRENT freed 867K, 60% free 2725K/6727K, external 2379K/2971K, paused 10ms+7ms
    12-04 12:10:24.912: D/dalvikvm(364): GC_CONCURRENT freed 857K, 60% free 2729K/6727K, external 2379K/2971K, paused 13ms+6ms
    12-04 12:10:25.122: D/dalvikvm(364): GC_CONCURRENT freed 836K, 60% free 2752K/6727K, external 2379K/2971K, paused 8ms+8ms
    12-04 12:10:25.352: D/dalvikvm(364): GC_CONCURRENT freed 875K, 60% free 2737K/6727K, external 2379K/2971K, paused 8ms+7ms
    12-04 12:10:25.483: D/dalvikvm(364): GC_FOR_MALLOC freed 914K, 61% free 2670K/6727K, external 2379K/2971K, paused 108ms
    12-04 12:10:25.702: D/dalvikvm(364): GC_CONCURRENT freed 731K, 59% free 2765K/6727K, external 2379K/2971K, paused 8ms+9ms
    12-04 12:10:25.932: D/dalvikvm(364): GC_CONCURRENT freed 852K, 59% free 2781K/6727K, external 2379K/2971K, paused 11ms+7ms
    12-04 12:10:26.163: D/dalvikvm(364): GC_CONCURRENT freed 859K, 59% free 2785K/6727K, external 2379K/2971K, paused 9ms+7ms

我能够将数据插入到数据库中,但在检索数据时会进入此状态 无限循环和我的应用程序变成一个空白的黑屏。有人遇到过这个吗?

帮助我们。这种情况一直发生在我身上,谷歌搜索说我正在做 使用CPU的一些密集过程,但它就像检索数据库一样简单。

5 个答案:

答案 0 :(得分:2)

啊,是的,把两个答案放在一起 - 你的错误就是你的条件是达到c.isAfterLast(),但是你把它增加到c.moveToLast(),它永远不会在最后一步之后。因此,无论是更改条件还是将递增步骤更改为c.moveToNext(),您都可以。 祝你好运!

答案 1 :(得分:1)

你的for循环有问题

  

for(c.moveToFirst();!c.isAfterLast(); c.moveToLast())

c.moveToLast()做了什么吗?

答案 2 :(得分:0)

你的循环是可疑的,如果你想通过迭代游标,总是使用do while。 e.g

if(cursor != null && cursor.moveToFirst()){

   do{
       // You code for reading from cursor
     }while(cursor.moveToNext());

}

这将为你完成这项工作。

答案 3 :(得分:0)

SQLiteDatabase db = mDbHelper.getReadableDatabase();

// Define a projection that specifies which columns from the database
// you will actually use after this query.
String[] projection = {
    COLUMN_ID,
    KEY_FIRSTNAME,  
    KEY_MIDDLENAME,
    KEY_LASTNAME
    };




Cursor c = db.query(
    TABLE_NAME,  // The table to query
    projection,                               // The columns to return
    selection,                                // The columns for the WHERE clause
    selectionArgs,                            // The values for the WHERE clause
    null,                                     // don't group the rows
    null,                                     // don't filter by row groups
    null                                 // The sort order
    );

尝试这一点......在选择中,如果你需要,选择的参数会实现你的条件

答案 4 :(得分:0)

在for循环中将您的上一个条件c.moveToLast()更改为c.moveToNext()

试试如下:

   public String getName() {
                String column[] = new String[] { KEY_FIRSTNAME, KEY_MIDDLENAME,
                        KEY_LASTNAME };
                Cursor c = mDB.query(DATABASE_TABLE, column, null, null, null, null,
                        null);
                String name = "";
                for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) 
                {
                  int iFirst = c.getColumnIndex(KEY_FIRSTNAME);
                  int iMiddle = c.getColumnIndex(KEY_MIDDLENAME);
                   int iLast = c.getColumnIndex(KEY_LASTNAME);

                    name = name + c.getString(iFirst) + "\t" + c.getString(iMiddle)
                            + "\t" + c.getString(iLast) + "\t";
                }
                c.close();
                return name;

            }