首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从SQLite中获取所有行并作为POST参数传递

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

Stack Overflow用户
提问于 2016-02-09 05:12:30
回答 2查看 692关注 0票数 1

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

代码语言:javascript
复制
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);
			
		}
		
	}

}
代码语言:javascript
复制
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");
	}

}

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-02-09 05:22:52

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

代码语言:javascript
复制
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;
    }

您可以得到如下的响应:

代码语言:javascript
复制
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"));

                }
           }

希望它能帮到你。

票数 0
EN

Stack Overflow用户

发布于 2016-02-09 05:57:46

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

代码语言:javascript
复制
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;
}

您可以在下面获得配置文件的详细信息:

代码语言:javascript
复制
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 

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35284364

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档