Android SQLite在170排后崩溃

时间:2015-08-07 08:15:02

标签: android sqlite

如何更改此逻辑以使用超过170行。

//Download as a canvas if contains complex data
    $('.wppdf-download-canvas').click(function() {  
        var pdfname = $(this).attr('name');             
        var htmlSource = $(this).siblings('.wppdf');    
        var canvasToImage = function(canvas){
            var img = new Image();
            var dataURL = canvas.toDataURL('image/png');
            img.src = dataURL;
            return img;
        };
        var canvasShiftImage = function(oldCanvas,shiftAmt){
            shiftAmt = parseInt(shiftAmt, 10) || 0;
            if(!shiftAmt){ return oldCanvas; }

            var newCanvas = document.createElement('canvas');
            newCanvas.height = oldCanvas.height - shiftAmt;
            newCanvas.width = oldCanvas.width;
            var ctx = newCanvas.getContext('2d');

            var img = canvasToImage(oldCanvas);
            ctx.drawImage(img,0, shiftAmt, img.width, img.height, 0, 0, img.width, img.height);

            return newCanvas;
        };

        var canvasToImageSuccess = function(canvas){
            var pdf = new jsPDF2('p','pt'),
                pdfInternals = pdf.internal,
                pdfPageSize = pdfInternals.pageSize,
                pdfScaleFactor = pdfInternals.scaleFactor,
                pdfPageWidth = pdfPageSize.width,
                pdfPageHeight = pdfPageSize.height,
                totalPdfHeight = 0,
                htmlPageHeight = canvas.height,
                htmlScaleFactor = canvas.width / (pdfPageWidth * pdfScaleFactor),
                safetyNet = 0;

            while(totalPdfHeight < htmlPageHeight && safetyNet < 15){
                var newCanvas = canvasShiftImage(canvas, totalPdfHeight);
                pdf.addImage(newCanvas, 'png', 15, 15, pdfPageWidth - (pdfPageWidth * .0 ), 0, null, 'NONE');

                totalPdfHeight += (pdfPageHeight * pdfScaleFactor * htmlScaleFactor);

                safetyNet++;
            }

            pdf.save(pdfname);
        };

我想按输入字符串选择所有行。它的逻辑工作与150行完美配合,但160后缓慢工作并在170行崩溃

3 个答案:

答案 0 :(得分:4)

  

如何更改此逻辑以使用超过170行?

// Getting All test 
public List<Test> getAllTests(String str) {
    List<Test> testList = new ArrayList<Test>();
    // Select All Query

    //String selectQuery = "SELECT  * FROM " + TABLE_TESTS;
String selectQuery = "SELECT id,result FROM " + TABLE_TESTS + " where name ='" + str + "'";
    // Now you are saving memory of one column. 

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

   // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
           Test test = new Test();
           // moved outside loop to prevent creating new object every time.
        do {
            //select rows by input string   
            //if(cursor.getString(1).equals(str)){
            // No need for if Codition any more
                test.setId(Integer.parseInt(cursor.getString(0)));
                //test.setTest(cursor.getString(1));
                test.setTest(str);
                test .setResult(Integer.parseInt(cursor.getString(2)));
                // Adding test to list
                testList.add(test);
            //}
        } while (cursor.moveToNext());
    }
    //close database 
    db.close();

    //return list data     
    return testList;
}

答案 1 :(得分:2)

编辑:错误的方法

使用

String selectQuery = "SELECT  * FROM " + TABLE_TESTS + " WHERE " + your_id + " > " + String.valueOf(last_id) + " LIMIT 150";

作为您的查询结构,然后跟踪最后一行ID,如此`

int last_id;
do {
        //select rows by input string   
        if(cursor.getString(1).equals(str)){
            Test test = new Test();
            last_id = Integer.parseInt(cursor.getString(0));
            test.setId(last_id);
            ...
        }
    } while (cursor.moveToNext());

每次循环结束时,只需再次查询你的数据库;这些行将从您需要的下一行中获取,因为last_id变量会根据您的进度发生变化。

答案 2 :(得分:1)

可以尝试以下列方式使用相同的代码

// using sql query Differently
(SQliteDatabase) db.query(
    "TABLE_TESTS" / table name /,
    new String[] { "id", "result" } / columns names /,
    "name = ?" / where or selection /,
    new String[] { str } / selectionArgs i.e. value to replace ? /,
    null / groupBy /,
    null / having /,
    null / orderBy /
);

另一种方法可能是使用LIMIT和OFFSET来获取部分数据以提高性能

// using LIMIT AND OFFSET 
public List<Test> getAllTests(String str) {

List<Test> testList = new ArrayList<Test>();
// Select All Query

            Integer count = 0;
            String countQuery = "SELECT count(id) FROM " + TABLE_TESTS;
            SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
            if (cursor.moveToFirst()) {
                            count= c.getCount();
            }
            db.close();

            int MAX_LENGTH = 150;

            if ( count > 0 ) {

                            int total_length = ( count / MAX_LENGTH ) + 1;

                            for ( int i=0; i<total_length; i++) {

                                            String selectQuery = "SELECT id,result FROM " + TABLE_TESTS + " LIMIT " + MAX_LENGTH + " OFFSET " + (i*MAX_LENGTH) ;

                                            db = this.getWritableDatabase();
                                            cursor = db.rawQuery(selectQuery, null);

                               // looping through all rows and adding to list
                                            if (cursor.moveToFirst()) {
                                                               Test test = new Test();
                                                               // moved outside loop to prevent creating new object every time.
                                                            do {
                                                                            //select rows by input string   
                                                                            if(cursor.getString(1).equals(str)){
                                                                                            test.setId(Integer.parseInt(cursor.getString(0)));
                                                                                            //test.setTest(cursor.getString(1));
                                                                                            test.setTest(str);
                                                                                            test .setResult(Integer.parseInt(cursor.getString(2)));
                                                                                            // Adding test to list
                                                                                            testList.add(test);
                                                                            }
                                                            } while (cursor.moveToNext());
                                            }
                                            //close database 
                                            db.close();

                            }

            }

//return list data     
return testList;
}