Android开发 建立数据库




/**
 * @author hhl
 *
 */
public class DBAdapter
{
    public static final String DB_ACTION="db_action";//LogCat
    
    private static final String DB_NAME="people.db";//数据库名
    private static final String DB_TABLE="peopleinfo";//数据库表名
    private static final int    DB_VERSION=1;//数据库版本号
    
    public static final String KEY_ID = "_id";  //表属性ID
    public static final String KEY_NAME = "name";//表属性name
    public static final String KEY_AGE  = "age";//表属性age
    public static final String KEY_HEIGHT = "height";//表属性height
    
    private SQLiteDatabase db ;
    private Context xContext ;
    private DBOpenHelper dbOpenHelper ;
    public DBAdapter(Context context)
    {
        xContext = context ;
    }
    
    /** 空间不够存储的时候设为只读
     * @throws SQLiteException
     */
    public void open() throws SQLiteException
    {
        dbOpenHelper = new DBOpenHelper(xContext, DB_NAME, null,DB_VERSION);
        try
        {
            db = dbOpenHelper.getWritableDatabase();
        }
        catch (SQLiteException e)
        {
            db = dbOpenHelper.getReadableDatabase();
        }
    }
    
    public void close()
    {
        if(db != null)
        {
            db.close();
            db = null;
        }
    }
    /**
     * 向表中添加一条数据
     * @param people
     * @return
     */
    public long insert(People people)
    {
        ContentValues newValues = new ContentValues();
        
        newValues.put(KEY_NAME, people.Name);
        newValues.put(KEY_AGE, people.Age);
        newValues.put(KEY_HEIGHT, people.Height);
        
        return db.insert(DB_TABLE, null, newValues);
    }
    
    /**
     * 删除一条数据
     * @param id
     * @return
     */
    public long deleteOneData(long id)
    {
        return db.delete(DB_TABLE, KEY_ID+"="+id, null );
    }
    /**
     * 删除所有数据
     * @return
     */
    public long deleteAllData()
    {
        return db.delete(DB_TABLE, null, null);
    }
    /**
     * 根据id查询数据的代码
     * @param id
     * @return
     */
    public People[] queryOneData(long id)
    {
        Cursor result = db.query(DB_TABLE, new String[] {KEY_ID,KEY_NAME,KEY_AGE,KEY_HEIGHT}, 
                KEY_ID+"="+id, null, null, null, null);
        return ConvertToPeople(result) ;
    }
    /**
     * 查询全部数据的代码
     * @return
     */
    public People[] queryAllData()
    {
        Cursor result = db.query(DB_TABLE, new String[] {KEY_ID,KEY_NAME,KEY_AGE,KEY_HEIGHT}, 
                null, null, null, null, null);
        return ConvertToPeople(result);
    }
    
    public long updateOneData(long id ,People people)
    {
        ContentValues newValues = new ContentValues();
        
        newValues.put(KEY_NAME, people.Name);
        newValues.put(KEY_AGE, people.Age);
        newValues.put(KEY_HEIGHT, people.Height);
        
        return db.update(DB_TABLE, newValues, KEY_ID+"="+id, null);
    }
    
    private People[] ConvertToPeople(Cursor cursor)
    {
        int resultCounts = cursor.getCount();
        if(resultCounts == 0 || !cursor.moveToFirst())
        {
            return null ;
        }
        People[] peoples = new People[resultCounts];
        Log.i(DB_ACTION, "PeoPle len:"+peoples.length);
        for (int i = 0; i < resultCounts; i++)
        {
            peoples[i] = new People();
            peoples[i].ID = cursor.getInt(0);
            peoples[i].Name = cursor.getString(cursor.getColumnIndex(KEY_NAME));
            peoples[i].Age  = cursor.getInt(cursor.getColumnIndex(KEY_AGE));
            peoples[i].Height = cursor.getFloat(cursor.getColumnIndex(KEY_HEIGHT));
            Log.i(DB_ACTION, "people "+i+"info :"+peoples[i].toString());
            cursor.moveToNext();
        }
        return peoples;
    }
    
    
    /**
     * 静态Helper类,用于建立、更新和打开数据库
     */
    private static class DBOpenHelper extends SQLiteOpenHelper
    {
        /*
         * 手动建库代码
        CREATE TABLE peopleinfo
        (_id integer primary key autoincrement,
        name text not null,
        age integer,
        height float);*/
        private static final String DB_CREATE=
        "CREATE TABLE "+DB_TABLE
        +" ("+KEY_ID+" integer primary key autoincrement, "
        +KEY_NAME+" text not null, "
        +KEY_AGE+" integer,"+
        KEY_HEIGHT+" float);";
        public DBOpenHelper(Context context, String name,
                CursorFactory factory, int version)
        {
            super(context, name, factory, version);
        }


        @Override
        public void onCreate(SQLiteDatabase db)
        {
            db.execSQL(DB_CREATE);
            Log.i(DB_ACTION, "onCreate");
        }


        @Override
        public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion)
        {
            //函数在数据库需要升级时被调用,
            //一般用来删除旧的数据库表,
            //并将数据转移到新版本的数据库表中
            _db.execSQL("DROP TABLE IF EXISTS "+DB_TABLE);
            onCreate(_db);
            Log.i(DB_ACTION, "Upgrade");
        }


    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值