如何在android中读取整个sqlite数据库

时间:2012-04-05 08:43:15

标签: android sql

请帮助我以下,我有一个包含字符串的数据库(例如fotonames)。 我想把整个fotonames列读成一个arraylist。 我怎么能这样做?

2 个答案:

答案 0 :(得分:2)

public Cursor getfotoname() throws SQLException
{
 String getRT = "SELECT fotonames from "+ TABLE_NAME+";";
 Cursor mCur = sqldb.rawQuery(getRT, null);
 return mCur;
}

现在打电话

Cursor mCursor=null;

mCursor= DatabaseObject.getfotoname();

ArrayList<WhateverTypeYouWant> mArrayList = new ArrayList<WhateverTypeYouWant>();
for(mCursor.moveToFirst(); mCursor.moveToNext(); mCursor.isAfterLast()) {
// The Cursor is now set to the right position
mArrayList.add(mCursor.getWhateverTypeYouWant(WHATEVER_COLUMN_INDEX_YOU_WANT));
}

答案 1 :(得分:1)

看看这个教程会对你有所帮助 http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/

您可以在这里学习如何从一列中获取所有值 http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/3/#getrowasarray

或其他方式

像这样的火灾查询

 public Cursor columnValues() throws SQLException{
            // TODO Auto-generated method stub


            Cursor mCursor = db.query(Course_Info_Table,
                              new String[] {Column1 , column2 },
                                            null,null, null, null, null);




                    //Cursor mCursor = mDb.rawQuery("Select",null);
                    if (mCursor != null)
                    {
                    mCursor.moveToFirst();
                    }

                    return mCursor;



        }

并像

一样接收它
    ArrayList<String> list1  = new ArrayList<String>();

    ArrayList<String> list12 = new ArrayList<String>();

cursor = dbm.columnValueofCourse();
        cursor.moveToFirst();
        startManagingCursor(cursor);

        for (int i = 0; i < cursor.getCount(); i++) {

        String  reciv = cursor.getString(cursor
                    .getColumnIndex("column1"));

String      reciv3 = cursor.getString(cursor
                    .getColumnIndex("column2"));



            list1.add(reciv);
            list2.add(reciv3);
            cursor.moveToNext();
        }