2016-05-20 164 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