SQLite数据库查询

时间:2016-05-20 04:09:10

标签: android sqlite

我正在构建SQLite数据库。其中一个表由2列组成 - 术语和定义。

我的问题是:我如何查询数据库,以便返回对的术语定义,以便能够在活动之后插入数据(术语和数据)在ExpandableListView中,术语是Key,数据 - 值。

以下是目前数据源的代码:

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;
}
}

1 个答案:

答案 0 :(得分:2)

创建文件夹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"); 文件中插入数据库中的数据。

var maxListLength = 100000;

通过以下代码调用此方法:

node --max-old-space-size=4096 your_app.js