SQLite
- SQLite数据库实现结构化数据存储。
- SQLite是一个嵌入式数据库引擎,目的在于为内存等资源有限的设备 。
- SQLite是基于C语言设计开发的开源数据库,最大支持2048G数据。
- SQLite特征 :轻量级 、独立 、便于管理和维护 、可移植性 、语言无关 、事务性 。
- SQLite操作简单,且数据库功能强大,提供了基本数据库、表以及记录的操作包括- 数据库创建、数据库删除、表创建、表删除、记录插入、记录删除、记录更新、记录查询。
SQLiteDatabase常用的方法:
方法 | 方法描述 |
---|---|
openOrCreateDatabase(String path,SQLiteDatabase.CursorFactory factory) | 打开或创建数据库 |
openDatabase(String path, SQLiteDatabase.CursorFactory factory, int flags) | 打开指定的数据库 |
close() | 关闭数据库 |
insert(String table,String nullColumnHack,ContentValues values) | 插入一条记录 |
delete(String table,String whereClause,String[] whereArgs) | 删除一条记录 |
query (boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) | 查询记录 |
update(String table,ContentValues value,String whereClause, String[] whereArgs) | 修改记录 |
execSQL(String sql) | 执行一条SQL语句 |
SQLite之增删改查
一、创建SQLiteHelper 继承 SQLiteOpenHelper
onCreate创建表
二、获取SQLiteDatabase对象
SQLiteHelper sqLiteHelper = new SQLiteHelper(SQLiteActivity.this);
SQLiteDatabase database = sqLiteHelper.getReadableDatabase();
三、增
ContentValues contentValues= new ContentValues();
contentValues.put("id",1);
contentValues.put("name","张先生");
contentValues.put("age",18);
//参数1:表名 参数2:null 参数3:contentValues
database.insert("user",null,contentValues);
Log.d("TAG","====insert====");
四、改
ContentValues contentValues = new ContentValues();
contentValues.put("age",88);
***//第一个参数:表名 第二个参数:修改的内容 第三个参数:修改的条件***
***第四个参数:条件值***
database.update("user",contentValues,"name=?",new String[]{"张先生"});
Log.d("TAG","====update====");
五、删除
***//第一个参数:表名 第二参数:条件 第三个参数:条件值***
database.delete("user","name=?",new String[]{"张先生"});
Log.d("TAG","====delete====");
六、查询
//第一个参数:表名 第二个参数:类名 ,后边五个参数null
Cursor cursor = database.query("user", new String[]{"id", "name", "age"}, null, null, null, null, null);
while (cursor.moveToNext()){
int id = cursor.getInt(0);
String name = cursor.getString(1);
int age = cursor.getInt(2);
Log.d("TAG","id=="+id+",name=="+name+",age=="+age);
}
cursor.close();
Log.d("TAG","====query====");
String selection = "name=? and age=?";
String[] selectionArgs = new String[]{ "张先生","88"};
一、
1、点击添加按钮弹出对话框
2、对话框可以输入 id,name,age。
3、点击对话框的确认按钮进行数据库添加数据
AlertDialog.Builder builder = new AlertDialog.Builder(MySqliteActivity.this);
// builder.setMessage()
//将xml转换成view
View view;
view.findViewById()
builder.setView(view);
Content Provider 准备工作
- ContentProvider是Android提供的应用组件,定义在android.content包下面,通过这个组件可访问Android提供的应用数据如联系人列表。
ContentProvider的常用方法:
-
1、添加权限读取联系人的权限
-
2、使用时,动态权限申请
-
3、查询