Sqlite的使用和一个简单的书籍管理系统(上)

前端之家收集整理的这篇文章主要介绍了Sqlite的使用和一个简单的书籍管理系统(上)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

虽然说app与网络打交道比较多但是本地存储数据库sqlite还是有它很重要的责任

那来一起学习一下sqlite吧
android的存储方式有如下几种

  1. sharedpreferences
  2. 文件存储
  3. sqlite数据库
  4. contentprovider
  5. 网络

我们来看看sqlite
他通过sqliteOpenHelper这个抽象类;来创建和打开数据库

  1. public class DBHelper extends sqliteOpenHelper {
  2. private static final String DATA_NAME = "jaytang.db";
  3. private static final int version = 1;// 版本
  4. private static final String TAG = "DBHelper";// 版本
  5.  
  6. public DBHelper(Context context) {
  7. super(context,DATA_NAME,null,version);
  8. // TODO Auto-generated constructor stub
  9. }
  10.  
  11. /** * 数据库第一次创建时回调该方法,一般做数据库的初始化操作 建表,添加数据等 sqliteDatabase:增删查改 */
  12. @Override
  13. public void onCreate(sqliteDatabase db) {
  14. // TODO Auto-generated method stub
  15. Log.v(TAG,"创建表");
  16. db.execsql("create table t_book(_id integer primary key autoincrement,name text,price integer)");
  17. }
  18.  
  19. @Override
  20. public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) {
  21. // TODO Auto-generated method stub
  22.  
  23. }
  24.  
  25. }

重写一个DBhelper来继承sqliteOpenHelper
并重写他里面的方法;在oncreate方法里面新建一个表

  1. db.execsql("create table t_book(_id integer primary key autoincrement,price integer)");

这样我们就新建了一个表了
可以在 project Explorer里面查看我们新建的数据库jaytang.db
再来写一个工具类来实现数据库的增删查改方法
通过数据库的辅助类sqliteDatabase来操作

  1. /** * 操作数据库的工具类 * * @author Jay-Tang * */
  2. public class DButils {
  3. public static final String TABLE = "t_book";
  4. public static final String ID = "id";
  5. public static final String NAME = "name";
  6. public static final String PRICE = "price";
  7. private DBHelper dbhelper;
  8.  
  9. public DButils(Context context) {
  10. dbhelper = new DBHelper(context);
  11.  
  12. }
  13.  
  14. // 保存操作
  15. public int save(ContentValues values) {
  16. // 通过数据库的辅助类来获取sqliteDatabase
  17. // 没有设置成全局因为每个操作都要用
  18. sqliteDatabase db = dbhelper.getWritableDatabase();
  19. /** * 插入一条记录 表名 空字段回调:目的在于拼sql语句时不报错 第三个参数是键值对 插入字段的key和value */
  20. long id = db.insert(TABLE,values);
  21. db.close();
  22. return (int) id;
  23. }
  24.  
  25. // 删除操作
  26. public int delete(int id) {
  27. int effectNum = 0;
  28. sqliteDatabase db = dbhelper.getWritableDatabase();
  29. /** * 删除操作 表 条件 条件操作 源码是拼串构成的 */
  30. effectNum = db.delete(TABLE,ID + "=?",new String[] { String.valueOf(id) });
  31. db.close();
  32. return effectNum;
  33. }
  34.  
  35. // 更新要求ContentValues传一个id过来
  36. public int update(ContentValues values) {
  37. // 更新那个位置就传那个位置的id过来
  38. String id = values.getAsString(ID);
  39. // 影响的行数
  40. int effecNums = 0;
  41. sqliteDatabase db = dbhelper.getWritableDatabase();
  42. effecNums = db.update(TABLE,values,ID + "=" + id,null);
  43. db.close();
  44. return effecNums;
  45. }
  46.  
  47. // 查询
  48. public Cursor findcursor() {
  49. sqliteDatabase db = dbhelper.getReadableDatabase();
  50. Cursor cursor = db.query(TABLE,"price disc");
  51. return cursor;
  52. // 假如是返回游标不嫩关闭数据库
  53. }
  54.  
  55. // 查询,返回的是list<map>
  56. public List<Map<String,Object>> find() {
  57. List<Map<String,Object>> data = null;
  58. sqliteDatabase db = dbhelper.getReadableDatabase();
  59. Cursor cursor = db.query(TABLE,"price disc");
  60. // 假如有数据
  61. if (cursor.getCount() > 0) {
  62. data = new ArrayList<Map<String,Object>>();
  63. }
  64. // 遍历游标,把数据存放在list中
  65. while (cursor.moveToNext()) {
  66. Map<String,Object> map = new HashMap<String,Object>();
  67. map.put(ID,cursor.getInt(cursor.getColumnIndex(ID)));
  68. map.put(NAME,cursor.getInt(cursor.getColumnIndex(NAME)));
  69. map.put(PRICE,cursor.getInt(cursor.getColumnIndex(PRICE)));
  70. data.add(map);
  71. }
  72. db.close();
  73. return data;
  74. }
  75.  
  76. public void pay() {
  77.  
  78. }
  79.  
  80. }

上面的工具类DButils能基本实现增删改查的效果
//工具类是可以复用的 所以尽量写好,并且进行单元测试

布局文件如下

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >
  2.  
  3. <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="10dp" android:text="书籍管理系统" android:textColor="#0000ff" android:textSize="22sp" />
  4.  
  5. <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal" >
  6.  
  7. <TextView style="@style/MyTitleStyle" android:text="编号" />
  8.  
  9. <TextView style="@style/MyTitleStyle" android:layout_weight="2" android:text="书名" />
  10.  
  11. <TextView style="@style/MyTitleStyle" android:text="价格" />
  12.  
  13. <ImageView android:layout_width="30dp" android:layout_height="30dp" android:src="@drawable/ic_menu_delete" android:visibility="invisible" />
  14. </LinearLayout>
  15.  
  16. <ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:divider="#0000ff" android:dividerHeight="2dp" >
  17. </ListView>
  18.  
  19. <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" >
  20.  
  21. <EditText android:id="@+id/name_et" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:hint="书名" />
  22.  
  23. <EditText android:id="@+id/price_et" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="价格" android:inputType="number" />
  24.  
  25. <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="add" android:text="添加" />
  26. </LinearLayout>
  27.  
  28. </LinearLayout>

猜你在找的Sqlite相关文章