标签:
使用这种方式调用数据库,比常规方式好在,简洁且具有返回值,可以判断是否成功
add:
SQLiteDatabase db=myOpenHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("username", "wangwu");
values.put("password", "110");
long insert=db.insert("user", null, values);
db.close();
if(insert>0){
Toast.makeText(getApplicationContext(), "添加成功", 1).show();
}else{
Toast.makeText(getApplicationContext(), "添加失败", 1).show();
}
delete:
SQLiteDatabase db=myOpenHelper.getWritableDatabase();
int delete=db.delete("user", "username=?", new String[]{"wangwu"});
db.close();
Toast.makeText(getApplicationContext(), "删除了"+delete+"行", 0).show();
update:
SQLiteDatabase db=myOpenHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("password", "123456789123");
int update=db.update("user", values, "name=?", new String[]{"wangwu"});
Toast.makeText(getApplicationContext(), "更新了"+update+"行", 0).show();
db.close();
find:
SQLiteDatabase db=myOpenHelper.getWritableDatabase();
//colums代表你要查询的列
Cursor cursor=db.query( "user", new String[]{"password"},"username=?" ,new String[]{"wangwu"}, null, null, null);
if(cursor!=null&&cursor.getCount()>0){
while(cursor.moveToNext()){
String pwd=cursor.getString(0);
System.out.println("password:"+pwd);
}
1.获取数据库
2.对数据库进行操作
此时列:
ContentValues values=new ContentValues();
values.put("password", "123456789123");
int update=db.update("user", values, "name=?", new String[]{"wangwu"});
在这种方法中,sql语句的形式已经写好,缺少一些values
3.操作完毕,关闭数据库
标签:
原文地址:http://www.cnblogs.com/mrzhangkang/p/5229764.html