1、创建数据库、表



publicclass SQLiteStorage extends SQLiteOpenHelper {

   privatestaticfinal String name = "purchase_case";

   privatestaticfinalintversion = 1;

 

   public SQLiteStorage(Context context) {

     super(context, name, null, version);

   }

 

   @Override

   publicvoid onCreate(SQLiteDatabase db) {

     db.execSQL("CREATE TABLE IF NOT EXISTS BABY_COLLECT("

          + "_id integerprimary key autoincrement,"

          + "baby_id varchar(30)," + "baby_descvarchar(100),"

          + "baby_namevarchar(30))");

   }

 

   @Override

   publicvoid onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

     this.onCreate(db);

   }

}

 

 

2、实现数据表的基本操作方法

/**

 *

 * Class Name:

 * <p>

 * OrderService

 * </p>

 *

 * Description:

 * <p>

 * 商品列表数据库操作类

 * </p>

 *

 */

publicclass BabyService {

   private SQLiteStorage storageHandler;

 

   public BabyService(Context context) {

     this.storageHandler = new SQLiteStorage(context);

   }

   /***

    * @Description本地数据库用户新增操作

    *

    * @param content

    */

   publicvoid save(Content content) {

 

     SQLiteDatabase database = storageHandler.getWritableDatabase();

     database.beginTransaction();

     try {

        ContentValues contentvalues = new ContentValues();

        contentvalues.put("baby_id", content.getId());

        contentvalues.put("baby_desc", content.getDesc());

        contentvalues.put("baby_name", content.getTitle());

        database.insert("BABY_COLLECT", null, contentvalues);

        database.setTransactionSuccessful();

     } finally {

        database.endTransaction();

        database.close();

     }

   }

   /***

    * @Description本地数据库用户更改操作

    *

    * @param content

    */

   publicvoid update(Content content) {

 

     SQLiteDatabase database = storageHandler.getWritableDatabase();

     database.beginTransaction();

     try {

        ContentValues contentvalues = new ContentValues();

        contentvalues.put("baby_id", content.getId());

        contentvalues.put("baby_desc", content.getDesc());

        contentvalues.put("baby_name", content.getTitle());

        database.update("BABY_COLLECT", contentvalues, "baby_id=?",

             new String[] { String.valueOf(content.getId()) });

        database.setTransactionSuccessful();

     } finally {

        database.endTransaction();

        database.close();

     }

   }

 

   /***

    * @Description根据Id查询方法

    *

    * @param id

    *

    * @return content

    */

   public Content findContentById(String id) {

     Content content = null;

     SQLiteDatabase database = storageHandler.getReadableDatabase();

     Cursor cursor = null;

     cursor = database.query("BABY_COLLECT", new String[] { "baby_id",

           "baby_desc", "baby_name" }, "baby_id=?", new String[] { id },

          null, null, null);

     if (cursor.getCount() > 0) {

        content = new Content();

        while (cursor.moveToNext()) {

          content.setTitle(cursor.getString(2));

          content.setDesc(cursor.getString(1));

          content.setId(Long.parseLong(cursor.getString(0)));

        }

     }

     cursor.close();

     database.close();

     return content;

   }

 

   /***

    * 本地数据库用户信息删除操作

    *

    * @param content

    *

    */

   publicvoid deleteContent(Content content) {

 

     SQLiteDatabase database = storageHandler.getWritableDatabase();

 

     database.delete("BABY_COLLECT", "baby_id=?",

          new String[] { String.valueOf(content.getId()) });

     database.close();

   }

}