Android数据存储--sqlite

SQLite是轻量级的、嵌入式的、关系型数据库,目前已经在iPhone、Android等手机系统中使用,而且被其他的公司广泛使用。

Android SQLite,从Android SDK中可以看到,对数据库的操作都封装好了,

常用的一个类就是ContentValues,类似一个Hashmap存放键值对。

我们一般使用sqlite,自己写个类型继承SQLiteOpenHelper,通过这个类的getWritableDatabase方法,可以获得一个SQLiteDatabase的实例。

进而我们可以实现对数据的基本操作了(增,删,改,查)。


public class DBHelper extends SQLiteOpenHelper {

	
	public final static String DB_NAME="contact";
	public final static int VERSION=1;
	private static DBHelper instance=null;
	
	private  SQLiteDatabase db;
	public static DBHelper getInstance(Context context){
		if(instance==null)
		{
			instance=new DBHelper(context);
		}
		return instance;
	}
	
	private void openDatabase()
	{
		db=this.getWritableDatabase();
	}
	
	public DBHelper(Context context){
		super(context,DB_NAME,null,VERSION);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		
		StringBuffer tableUserCreate =new StringBuffer();
		
		
		tableUserCreate.append("create table User (id integer primary key autoincrement,")
					   .append("name text,")
					   .append("phonenumber text)");
		
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub
		String sql="drop table if exists user";
	
		db.execSQL(sql);
		onCreate(db);
	}
	//增加一个user
public  boolean saveUser(User user)
{      openDatabase();
	ContentValues value=new ContentValues();
	String phonenumber = user.getPhoneNumber();
	System.out.println("pjoneId:"+phonenumber);
	Cursor cursor = db.query("User", new String []{"id","phonenumber","name"} , "phonenumber=?",new String[]{phonenumber} , null, null, null);
	cursor.moveToNext();
	System.out.println("count:"+cursor.getCount());
	if(cursor.getCount()==0)
	{
	value.put("name", user.getName());
	value.put("phonenumber", user.getPhoneNumber());
	db.insert("User", null, value);
	System.out.println("save");
	}
	System.out.println("no save");
	if(cursor!=null){
	cursor = null;
	db.close();
	}
	return true;
}


 //获取数据库中所有的user
public List<Map<String, Object>> getUser(){
	openDatabase();
	List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
	Cursor cursor = db.query("User", new String []{"id","name","phonenumber"} , null, null, null, null, null);
	if(cursor!=null){
	if(cursor.isBeforeFirst()){
		while(cursor.moveToNext())
		{
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("name",cursor.getString(cursor.getColumnIndex("name"));
			map.put("id",cursor.getString(cursor.getColumnIndex("id")));
			map.put("phonenumber",cursor.getString(cursor.getColumnIndex("phonenumber")));
			
			list.add(map);
		}
		
	}
	}
	if(cursor!=null){
		cursor = null;
		db.close();
		}
	return list;
}


  //修改user信息
public boolean editUser(User user) {
	// TODO Auto-generated method stub
	 openDatabase();
	 ContentValues cv = new ContentValues();
	 cv.put("phonenumber ",user.getPhoneNumber());
	 cv.put("name", user.getName());
	 db.update("user", cv, "id=+'"+user.getId()+"'" , null);
	 db.close();
	return true;
}


 //取user中的某一字段
public String getUserName(String phonenumber) {
	// TODO Auto-generated method stub
	 openDatabase();
	 String name = null;
	 Cursor cursor = db.query("User", new String []{"name"}, "phonenumber='" + phonenumber + "'", null, null, null, null);
	 if(cursor.isBeforeFirst()){
			while(cursor.moveToNext())
			{
				name = cursor.getString(cursor.getColumnIndex("name"));
				System.out.println("name="+name);
			}
	 }
	 if(cursor!=null){
		 cursor = null;
			db.close();
		}
	return name;
}
  //按条件删除user
public boolean deleteUserById(int id) {
	// TODO Auto-generated method stub
	 openDatabase();
	    db.delete("User","id='"+id+"'", null);
		System.out.println("true");
		db.close();
		return true;
}

@Override
public String getUserNumber(String str) {
	// TODO Auto-generated method stub
	 openDatabase();
	 String number = null;
	 Cursor cursor = db.query("User", new String []{"phonenumber"}, "name='" + str + "'", null, null, null, null);
	 if(cursor.isBeforeFirst()){
			while(cursor.moveToNext())
			{
				number = cursor.getString(cursor.getColumnIndex("name"));
				System.out.println("number="+number);
			}
	 }
	 if(cursor!=null){
		 cursor = null;
			db.close();
		}
	return number;
}

}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值