添加android
insert into info (name,phone) values ('zhangsan','110')sql
删除数据库
delete from info where name='zhangsan'设计模式
修改安全
update info set phone ='999' where name ='zhangsan'mvc
查询ide
select * from info where name='zhangsan'工具
注意:操做数据库 必定要记得把数据库**链接**给关闭掉。 cursor 用完后,也记得关闭
布局
getReadableDatabase() getWriteableDatabase()返回的是同一个数据库的实例, 区别就是数据库返回的时候是否加锁。ui
利用sqlite3工具查看数据库的内容
sqlite3 xxx.db
若是出现中文乱码 须要修改cmd的编码集
65001 utf-8
chcp 65001 更改cmd窗口的编码,默认是gb2312
生成构造方法
/** * 建立一个数据库帮助类,去建立/打开/管理 数据库 * @param context 上下文 * @param name 设置数据库名称 * @param factory CursorFactory 定义一个结果集,游标工厂。 * Cursor 游标(结果集,保存了对数据库的引用,指针) * 设置为null,表示调用默认游标 * @param version 数据库的版本,从1开始 */ public MySqliteDB(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); // TODO Auto-generated constructor stub }
重写onCreate(SQLiteDatabase db)方法 ,数据库第一次被建立的时候调用的方法。适合数据库表结构的初始化.
create table info (_id integer primary key autoincrement, name varchar(20), phone varchar(20))
重写onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion),数据库被更新的时候调用的方法.数据库的版本号增长的时候调用。
/data/data/包名/databases/xxx.db
数据库在建立的时候不指定里面的内容,默认建立只有一张表 metadata保存系统语言环境。
经常使用方法:
boolean move(int offset) Move the cursor by a relative amount, forward or backward, from the current position. boolean moveToFirst() Move the cursor to the first row. boolean moveToLast() Move the cursor to the last row. boolean moveToNext() Move the cursor to the next row. boolean moveToPosition(int position) Move the cursor to an absolute position. boolean moveToPrevious() Move the cursor to the previous row.
方法:绑定参数
//示例 xx.exeSQL("insert into person(name,age) values (?,?)",new Object[]{name,age})
插入数据
long insert(String table, String nullColumnHack, ContentValues values) Convenience method for inserting a row into the database.
删除数据
int delete(String table, String whereClause, String[] whereArgs) Convenience method for deleting rows in the database.
修改数据
int update(String table, ContentValues values, String whereClause, String[] whereArgs) Convenience method for updating rows in the database.
查询数据
Cursor query(boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) Query the given URL, returning a Cursor over the result set. Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy) Query the given table, returning a Cursor over the result set. Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) Query the given table, returning a Cursor over the result set.
优势:安全,高效
保证操做要么同时成功,要么同时失败。 银行转帐
A->B 汇款 A - 100块 B + 100块
事务的模板代码
//开启事务 db.beginTransaction(); try { ... 处理业务逻辑A ... //设置成功(回滚)点,即要么全成功要么全失败 db.setTransactionSuccessful(); ... 处理业务逻辑B ... //设置成功(回滚)点。(同时操做若干个动做时能够设置多个成功点) db.setTransactionSuccessful(); ... } finally { //结束事务 db.endTransaction(); //千万不要忘记干掉这个 db.close(); }
mvc 设计模式。 * model 数据模型 Person * view 视图 ListView * controller 控制器 Adapter 数据适配器,将数据集合以特定的方式组织到界面上
本身理解: ListView + Adapter机制,在须要的时候去建立TextView对象 根据当前屏幕可显示的条数去建立对象,显示过去(以前在屏幕中出现,而后又消失的条目)的条目对象会被从新赋予新值(即显示出的新的条目)
定义一个xml文件 View view = View.inflate(MainActivity.this, R.layout.rl_item, null); 修改view对象里面子孩子显示的内容 view.findViewById();
如何将XML数据(布局文件)转换成View对象? 将一个XML的布局文件填充成一个View对象,以便添加到其余 View容器中。
SimpleAdapter —— 显示图片文本信息 ArrayAdapter —— 只能显示图片
Builder
通知对话框
列表对话框
单选对话框
多选对话框
ProgressDialog * 进度对话框
数据库文件 通常是私有的。 -rw-rw- --- 别的应用程序是没办法访问私有的数据库。
目的: 保证应用程序数据的安全。每一个应用程序都是独立的,不能够操做另一个应用程序数据库的数据。
有一些特殊的需求,须要把本身私有的数据库暴露给别的应用程序让别的应用程序访问。
内容提供者就是作这个事情的。
android:name="com.itheima.db.DaYifuProvider"
android:authorities="com.itheima.db.persondb"
ContentResolver resolver = getContentResolver();