2012-03-27 5 views
2
아래

을 사용하여 일괄 데이터 삽입 (한 번) 코드입니다, 현재 사용하고 있습니다,하지만 내 삽입 속도를 높이는 방법을 찾고 있는데 처음으로 100 개 이상의 행을 삽입했습니다 ... any 생각?안드로이드 : SQLite

public class SQLiteAdapter { 

private SQLiteHelper sqLiteHelper; 
private SQLiteDatabase sqLiteDatabase; 
private Context context; 

public SQLiteAdapter(Context c){ 
    context = c; 
} 

    public long insert(String empId, String name......) 
    { 
     ContentValues contentValues = new ContentValues(); 
     contentValues.put(KEY_EMP_ID, empId); 
     ..................... 
     ..................... 
     ..................... 
     return sqLiteDatabase.insert(KEY_TABLE, null, contentValues); 
    } 


    public void InsertReciters() 
    { 
    //currently, this is how i am doing, how can i speed up inserting the rows in to db? 
    //i have 100+ rows .......... 

    this.insert("ac123", ................); 
    this.insert("ac133", ................); 
    this.insert("ac143", ................); 
    this.insert("ac153", ................); 
    this.insert("ac163", ................); 
    ................. 
    ................. 
    } 
} 
+1

당신은 미리 채워진 데이터베이스를 사용할 수 있습니다 http://stackoverflow.com/a/9109728/265167을 –

+0

감사 Yaqub 1+ –

+0

준비된 문 및 거래 : HTTP : // 유래. com/a/29797229/3681880 – Suragch

답변

2

큰 데이터를 트랜잭션으로 삽입하십시오.

코드 형식 아래 참조 :

List<String[]> hugeData=new ArrayList<String[]>(); 
try{ 
    sqLiteDatabase.beginTransaction(); 
    //insert huge data 
    //get pre-compiled SQLiteStatement object 
    SQLiteStatement statement=sqLiteDatabase.compileStatement("insert into tablename(..) value (?,?)") 
    for(String[] row:hugeData){ 
     statement.bindString(1,row[0]); 
     //...... 
     statement.execute(); 
    } 
    sqLiteDatabase.setTransactionSuccessful(); 
}finally{ 
    sqLiteDatabase..endTransaction(); 
} 

이 --------------------- 편집 ------------ ------

public class SQLiteAdapter { 

private SQLiteHelper sqLiteHelper; 
private SQLiteDatabase sqLiteDatabase; 
private Context context; 
List<String[]> insertData=null; 

public SQLiteAdapter(Context c){ 
    context = c; 
} 
public SQLiteAdapter(Context c,List<String[]> _insertData){ 
    this(c); 
    insertData=_insertData; 
} 
    public long insert(String empId, String name......) 
    { 
     ContentValues contentValues = new ContentValues(); 
     contentValues.put(KEY_EMP_ID, empId); 
     ..................... 
     ..................... 
     ..................... 
     return sqLiteDatabase.insert(KEY_TABLE, null, contentValues); 
    } 


    public void InsertReciters() 
    { 
    //currently, this is how i am doing, how can i speed up inserting the rows in to db? 
    //i have 100+ rows .......... 

    this.insert("ac123", ................); 
    this.insert("ac133", ................); 
    this.insert("ac143", ................); 
    this.insert("ac153", ................); 
    this.insert("ac163", ................); 
    ................. 
    ................. 
    } 

    public void InsertBatchData() 
    { 
    try{ 
    sqLiteDatabase.beginTransaction(); 
    //insert huge data 
    //get pre-compiled SQLiteStatement object 
    SQLiteStatement statement=sqLiteDatabase.compileStatement("insert into tablename(..) value (?,?)") 
    for(String[] row:hugeData){ 
     statement.bindString(1,row[0]); 
     //...... 
     statement.execute(); 
    } 
    sqLiteDatabase.setTransactionSuccessful(); 
    }finally{ 
    sqLiteDatabase..endTransaction(); 
    } 
    } 
} 
+0

필자는 '.... compileStatement ("insert ....'내가 100 개 이상의 행을 가지고 있음을 이해하지 못한다 ... –

+0

그 결과 미리 컴파일 된 SQLi 설명 객체. 그런 다음이 객체를 사용하여 여러 번이 명령문을 효율적으로 실행할 수 있습니다. – boiledwater

+1

좋아요. 그래서'value'에서 혼란 스럽습니다. 정의 할 행에 대한 값을 입력 한 다음 for 루프 문에 입력 하시겠습니까? 설명하기 위해 내 코드를 사용할 수 있습니까? 내가 삽입 문을 사용한 방식과 조금 혼동 스러울 수 있습니다. –