[安卓基础]学习第三天

来源:互联网 发布:西安行知中学是83中吗 编辑:程序博客网 时间:2024/06/10 22:11

SQLiteDatabase、Cursor

一、数据库介绍

- sqlite,轻量级数据库- 什么情况下使用数据库? 有大量相似的数据需要存储的时候,

二、数据库创建

定义一个类继承SQLiteOpenHelper
public class MyOpenHelper extends SQLiteOpenHelper {    /*     * context 上下文     * name 数据库的名字     * factory 目的创建cursor对象     * version 版本必须从1开始     * */    public MyOpenHelper(Context context) {        super(context, "elnui.db", null, 1);        // TODO Auto-generated constructor stub    }    @Override    public void onCreate(SQLiteDatabase db) {    }    @Override    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {    }}

三、数据库的oncreat方法和onupgrade方法

/* * Called when the database is created for the first time.  * -- 这个函数特别适合做表结构的初始化,就是写sql语句 * */@Overridepublic void onCreate(SQLiteDatabase db) {    // id一般以下划线开头,_id    db.execSQL("CREATE table info(_id integer primary key autoincrement,name varchar(20))");}/* *  Called when the database needs to be upgraded.  *  -- 这个函数适合做表结构的更新 * */@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {    db.execSQL("alter table info add phone varchar(20)");}

四、使用sql语句对数据库进行增删改查

缺点:    - sql语句容易写错    - 执行sql语句没有返回值,不易于判断是否操作成功优点:    - 多表查询容易
public void add(View v){    // 获取数据库对象    SQLiteDatabase db =  myopenhelper.getWritableDatabase();    //执行sql语句    db.execSQL("insert into info(name,phone) values(?,?)",new Object[]{"M","10086"});    //数据库用完万必须关闭(官方规定)    db.close();}public void del(View v){    // 获取数据库对象    SQLiteDatabase db =  myopenhelper.getWritableDatabase();    //执行sql语句    db.execSQL("delete from info where name=?", new Object[]{"M"});    //数据库用完万必须关闭(官方规定)    db.close();}public void alter(View v){    // 获取数据库对象    SQLiteDatabase db =  myopenhelper.getWritableDatabase();    //执行sql语句    db.execSQL("update info set phone=? where name=?", new Object[]{"10010","M"});    //数据库用完万必须关闭(官方规定)    db.close(); }public void select(View v){    // 获取数据库对象    SQLiteDatabase db =  myopenhelper.getWritableDatabase();    Cursor cursor =  db.rawQuery("select * from info", null);    if(cursor != null && cursor.getCount() > 0){        while(cursor.moveToNext()){            String name = cursor.getString(1);            String phone = cursor.getString(2);            System.out.println("name:" + name + "-" + phone);        }    }    //数据库用完万必须关闭(官方规定)    db.close(); }

4-1

[1] 使用命令行工具sqlite3可以打开数据库[2] 使用dos行命令'chcp 936'改变编码方式,解决中文乱码

五、使用谷歌封装好的api对数据库进行增删改查

优点:    [1] 不需要写复杂的sql语句    [2] 有返回值,方便开发者开发缺点:    [1] 多张表,使用谷歌封装的API不易进行查询
/********************************************************************** *  *                     使用谷歌封装好的额api实现增删改查 *  * ********************************************************************/public void add1(View v){    // 获取数据库对象    SQLiteDatabase db =  myopenhelper.getWritableDatabase();    /*     * table 表名     * ContentValues 内部封装了一个MAP,key对应列的名,     * */    ContentValues cv  =  new ContentValues();    cv.put("name", "女仆酱");    cv.put("phone", "110");    // 底层就是在组装sql语句    long result = db.insert("info", null, cv);    //数据库用完万必须关闭(官方规定)    db.close();    if(result > 0){        Toast.makeText(getApplicationContext(), "success", 1).show();    }else{        Toast.makeText(getApplicationContext(), "faile", 1).show();    }}public void del1(View v){    // 获取数据库对象    SQLiteDatabase db =  myopenhelper.getWritableDatabase();    int del_counts =  db.delete("info", "name=?", new String[]{"女仆酱"});    //数据库用完万必须关闭(官方规定)    db.close();    Toast.makeText(getApplicationContext(), "删除了" + del_counts, 1).show();}public void alter1(View v){    // 获取数据库对象    SQLiteDatabase db =  myopenhelper.getWritableDatabase();    ContentValues cv  =  new ContentValues();    cv.put("phone", "114");    int result = db.update("info", cv, "name=?", new String[]{"女仆酱"});    //数据库用完万必须关闭(官方规定)    db.close();     Toast.makeText(getApplicationContext(), "updated:" + result, 1).show();}public void select1(View v){    // 获取数据库对象    SQLiteDatabase db =  myopenhelper.getWritableDatabase();    Cursor cursor = db.query("info",new String[]{"phone"},"name=?",new String[]{"女仆酱"},null,null,null);    if(cursor != null && cursor.getCount() > 0){        while(cursor.moveToNext()){            String name = cursor.getString(0);            //String phone = cursor.getString(2);            System.out.println("phone:" + name);        }    }}

六、ListView入门

[1] 布局中定义ListView[2] 定义listView的数据适配器[3] 实现baseAdapter的getCount()和getView()

七、ListView的优化

public View getView(int position, View convertView, ViewGroup parent) {    TextView tv = null;    if(convertView == null){        tv = new TextView(MainActivity.this);        System.out.println("New: " + position);    }else{        tv = (TextView)convertView;        System.out.println("Old: " + position);    }       tv.setText("HAHA" + position);    return tv;    }}

八、ListView显示数据的原理

——————>==mvc==
javaweb:m: mode 数据v:view 视图[jsp]c:controller[servlet]Android:m:mode 数据[javabean]v:view [listview]c:adapter
补充:
listview的奇怪现象 ?    以后再使用listview的高的时候,使用match_parent填充父窗体

==不管是什么adapter,作用都是把数据展示到View上==

九、ListView显示复杂页面

线性布局,相对布局都继承自ViewGroup,可以有自己的孩子通过一个打气筒inflate,可以把一个布局转换成一个view对象
view = View.inflate(getApplicationContext(), R.layout.item, null);

十、获取打气筒的常用api

[1] View.inflate(Context context, int resource, ViewGroup root)
 示例:View.inflate(getApplicationContext(), R.layout.item, null)
[2] layoutInflater.from(Context context)
 示例:LayoutInflater.from(getApplicationContext()).inflate(R.layout.item, null)
[3] getSystemService(Context.LAYOUT_INFLATER_SERVICE)
 示例:LayoutInflater inflate = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

十一、数组适配器使用

item.xml
<?xml version="1.0" encoding="utf-8"?><TextView xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/textView1"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="TextView" />
ListView tv =  (ListView)findViewById(R.id.lv);// 创建一个ArrayAdapterArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.item, objects);tv.setAdapter(adapter);

十二、权重

- android:layout_weight="1"- 指定权重后,无需设置layout_weight="0dp"- 一般在线性布局中使用

十三、SimpleAdapter介绍

十四、把数据中的数据查询出来展示到ListView上

十五、今日总结

[1] 创建数据库    定义一个类继承SqliteOpenHelper,    sqliteDatabase;操作数据库    onCreat():当数据库第一次创建的时候调用,特别适合做数据库的初始化工作    onUpgrade():当数据库版本升级时调用[2] 操作数据库    - 传统sql    - 使用google封装的api        insert        delete        query[3] 命令行sqlite3[4] 数据库的事物[5] listview显示数据,需要数据适配器    - BaseAdapter    - ArrayAdapter    - SimpleAdapter[6] 获取打气筒的三种方式
0 0
原创粉丝点击