2016-05-20 219 views
1

我正在構建一個SQLite數據庫。其中一個表由2列 - 術語和定義組成。SQLite數據庫查詢

我的問題是:我怎樣才能查詢數據庫,以便對項定義,以便能夠插入數據在活動後(術語和數據是ExpandableListView要返回,該術語是密鑰,數據 - 價值)。

這裏是數據源的到目前爲止的代碼:

public class TermDataSource extends DAO { 

//constants 
public static final String TABLE_NAME = "terms"; 
public static final String TERM = "term"; 
public static final String DEFINITION = "definition"; 

//columns in the table 
public static final int FIELD_ID_ID = 0; 
public static final int FIELD_ID_TERM = 1; 
public static final int FIELD_ID_DEFINITION = 2; 

public TermDataSource (Context context){ 
    super(context); 
} 

private String [] selectFields = {_ID, TERM, DEFINITION}; 

public Cursor getTermsData(){ 
    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor cursor = db.query(TABLE_NAME, selectFields, null, null, null, null, null); 

    return cursor; 
} 
public List<Term> getTerms(){ 

    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor cursor = db.query(TABLE_NAME, selectFields, null, null, null, null, null); 
    List<Term> terms = new ArrayList<Term>(); 

    if(cursor!=null){ 

     Term term = null; 
     while(cursor.moveToNext()){ 
      term = getTermFromCursor(cursor); 
      terms.add(term); 
     } 
     cursor.close(); 
    } 
    db.close(); 
    return terms; 
} 

private Term getTermFromCursor (Cursor cursor){ 
    Term term = new Term(); 
    term.setTermId(cursor.getInt(FIELD_ID_ID)); 
    term.setTerm(cursor.getString(FIELD_ID_TERM)); 
    term.setDefinition(cursor.getString(FIELD_ID_DEFINITION)); 

    return term; 
} 
} 
+0

手段做你想要使用_term和definition_爲特定值來獲得數據? – Piyush

+0

我想用CSV文件中的數據填充它們 – VyaraG

+0

看到我的答案.. !! –

回答

2

創建一個文件夾res>raw和該文件夾中放youfile.csv

使用此方法從CSV文件中將數據插入到數據庫中。

public void insertCSVData(Activity activity, InputStream is, String tableName) { 

    String colunmNames = null, str1 = null; 
    open(); 

    try { 

     BufferedReader buffer = new BufferedReader(new InputStreamReader(is)); 
     String line = ""; 

     String str2 = ");"; 

     db.beginTransaction(); 

     int i = 0; 
     while ((line = buffer.readLine()) != null) { 
      i++; 
      if (i == 1) { 
       colunmNames = line; 
       str1 = "INSERT INTO " + tableName + " (" + colunmNames + ") values ("; 
      } else { 

       StringBuilder sb = new StringBuilder(str1); 
       String[] str = line.split(","); 

       for (int h = 0; h < str.length; h++) { 

        if (h == str.length - 1) { 
         sb.append("'" + str[h] + "'"); 
        } else { 
         sb.append("'" + str[h] + "',"); 
        } 
       } 

       sb.append(str2); 
       db.execSQL(sb.toString()); 
      } 
     } 

     db.setTransactionSuccessful(); 
     db.endTransaction(); 

    } catch (Exception e) { 

     close(); 

     e.printStackTrace(); 
    } 

    close(); 
} 

調用此方法通過下面的代碼:

insertCSVData(Activity.this, getResources().openRawResource(R.raw.yourfile),"Your Table Name"); 
+0

非常感謝!我會回來測試後的反饋:) – VyaraG

+0

我的榮幸..快樂編碼.. !! –

+0

謝謝@ janki,它工作的很好! – VyaraG