数据不是从android SQLite中检索的

时间:2015-03-01 05:10:19

标签: android sqlite

我正在尝试从toast信息中的数组列表中的sq lite数据库中检索数据,但显示

[util.AllProfileElement @ 4b0ae658,util.AllProfileElement @ 4b0a258c,...........]

喜欢我的数据保存在数据库中,我通过Toast消息确认请帮助我摆脱这个 enter code here

数据库助手类:

package database;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

   public class DatabaseHeleper extends SQLiteOpenHelper{

// Database Name
private static final String DATABASE_NAME = "profile.db";
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
//table name
public static final String PROFILE_TABLE_NAME = "profile_table";

// Table Columns names
public static final String COL_PROFILE_ID = "id";
public static final String COL_PROFILE_NAME = "name";
public static final String COL_PROFILEE_GENDER = "gender";
public static final String COL_PROFILE_BLOOD = "blood";
public static final String COL_PROFILE_AGE= "age";
public static final String COL_PROFILE_HEIGHT= "height";
public static final String COL_PROFILE_WEIGHT = "weight";

// table information
private static final String DATABASE_PROFILE_TABLE = "create table "+ PROFILE_TABLE_NAME  + 
        "( " + COL_PROFILE_ID + " integer primary key autoincrement, " + " "            
        +  COL_PROFILE_NAME  + " text not null," + " "
        + COL_PROFILEE_GENDER + " text not null," + " "
        + COL_PROFILE_BLOOD + " text not null," + " "
        + COL_PROFILE_AGE + " text not null," + " "
        + COL_PROFILE_HEIGHT + " text not null," + " "          
        + COL_PROFILE_WEIGHT + " text not null);";



public DatabaseHeleper(Context thiscontext) {
    // TODO Auto-generated constructor stub
    super(thiscontext, DATABASE_NAME, null, DATABASE_VERSION);

}
@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(DATABASE_PROFILE_TABLE);     
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.w(DatabaseHeleper.class.getName(),  "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data");       
    db.execSQL("DROP TABLE IF EXISTS " + DATABASE_PROFILE_TABLE);       
    onCreate(db);
}

}

nd ******数据库源代码:******

enter code here



 package database;

   import java.util.ArrayList;

   import util.AllNameList;
   import util.AllProfileElement;


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

    public class DatabaseSource {

    private SQLiteDatabase mDB;
    private DatabaseHeleper mHelper;




    public DatabaseSource(Context thiscontext) {
        // TODO Auto-generated constructor stub
        mHelper = new DatabaseHeleper(thiscontext);
    }

    public void open() throws SQLException {
        mDB = mHelper.getWritableDatabase();
    }

    public void close() {
        mHelper.close();
    }


    public long insertData(AllProfileElement object) {

        try {
            open();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        ContentValues values = new ContentValues();     

        values.put(DatabaseHeleper.COL_PROFILE_NAME, object.getName());
        values.put( DatabaseHeleper.COL_PROFILE_AGE, object.getAge());
        values.put( DatabaseHeleper .COL_PROFILEE_GENDER, object.getGender());
        values.put( DatabaseHeleper .COL_PROFILE_BLOOD, object.getBlood());     
        values.put( DatabaseHeleper .COL_PROFILE_HEIGHT,object.getHeight());
        values.put( DatabaseHeleper .COL_PROFILE_WEIGHT, object.getWeight());

        long inserted = mDB.insert( DatabaseHeleper. PROFILE_TABLE_NAME, null, values);
        close();

        this.close();

        return inserted;
    }
    //public ArrayList<AllProfileElement> getAllProfile() {
    public ArrayList<AllProfileElement> getAllProfile() 
    {
        //ArrayList<AllProfileElement> allprofile = new ArrayList<AllProfileElement>();
        ArrayList<AllProfileElement> allprofile = new ArrayList<AllProfileElement>();
    /*  try {
            open();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Cursor mCursor = mDB.query(DatabaseHeleper.PROFILE_TABLE_NAME, null, null, null, null,
                null, null);*/

        try {
            open();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Cursor mCursor = mDB.query(DatabaseHeleper.PROFILE_TABLE_NAME, null, null, null, null,
                null, null);



        // looping through all rows and adding to list
        if (mCursor != null && mCursor.getCount() > 0) {
            mCursor.moveToFirst();

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

                int mID = mCursor.getInt(mCursor.getColumnIndex(DatabaseHeleper.COL_PROFILE_ID));
                String name = mCursor.getString(mCursor.getColumnIndex(DatabaseHeleper.COL_PROFILE_NAME));
                String age = mCursor.getString(mCursor.getColumnIndex(DatabaseHeleper.COL_PROFILE_AGE));                        
                String gender = mCursor.getString(mCursor.getColumnIndex(DatabaseHeleper.COL_PROFILEE_GENDER));
                String blood = mCursor.getString(mCursor.getColumnIndex(DatabaseHeleper.COL_PROFILE_BLOOD));
                String height = mCursor.getString(mCursor.getColumnIndex(DatabaseHeleper.COL_PROFILE_HEIGHT));
                String weight = mCursor.getString(mCursor.getColumnIndex(DatabaseHeleper.COL_PROFILE_WEIGHT));
                AllProfileElement mProfile = new AllProfileElement(mID, name,age,gender,blood,height,weight);

                allprofile.add(mProfile);
                mCursor.moveToNext();
            }
        }
        mCursor.close();
        mDB.close();

        // return place list
        return allprofile;
    }

    }

0 个答案:

没有答案