如何通过游标读取SQLite并在textview中显示

时间:2015-01-25 05:45:31

标签: java android sqlite textview android-cursor

如何在HomeAcitivity TextView中阅读和显示“email”和“address”。

数据库设计为只存储1行数据。

DatabaseHandler.java

package com.example.androidjhfong.library;

import java.util.HashMap;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHandler extends SQLiteOpenHelper {

    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "android_database";

    // Table name
    private static final String TABLE_LOGIN = "login";

    // Login Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "name";
    private static final String KEY_EMAIL = "email";
    private static final String KEY_UID = "uid";
    private static final String KEY_ADDRESS = "address";
    private static final String KEY_PHONE = "phone";
    private static final String KEY_CREATED_AT = "created_at";


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

    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_LOGIN + "("
                + KEY_ID + " INTEGER PRIMARY KEY," 
                + KEY_NAME + " TEXT,"
                + KEY_EMAIL + " TEXT UNIQUE,"
                + KEY_UID + " TEXT,"
                + KEY_ADDRESS + " TEXT,"
                + KEY_PHONE + " TEXT,"
                + KEY_CREATED_AT + " TEXT" + ")";
        db.execSQL(CREATE_LOGIN_TABLE);


    }

    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOGIN);

        // Create tables again
        onCreate(db);
    }

    /**
     * Storing user details in database
     * */
    public void addUser(String name, String email, String uid, String address, String phone, String created_at) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_NAME, name); // Name
        values.put(KEY_EMAIL, email); // Email
        values.put(KEY_UID, uid); // Email
        values.put(KEY_ADDRESS, address); // Address
        values.put(KEY_PHONE, phone); // phone
        values.put(KEY_CREATED_AT, created_at); // Created At

        // Inserting Row
        db.insert(TABLE_LOGIN, null, values);
        db.close(); // Closing database connection
    }

    /**
     * Getting user data from database
     * */
    public HashMap<String, String> getUserDetails(){
        HashMap<String,String> user = new HashMap<String,String>();
        String selectQuery = "SELECT  * FROM " + TABLE_LOGIN;

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        // Move to first row
        cursor.moveToFirst();
        if(cursor.getCount() > 0){
            user.put("name", cursor.getString(1));
            user.put("email", cursor.getString(2));
            user.put("uid", cursor.getString(3));
            user.put("address", cursor.getString(4));
            user.put("phone", cursor.getString(5));
            user.put("created_at", cursor.getString(6));

        }
        cursor.close();
        db.close();
        // return user
        return user;
    }

    /**
     * Getting user login status
     * return true if rows are there in table
     * */
    public int getRowCount() {
        String countQuery = "SELECT  * FROM " + TABLE_LOGIN;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        int rowCount = cursor.getCount();
        db.close();
        cursor.close();

        // return row count
        return rowCount;
    }

    /**
     * Re crate database
     * Delete all tables and create them again
     * */
    public void resetTables(){
        SQLiteDatabase db = this.getWritableDatabase();
        // Delete All Rows
        db.delete(TABLE_LOGIN, null, null);
        db.close();
    }

    /**
     * Getting product status
     **/

    public String getData() {

        String username;
        String getdata = "SELECT  name FROM " + TABLE_LOGIN;
        //String[] columns = new String[]{ KEY_ID, KEY_NAME, KEY_ADDRESS, KEY_PHONE};
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(getdata, null);
        cursor.moveToFirst();
        username=cursor.getString(cursor.getColumnIndex("name"));
        cursor.close();
        db.close();
        return username;
    }
}

如何正确显示检索数据并在HomeActivity中显示?

HomeActivity

public class HomeActivity extends Activity implements OnItemSelectedListener{

TextView inputname
TextView inputemail;
TextView inputaddress;

Button btnPurchase;

// JSON Response node names
private static String KEY_SUCCESS = "success";
private static String KEY_ADDRESS = "address";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.Home);

    inputname= (TextView) findViewById(R.id.qrpurchaseitem);
    inputaddress = (TextView) findViewById(R.id.qrtextpurchaseaddress);
    inputemail = (TextView) findViewById(R.id.qrtextpurchasecomment);


    DatabaseHandler db = new DatabaseHandler(getApplicationContext());
    HashMap<String, String> key = db.getUserDetails();

      String text= DatabaseHandler.getUserDetails();
}

1 个答案:

答案 0 :(得分:0)

HomeActivty中添加此行

String email=db.getEmail()
String address=db.getAddress();

现在,您可以在Database Handler类中使用以下方法,只需从下面的方法

更改变量名称
 public String getStartBal() {
    Cursor cr=ourDatabase.rawQuery("SELECT "+KEY_START_BAL+" FROM "+DB_TABLE,null);
    String sum="";
    for(cr.moveToFirst();!cr.isAfterLast();cr.moveToNext()){
        sum=cr.getString(cr.getColumnIndex(KEY_START_BAL));
    }
    cr.close();
    return sum;
}