android sqllite dbhelper

 

 

               String userInfo = PrefInfoUtils.getUserInfo(mContext);

iUser = JsonParser.parseDateJson(userInfo, UserEntity.class);  

userDB = new UserDB(mContext, iUser.getId()+"");

 

package com.curiousby.fitnessandappointment.entity;

import java.io.Serializable;
import java.util.Date;
 

//com.curiousby.fitnessandappointment.entity.UserEntity
@SuppressWarnings("serial")
public class UserEntity  implements Serializable{

	
	private long id;
	private String name;
	private String nickName;
	private String password;
	private String mail;
	private String telphone;
	private String photo;
	private Date insertTime;
	private Date lastUpdateTime;
	
	
	public UserEntity(){}
	public UserEntity(long id) {
		this.id = id;
	}
	public UserEntity(long id, String name, String nickName, String password,
			String mail, String telphone, String photo, Date insertTime,
			Date lastUpdateTime) {
		this.id = id;
		this.name = name;
		this.nickName = nickName;
		this.password = password;
		this.mail = mail;
		this.telphone = telphone;
		this.photo = photo;
		this.insertTime = insertTime;
		this.lastUpdateTime = lastUpdateTime;
	}
 
	@Override
	public String toString() {
		return "UserEntity [id=" + id + ", name=" + name + ", nickName="
				+ nickName + ", password=" + password + ", mail=" + mail
				+ ", telphone=" + telphone + ", photo=" + photo
				+ ", insertTime=" + insertTime + ", lastUpdateTime="
				+ lastUpdateTime + "]";
	}
	
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getNickName() {
		return nickName;
	}
	public void setNickName(String nickName) {
		this.nickName = nickName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getMail() {
		return mail;
	}
	public void setMail(String mail) {
		this.mail = mail;
	}
	public String getTelphone() {
		return telphone;
	}
	public void setTelphone(String telphone) {
		this.telphone = telphone;
	}
	public String getPhoto() {
		return photo;
	}
	public void setPhoto(String photo) {
		this.photo = photo;
	}
	public Date getInsertTime() {
		return insertTime;
	}
	public void setInsertTime(Date insertTime) {
		this.insertTime = insertTime;
	}
	public Date getLastUpdateTime() {
		return lastUpdateTime;
	}
	public void setLastUpdateTime(Date lastUpdateTime) {
		this.lastUpdateTime = lastUpdateTime;
	}
	public void setId(long id) {
		this.id = id;
	}
	public long getId() {
		return this.id ;
	}
	
	

	
	
}

 

 

 

package com.curiousby.fitnessandappointment.db;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.curiousby.fitnessandappointment.entity.UserEntity;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class UserDB {
 
	
	private static final String TABLE_NAME = "user";
	public static final String  DB_NAME = "userId.db";
	
	public static final String SQL_ISEXITS =" " +
		" CREATE table IF NOT EXISTS user( " +
		" id INTEGER PRIMARY KEY AUTOINCREMENT," +
		" name TEXT, " +
		" nickName TEXT," +
		" password TEXT," +
		" mail TEXT," +
		" telphone TEXT," +
		" photo TEXT," +
		" insertTime TEXT," +
		" lastUpdateTime TEXT )" ;
	public static final String SQL_SELECT =" SELECT * FROM  user";  
	public static final String SQL_INSERT =" INSERT INTO  user (id,name,nickName,password,mail,telphone,photo,insertTime,lastUpdateTime) values(?,?,?,?,?,?,?,?,?)" ;
	
	
	
	private SQLiteDatabase db;
	public  UserDB (Context context,String userId){
		db = context.openOrCreateDatabase( userId+"_fitnesschat.db", Context.MODE_PRIVATE,null);
		db.execSQL(SQL_ISEXITS);
	}
	
	public  boolean isExist(String id) {
		Cursor c = db.rawQuery( SQL_SELECT + " WHERE id = ?", new String[] { id });
		return c.moveToFirst();
	}
	
	public void delete(String id) {
		db.delete(TABLE_NAME , "id=?", new String[] { id });
	}
	
	public void  deleteAll(){
		db.delete(TABLE_NAME, "id>?", new String[] {"0"});
	}
	
	public UserEntity getEntityById(String id){
		UserEntity user = null;
		Cursor c = db.rawQuery(SQL_SELECT + " WHERE id = ?",new String[] { id });
		while (c.moveToNext()) {  
			long itemId =c.getLong(c.getColumnIndex("id"));
			String name = c.getString(c.getColumnIndex("name"));
			String nickName = c.getString(c.getColumnIndex("nickName"));
			String password = c.getString(c.getColumnIndex("password"));
			String mail = c.getString(c.getColumnIndex("mail"));
			String telphone = c.getString(c.getColumnIndex("telphone"));
			String photo = c.getString(c.getColumnIndex("photo"));
			String insertTime = c.getString(c.getColumnIndex("insertTime"));
			String lastUpdateTime = c.getString(c.getColumnIndex("lastUpdateTime"));
			user = new UserEntity( itemId , name, nickName, password,
					mail, telphone, photo,  parse(insertTime),  parse(lastUpdateTime));
		}
		return user;
	}
	
	public  List<UserEntity> getAll(){
		List<UserEntity> list = new ArrayList<UserEntity>();
		UserEntity user = null;
		Cursor c = db.rawQuery(SQL_SELECT,null);
		while (c.moveToNext()) {  
			long itemId =c.getLong(c.getColumnIndex("id"));
			String name = c.getString(c.getColumnIndex("name"));
			String nickName = c.getString(c.getColumnIndex("nickName"));
			String password = c.getString(c.getColumnIndex("password"));
			String mail = c.getString(c.getColumnIndex("mail"));
			String telphone = c.getString(c.getColumnIndex("telphone"));
			String photo = c.getString(c.getColumnIndex("photo"));
			String insertTime = c.getString(c.getColumnIndex("insertTime"));
			String lastUpdateTime = c.getString(c.getColumnIndex("lastUpdateTime"));
			user = new UserEntity( itemId, name, nickName, password,
					mail, telphone, photo,  parse(insertTime),  parse(lastUpdateTime));
			list.add(user);
		}
		return list;
	}
	
	public  void  saveOrUpdate(UserEntity item){
		if(isExist(item.getId()+"")){
			ContentValues cv = new ContentValues();
			cv.put("name", item.getName());
			cv.put("nickName", item.getNickName());
			cv.put("password", item.getPassword());
			cv.put("mail", item.getMail());
			cv.put("telphone", item.getTelphone());
			cv.put("photo", item.getPhoto());
			cv.put("insertTime", formate(item.getInsertTime()));
			cv.put("lastUpdateTime", formate(item.getLastUpdateTime()));
			db.update(TABLE_NAME, cv, "id=?", new String[]{item.getId()+""});
		}else{
			db.execSQL(SQL_INSERT,new Object[]{
					item.getId(),
					item.getName(),
					item.getNickName(),
					item.getPassword(),
					item.getMail(),
					item.getTelphone(),
					item.getPhoto(),
					formate(item.getInsertTime()),
					formate(item.getLastUpdateTime())
			});
		}
	}
	 
	private Date parse(String date){
		try {
			return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date);
		} catch (ParseException e) {
			e.printStackTrace ();
		}
		return new Date();
	}
	
	private String formate(Date date){
		return new SimpleDateFormat("yyyy-MM-dd").format(date);
	}
	
	
}

 

 

 

 

 

 

 

 

 

 

 

 

 

Donate to developers

Driven by interest, I write 免费something with joy and sweat. I hope you like my work and can support it at the same time. Of course, if you have money to support a money field (the love sign in the upper right corner, support Alipay and PayPal donations), if you have no money to support a personal field, thank you.



 
 
 Thank you for your sponsorship, I will do better!

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326843537&siteId=291194637