从SQLite获取所有行并作为POST参数传递

时间:2016-02-09 05:12:31

标签: android sqlite android-volley

我有一个SQLite db有2个项目。我可以使用HashMap获取第一行并将其保存在字符串中以显示值。但是我希望得到所有行并将它们作为参数传递给Volley POST Stringrequest。要检查获取结果的输出,我将String值添加到textview,但textview仅显示forst行。那么如何获取所有行?



Activity:

public class NewActivity extends Activity{
	
	TextView name, mobile;
	private SQLiteHandler db;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.newactivity);
		
		name = (TextView)findViewById(R.id.name);
		mobile = (TextView)findViewById(R.id.mobile);
		db = new SQLiteHandler(getApplicationContext());
	}
	
	public void show(View v){  //on button click
		
		HashMap<String, String> pro = db.getProfDetails();
		for( int i=0; i<pro.size();i++){
			String proName = pro.get("name"); 
			String proMobile = pro.get("mobile"); 
			name.setText(proName);
			mobile.setText(proMobile);
			
		}
		
	}

}
&#13;
public class SQLiteHandler extends SQLiteOpenHelper {

	private static final String TAG = SQLiteHandler.class.getSimpleName();

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

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

	// Profile Settings table name
	private static final String TABLE_PROF = "prof";

	// Profile Settings information names
	private static final String KEY_ID = "id";
	private static final String KEY_NAME = "name";
	private static final String KEY_MOBILE = "mobile";


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

	// Creating Tables
	@Override
	public void onCreate(SQLiteDatabase db) {

		String CREATE_PROF_TABLE = "CREATE TABLE " + TABLE_PROF + "("+KEY_ID+" INTEGER PRIMARY KEY, "+KEY_NAME+" TEXT,  "+KEY_MOBILE+" TEXT" + ")";

		db.execSQL(CREATE_PROF_TABLE);

		Log.d(TAG, "Database tables created");
	}

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

		// Create tables again
		onCreate(db);
	}


	/**
	 * Storing Prof_settings details in database
	 * */
	public void addProfile(String name, String mobile){

		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();

		values.put(KEY_NAME, name);
		values.put(KEY_MOBILE, mobile);

		// Inserting Row
		//long id = db.update(TABLE_PROF, values, KEY_ID, null);  //for update
		//db.execSQL("delete from "+ TABLE_PROF); //clears 1st row
		long id = db.insert(TABLE_PROF, null, values); // insert to 1st row
		db.close(); // Closing database connection

		Log.d(TAG, "New profile settings inserted into sqlite: " + id);

	}




	/**
	 * Getting Profile Settings data from database
	 * */
	public HashMap<String, String> getProfDetails() {
		HashMap<String, String> pro = new HashMap<String, String>();
		String selectQuery = "SELECT  * FROM " + TABLE_PROF;

		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);
		// Move to first row
		cursor.moveToFirst();
		if (cursor.getCount() > 0) {
			pro.put("name", cursor.getString(1));
			pro.put("mobile", cursor.getString(2));
		}
		cursor.close();
		db.close();
		// return log details
		Log.d(TAG, "Fetching profile details from Sqlite: " + pro.toString());

		return pro;
	}

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

		Log.d(TAG, "Deleted all profile info from sqlite");
	}

}
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

以下是从表中获取所有记录的DB方法:

public ArrayList<HashMap<String, String>> getProfDetails()
    {
        ArrayList<HashMap<String, String>> array_list = new ArrayList<HashMap<String, String>>();

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor res = db.rawQuery("SELECT * " + " FROM " + PROFILE_TABLE_NAME + " GROUP BY " + PRFOILE_COLUMN_USERNAME + " ORDER BY " + PRFOILE_COLUMN_USERNAME + " COLLATE NOCASE;", null);
        res.moveToFirst();

        while (res.isAfterLast() == false)
        {
            hashmap = new HashMap<String, String>();
            hashmap.put("name", res.getString(res.getColumnIndex(PROFILE_COLUMN_USERNAME)));
            hashmap.put("mobile", res.getString(res.getColumnIndex(PROFILE_COLUMN_MOBILENUMBER)));

            array_list.add(hashmap);
            res.moveToNext();
        }
        return array_list;
    }

您可以得到如下答案:

ArrayList<HashMap<String, String>> arrayList = new ArrayList<HashMap<String, String>>();
arrayList = databaseHandler.getProfDetails();

if (arrayList.size() != 0)
            {
                for (int i = 0; i < arrayList.size(); i++)
                {
                profilenameList.add(arrayList.get(i).get("name"));     
                profilemobilenumberlist.add(arrayList.get(i).get("mobile"));

                }
           }

希望它会对你有所帮助。

答案 1 :(得分:0)

请检查以下代码中的小更改,它将起作用:

public ArrayList<HashMap<String, String>> getProfDetails()
{
    ArrayList<HashMap<String, String>> array_list = new  ArrayList<HashMap<String, String>>();

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = database.rawQuery("SELECT *  FROM " + TABLE_PROF, null);

 if(cursor!=null)
 {
    if (cursor.moveToFirst()) {
      do {
            hashmap = new HashMap<String, String>();
            hashmap.put("name", res.getString(res.getColumnIndex(KEY_NAME)));
            hashmap.put("mobile", res.getString(res.getColumnIndex(KEY_MOBILE)));

            array_list.add(hashmap);

        } while (cursor.moveToNext());
     } 

}
    return array_list;
}

您可以获得以下详细信息:

ArrayList<HashMap<String, String>> arrayList = new ArrayList<HashMap<String, String>>();
arrayList = databaseHandler.getProfDetails();

if(arrayList !=null && arrayList.size()>0){
{
  //extract  the name and mobile number from arrayList 

}