Android学习之SQLite浅析

  Android作为运行在手机上的操作系统,它的很多性能因为受限于手机自身的条件,所以在很多选择上考虑的更多是占用空间更小,效率相对较高。所以在数据库选择方面,Android选择SQLite作为内嵌的数据库,SQLite这款数据库的特点是占用空间较小,并且具有完整的关系数据库的特点,所以受众多嵌入式设备的青睐。下面我们言归正传,说一下Android平台下的应用。在Android的类库中,提供众多类来对数据库进行操作,下面我列举一下:

  SQLiteClosable SQLiteCursor SQLiteDatabase SQLiteOpenHelper SQLiteProgram SQLiteQuery SQLiteQueryBuilder SQLiteStatement

对于大家所关心的如何使用SQLite数据库,Android提供了SQLiteOpenHelper这个类,使用者只需继承这个类,并复写这个类的方法就可以轻松使用数据库,好,废话不多说直接上代码:

  //通过继承SQLiteOpenHelper来对数据库进行操作

   public class SQLHelper extends SQLiteOpenHelper {

   //定义数据库版本

    private static int VERSION = 1;

   //三个构造函数

    public SQLHelper(Context context, String name, CursorFactory factory,
            int version) {
        super(context, name, factory, version);
        // TODO Auto-generated constructor stub
    }
    
    public SQLHelper(Context context, String name, CursorFactory factory) {
        // TODO Auto-generated constructor stub
        this(context, name, factory, VERSION);
    }
    public SQLHelper(Context context, String name) {
        // TODO Auto-generated constructor stub
        this(context, name, null);
    }
   //Oncreate方法,负责创建一个数据库,它是一个回调函数
    @Override
    public void onCreate(SQLiteDatabase arg0) {
        // TODO Auto-generated method stub
        System.out.println("正在创建数据库");
        arg0.execSQL("create table myfirst (id int primary key,name varchar(20))");
    }
 //onUpgrade更新数据库时调用
    @Override
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
        // TODO Auto-generated method stub
        VERSION = VERSION+1;
        System.out.println("正在更新数据库");
    }
    
}
    private Button createDataBase = null;
    private Button updateDataBase = null;
    private Button insertButton = null;
    private Button updateButton = null;
    private Button searchButton = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 设置按钮属性
        createDataBase = (Button) findViewById(R.id.createdatabase);
        updateDataBase = (Button) findViewById(R.id.updatedatabase);
        insertButton = (Button) findViewById(R.id.insertdata);
        updateButton = (Button) findViewById(R.id.updatedate);
        searchButton = (Button) findViewById(R.id.searchdate);
        // 添加监听事件
        createDataBase.setOnClickListener(new createDataBaseListener());
        updateDataBase.setOnClickListener(new updateDataBaseListener());
        insertButton.setOnClickListener(new insertDataListener());
        updateButton.setOnClickListener(new updateDataListener());
        searchButton.setOnClickListener(new searchDataListener());
    }
   //为创建数据库所设置的监听器
    class createDataBaseListener implements OnClickListener {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            //创建一个SQLHelper对象
            SQLHelper sh = new SQLHelper(MainActivity.this, "mydatabase");

           //此行必须存在,它负责调用Oncreate方法

            //获得一个可写的数据库

            sh.getReadableDatabase();
            System.out.println(sh.getReadableDatabase());
        }

    }

    class updateDataBaseListener implements OnClickListener {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

           //与上一个监听器不同的地方是,通过不同的构造函数生成SQLHelper对象

            SQLHelper sh = new SQLHelper(MainActivity.this, "mydatabase", null,2);

          //此行必须存在,它负责调用Onupdate方法

           //获得一个可读的数据库
            sh.getReadableDatabase();
            System.out.println(sh.getReadableDatabase());
        }

    }

    class insertDataListener implements OnClickListener {

        @Override
        public void onClick(View v) {
             // TODO Auto-generated method stub

           // 创建一个ContentValues来存储数据

           ContentValues cv = new ContentValues();
            cv.put("name", "风清扬");
            SQLHelper sh = new SQLHelper(MainActivity.this, "mydatabase");

           //获得一个可写的数据库

            SQLiteDatabase sd = sh.getWritableDatabase();
            sd.insert("myfirst", null, cv);
        }

    }

    class updateDataListener implements OnClickListener {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            SQLHelper sh = new SQLHelper(MainActivity.this, "mydatabase");
            SQLiteDatabase sd = sh.getWritableDatabase();
            ContentValues cv = new ContentValues();
            cv.put("name", "yangminglei");

            //此行的4个参数的意义分别为  数据库表名   所要修改的数据   修改数据的ID   修改数据的ID值
            sd.update("myfirst", cv, "id = ?", new String[]{"1"});
        }

    }

    class searchDataListener implements OnClickListener {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            SQLHelper sh = new SQLHelper(MainActivity.this, "mydatabase");
            SQLiteDatabase sd = sh.getWritableDatabase();

            //和JDBC中ResultSet对象的用法相似,只不过需要注意的是getString方法不是要的列名,而是列的索引值,还需要通过getColumnIndex方法来获取索引值

            Cursor c = sd.query("myfirst", new String[]{"id","name"}, null,null, null, null, null);
            while(c.moveToNext()){
                String name = c.getString(c.getColumnIndex("name"));
                String id = c.getString(c.getColumnIndex("id"));
                System.out.println("ID:"+id+"   Name:"+name);
            }
        }

    }

 

 

      
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值