android SQLite数据的使用
来源:互联网 发布:js数据类型转换 编辑:程序博客网 时间:2024/06/05 06:21
SQLite数据库是一个轻量级的数据库 、非常小 、 移植性好、效率高、可靠 ,功能齐全、嵌入式设备因为受到硬件条件的限制所以非常适合使用 SQLite 数据库,android提供了一系列操作SQLite数据库的类
1.SQLiteOpenHelper类,该类主要是创建和更新数据库使用
public class DBHelper extends SQLiteOpenHelper { private static DBHelper gInstance; private static final String DATABASE_NAME = "xy.db"; private DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { this(context, name, factory, version, null); } //name 数据库名、factory 用来创建对象游标,或者默认为null、 version数据版本 、errorHandler 当sqlite报告一个数据库毁坏错误时,DatabaseErrorHandler会被使用 private DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, DatabaseErrorHandler errorHandler) { super(context, name, factory, version, errorHandler); } public static synchronized DBHelper getInstance(Context context, SQLiteDatabase.CursorFactory factory, int version){ if(gInstance == null){ gInstance = new DBHelper(context, DATABASE_NAME, factory, version); } return gInstance; } public static synchronized DBHelper getInstance(Context context, SQLiteDatabase.CursorFactory factory, int version, DatabaseErrorHandler errorHandler){ if(gInstance == null){ gInstance = new DBHelper(context, DATABASE_NAME, factory, version, errorHandler); } return gInstance; } @Override//该方法只在创建时,被调用一次 public void onCreate(SQLiteDatabase db) { //SQLiteDatabase 是操作数据库的增删改查的类 //此处创建数据库 db.execSQL("create table table1(_id integer, name varchar)"); } @Override//当数据库的版本newVersion比oldVersion高时,此方法会本调用 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { //oldVersion 上一次数据库的版本号,newVersion 新数据库的版本号 }}
该类有两个获取数据库引用的方法,getWritableDatabase()和getReadableDatabase()。getWritableDatabase() 方法以写读方式打开数据库,一旦数据库的磁盘空间满了,数据库就只能读而不能写,倘若使用getWritableDatabase()打开数据库就会出错。getReadableDatabase()方法先以读写方式打开数据库,如果数据库的磁盘空间满了,就会打开失败,当打开失败后会继续尝试以只读方式打开数据库
下面介绍一个小实例配合SQLiteDatabase类
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/add" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="添加数据"/> <Button android:id="@+id/delete" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="删数据"/> <Button android:id="@+id/modify" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="改数据"/> <Button android:id="@+id/find" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="查数据"/></LinearLayout>
public class MainActivity extends AppCompatActivity { private DBHelper mDbHelper; private SQLiteDatabase mDatabase; private int mId; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mDbHelper = DBHelper.getInstance(MainActivity.this, null, 1); mDatabase = mDbHelper.getReadableDatabase(); MyOnClickListener listener = new MyOnClickListener(); findViewById(R.id.add).setOnClickListener(listener); findViewById(R.id.delete).setOnClickListener(listener); findViewById(R.id.modify).setOnClickListener(listener); findViewById(R.id.find).setOnClickListener(listener); } class MyOnClickListener implements View.OnClickListener{ @Override public void onClick(View v) { switch (v.getId()){ case R.id.add: add(); break; case R.id.delete: delete(); break; case R.id.modify: modify(); break; case R.id.find: find(); break; } } } private void find() { Cursor c = mDatabase.query("table1", null, null, null, null, null, null); if(c != null){ while (c.moveToNext()){ int id = c.getInt(c.getColumnIndex("_id")); String name = c.getString(c.getColumnIndex("name")); System.out.println("_id = " + id + " name = " + name); } c.close(); } } private void modify() { ContentValues values = new ContentValues(); values.put("_id", mId); values.put("name", "xiao" + mId); int count = mDatabase.update("table1", values, "_id = ?", new String[]{String.valueOf(mId)}); System.out.println("修改了" + count + "条"); } private void delete() { int count = mDatabase.delete("table1", "_id = ?", new String[]{String.valueOf(mId)}); --mId; System.out.println("删除了" + count + "条"); } private void add() { ContentValues values = new ContentValues(); values.put("_id", mId); values.put("name", "wurui" + mId); long id = mDatabase.insert("table1", null, values); System.out.println("成功添加一条 id为" + id); ++mId; } @Override protected void onDestroy() { if(mDatabase != null){ mDatabase.close(); } if(mDbHelper != null){ mDbHelper.close(); } super.onDestroy(); }}
代码相对于简单,在此不过多介绍了,可以看到,增删改查,都是通过SQLiteDatabase类完成的
0 0
- android SQLite数据的使用
- android数据存储之SQLite的使用
- android使用sqlite存储数据
- Android 数据存储 &SQLite使用,
- Android使用SQLite保存数据
- Android:使用SQLite存取数据
- Android使用SQLite存储数据
- android SQlite 的使用
- android的sqlite使用
- android sqlite 的使用
- android Sqlite 的使用
- Android SQLite的使用
- android SQLite 的使用
- Android SQLite的使用
- Android:Sqlite的使用
- [Android] SQlite的使用
- android SQLite的使用
- android SQLite的使用
- 1064. 朋友数
- [FFT] 快速傅里叶变换学习笔记
- NOIP2013 Day1
- Nginx配置反向代理安装和配置-初探
- 【数据结构-堆排序】堆排序 Heap Sort
- android SQLite数据的使用
- videoView播放视频,简易视频播放器自己写
- 关于支付宝sdk出现Fatal error问题解决办法
- ionic2框架 图片选择与上传(支持ios,android)
- 链表算法之环形链表插值
- Android开发之ThreadLocal的使用
- C++上机作业5实验
- react 学习笔记
- 第121课:Spark Streaming性能优化:通过摄像头图像处理案例来说明Spark流处理性能评估新方法及性能调优参数调试