0

我有以下从 android SQLite 数据库中获取数据

我在android中的功能

public List<String> getMyDetails()
    {
            List<String> dataList = new ArrayList<String>();
    Cursor cursor = myDataBase.rawQuery("SELECT * FROM pic_details WHERE _id=1",null);

if (cursor .moveToFirst()) {

        do {
                String name = cursor.getString(cursor.getColumnIndex("name"));
                dataList.add(name);
        } while (cursor.moveToNext());
    }

return dataList;

}

在这样的活动调用中

List<String> piclist= dbHelper. getMyDetails();

它返回候选人的姓名。但我想在 PHP、MySQL 中返回表格的整个列

$sql=mysql_query("SELECT * FROM pic_details WHERE _id=1");
$i=0;
$out=array();

while($t=mysql_fetch_array($sql)){

$out[$i]['name']=$t['name'];
$out[$i]['file']=$t['file'];
$out[$i]['age']=$t['age'];
$out[$i]['date_time']=$t['date_time'];

$i++;
}

return $out;

这样我就可以根据需要取出 $out。

谁能帮帮我吗 ?提前致谢

4

2 回答 2

2

正如您在php code问题中提供的那样,它不是单个column. 这是一个row data。因此,您基本上想要的是整个行信息。为了获得整个行的详细信息,您必须执行类似的操作。

 String name = cursor.getString(cursor.getColumnIndex("name"));
 String address = cursor.getString(cursor.getColumnIndex("address"));

因此,generic将是:

datatype fetchedData = cursor.getterofthatparticulardatatype(cursor.getColumnIndex(columnName));

编辑:

要么POJO class像其他回答者所说的那样使用,要么这样:

public List<HashMap<String, String>> getMyDetails()
    {

 List<HashMap<String, String>> datalist = new  ArrayList<HashMap<String, String>>();

    Cursor cursor = myDataBase.rawQuery("SELECT * FROM pic_details WHERE _id=1",null);

if (cursor.moveToFirst()) {

        do {
                HashMap<String, String> hashmap = new HashMap<String, String>();

                String name = cursor.getString(cursor.getColumnIndex("name"));
                String story = cursor.getString(cursor.getColumnIndex("story"));

                hashmap.put("nameKey",name);
                hashmap.put("storyKey",story);

                dataList.add(hashmap);
        } while (cursor.moveToNext());
    }

return dataList;

}

您应该检索的方式:

List<HashMap<String, String>> piclist= dbHelper. getMyDetails();

for(HashMap hashmap : piclist)
{
         String name = hashmap.get("nameKey");
         String story = hashmap.get("storyKey");
         Log.d("name,story",name+", "+story);
}
于 2013-05-16T10:12:33.397 回答
0

首先创建 Java 类。

public class Person {
    private String image;
    private String name;

    public Person(String image, String name) {
        super();
        this.image = image;
        this.name = name;

    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Then create ArrayList of person class

Public List<Person> getMyDetails()
    {
            List<Person> dataList = new ArrayList<Person>();

    Cursor cursor = myDataBase.rawQuery("SELECT * FROM pic_details WHERE _id=1",null);

if (cursor .moveToFirst()) {

        do {
                String name = cursor.getString(cursor.getColumnIndex("name"));
              person = new Person(image, name)
                dataList.add(person);
        } while (cursor.moveToNext());
    }

return dataList;

}

Then use this arraylist in you activity class.
于 2013-05-16T08:32:12.763 回答