@OnClick({R.ID.click1,R.ID.click2,R.ID.click3,R.ID.click4}) public voID onVIEwClicked(VIEw vIEw) { mDb = mMyOpenHelper.getWritableDatabase(); switch (vIEw.getID()) { case R.ID.click1: mDb.execsql("insert into loginin(name,phone) values(‘a‘,‘18634757219‘)"); break; case R.ID.click2: mDb.execsql("delete from loginin where name=‘a‘"); break; case R.ID.click3: mDb.execsql("update loginin set phone = ‘18634757219‘ where name=‘a‘"); break; case R.ID.click4: Cursor cursor = mDb.rawquery("select * from loginin",null); if (cursor!=null){ while(cursor.movetoNext()){ int ID = cursor.getInt(0); String name = cursor.getString(1); String phone = cursor.getString(2); Log.d(TAG,"onVIEwClicked: "+ID+"--"+name+"--"+phone); } } break; } mDb.close(); }
使用谷歌封装好的API实现增删改查逻辑 传统实现方式 调用方法没有返回值,程序员不容易进行判断,传统的SQL语句很容易写错.
@OnClick({R.ID.click1,R.ID.click4}) public voID onVIEwClicked(VIEw vIEw) { mDb = mMyOpenHelper.getWritableDatabase(); switch (vIEw.getID()) { case R.ID.click1: ContentValues values = new ContentValues(); values.put("name","b"); values.put("phone","18634757219"); long loginin = mDb.insert("loginin",null,values); if (loginin==-1){ Log.d(TAG,"onVIEwClicked: 插入失败"); }else{ Log.d(TAG,"onVIEwClicked: 插入成功"); } break; case R.ID.click2: int delete = mDb.delete("loginin","name=?",new String[]{"b"}); Log.d(TAG,"onVIEwClicked: 删除了"+delete+"行"); break; case R.ID.click3: ContentValues values1 = new ContentValues(); values1.put("phone","110"); int update = mDb.update("loginin",values1,new String[]{"b"}); if (update==-1){ Log.d(TAG,"onVIEwClicked: 更新失败"); }else{ Log.d(TAG,"onVIEwClicked: 更新成功"); } break; case R.ID.click4: Cursor cursor = mDb.query("loginin",null,"onVIEwClicked: "+ID+"--"+name+"--"+phone); } } break; } mDb.close(); }总结
以上是内存溢出为你收集整理的sqlite数据库全部内容,希望文章能够帮你解决sqlite数据库所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)