我一直在研究一些sqlite数据库,我已经创建了一个表,并且已成功插入数据,如何获取数据并在textview中显示?
public class EmployeeTable{
public static final String KEY_NAME = "emp_name";
public static final String KEY_DESIGNATION = "emp_designation";
public static final String KEY_ROWID = "_id";
private static final String TAG = "EmployeeTable";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static final String DATABASE_NAME = "employee_database";
private static final String DATABASE_TABLE = "employee";
private static final int DATABASE_VERSION = 3;
/**
* Database creation sql statement
*/
private static final String DATABASE_CREATE =
"create table " + DATABASE_TABLE + " (" + KEY_ROWID + " integer primary key autoincrement, "
+ KEY_NAME +" text not null, " + KEY_DESIGNATION + " text not null);";
private final Context mCtx;
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.i(TAG, "Creating DataBase: " + DATABASE_CREATE);
db.execSQL(DATABASE_CREATE);
}`
关注我的主要课程:
public class SqliteDBActivity extends Activity {
/** Called when the activity is first created. */
private static final String TAG = "EmployeeTable";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EmployeeTable employeeTable = new EmployeeTable(this);
employeeTable.open();
employeeTable.createEmployee("Prashant Thakkar", "Tech Lead");
employeeTable.createEmployee("Pranay anand", "Tech Lead");
employeeTable.createEmployee("rajeev kumar", "Tech Lead");
Log.i(TAG, "Fetching record...");
employeeTable.close();
}
}
答案 0 :(得分:3)
this.db = openHelper.getWritableDatabase();
Cursor cursor = this.db.query(constantValues.TABLE_NAME, new String[] { "emailid" },null, null, null, null, null);
if (cursor.moveToFirst()) {
do {
String emailid=cursor.getString(0); // Here you can get data from table and stored in string if it has only one string.
textview.setText(emailid);
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
if(db!=null)
{
db.close();
}
}
如果它有多个数据并存储在多个textview中。你必须存储它列表,然后从列表中获取数据并设置为textview,如下所示,
List<String> list = new ArrayList<String>();
Cursor cursor = this.db.query(constantValues.TABLE_NAME, new String[] { "emailid"},null, null, null, null, null); // here emailid is the field name in the table and contantValues.TABLE_NAME is the table name
if (cursor.moveToFirst()) {
do {
list.add(cursor.getString(0));
} while (cursor.moveToNext());
}
然后从列表中获取数据并设置为textview。有关进一步参考,请查看this链接
答案 1 :(得分:1)
private SQLiteDatabase database; DatabaseHelperClass dbHelper = new ExternalDbOpenHelper(this, nameofyourdatabase);
database = dbHelper.openDataBase(); String qry = "select * from table WHERE yourcolumn"; String stringfortextview;
Cursor c = database.rawQuery(qry, null);
c.moveToFirst();
if (!c.isAfterLast())
{
do
{
stringfortextview = c.getString(4);
}
while (c.moveToNext());
}
c.close(); TextView tv = (TextView)findViewById(R.id.textView); tv.setText(stringfortextview);