将android SQLite文本内容转储到textView中

时间:2014-03-06 02:30:51

标签: java android sql database sqlite

我构建了一个应用程序,到目前为止设法将文本添加到数据库(无法验证但我杀了FC)。但是,当我尝试从数据库读取FC时,因为我不确定如何使用Cursor对象。我阅读了文档,这让我更加困惑。我有一个包含表“记录”和单列“文本”的数据库。虽然我的项目设法将文本输入数据库(通过EditText),但我尝试将所有数据库条目从这个“文本”列返回到textView中。

DB Helper内部类:

private class dbHelper extends SQLiteOpenHelper {

    public dbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        System.out.println("create statement"+SQL_CREATE_ENTRIES);
        try
        {
            db.execSQL(SQL_CREATE_ENTRIES);

        }catch(SQLiteException sql)
        {
            sql.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS records");
        onCreate(db);

    }

    public void addRecord(String t) {

        ContentValues values = new ContentValues(1);
        values.put(RECORDS_COLUMN_TEXTS,t);

        getWritableDatabase().insert(TABLE_NAME, RECORDS_COLUMN_TEXTS, values);
    }

    public Cursor getRecords() {

        return getWritableDatabase().rawQuery("select * from " + TABLE_NAME, null);

    }   

}

在主要课程中:

 private static final int DATABASE_VERSION = 1;
 private static final String DATABASE_NAME = "CSCI598";
 private static final String TABLE_NAME = "records";

 public static final String RECORDS_COLUMN_TEXTS = "texts";

 private dbHelper openHelper;

 private static final String SQL_CREATE_ENTRIES = "CREATE TABLE " + TABLE_NAME + "(" + RECORDS_COLUMN_TEXTS + " TEXT PRIMARY KEY AUTOINCREMENT" + ");"; 


public void onClick(View v) {
    TextView tv=(TextView)findViewById(R.id.textView1);
    EditText et=(EditText)findViewById(R.id.editText1); 

    openHelper = new dbHelper(this);

    switch(v.getId()) {

    case R.id.fetch1:
        Cursor cursor = openHelper.getRecords();
        StringBuffer sb = new StringBuffer();

        cursor.moveToFirst();
        while(cursor.moveToNext()) {
            sb.append(cursor.getString(0) + "\n");
        }
        tv.setText(sb.toString());

        break;

    case R.id.append1:
        String txt=et.getText().toString(); 
        openHelper.addRecord(txt);
        break;

}

logcat:http://pastebin.com/mqUn6g7P

正如你所看到的,我可能正在尝试点击获取按钮的动作非常愚蠢,即。 "openHelper.getRecords().toString()"尝试将所有Cursor转换为string。我怎样才能以最简单的方式交替进行呢?

1 个答案:

答案 0 :(得分:1)

Ok首先,你通过执行此操作从光标获取字符串

Cursor cursor = openHelper.getRecords();
StringBuffer sb = new StringBuffer();
while(cursor.moveToNext()) {
    sb.append(cursor.getString(0) + "\n");
}

然后您可以将其设置为textView

 tv.setText(sb.toString);

这实际上应该有效 。替换代码的这一部分

case R.id.fetch1:
        tv.setText(openHelper.getRecords().toString());
        break;

有了这个

case R.id.fetch1:
Cursor cursor = openHelper.getRecords();
    StringBuffer sb = new StringBuffer();
    while(cursor.moveToNext()) {
        sb.append(cursor.getString(0) + "\n");
    }
tv.setText(sb.toString);
break;

在创建表时,还要删除Text的自动增量 替换这个:

private static final String SQL_CREATE_ENTRIES = "CREATE TABLE " + TABLE_NAME + "(" + RECORDS_COLUMN_TEXTS + " TEXT PRIMARY KEY AUTOINCREMENT" + ");";

有了这个:

private static final String SQL_CREATE_ENTRIES = "CREATE TABLE " + TABLE_NAME + "(" + RECORDS_COLUMN_TEXTS + " TEXT" + ")";

还在构造函数中添加表名而不是数据库名。改变这个:

public dbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);

    }

对此:

 public dbHelper(Context context) {
            super(context, TABLE_NAME, null, DATABASE_VERSION);

        }