【android】 sqliter数据库CRUD

基础类,DBHelper
package com.duduli.li.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {

	private final static String DB_NAME = "BOOK.DB";
	
	public DBHelper(Context context) {
		super(context, DB_NAME, null, 1);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		db.execSQL("create table bookinfo (_id integer PRIMARY KEY ,name varchar(20),position long,linemsg varchar(100),page int,fileplace varchar(40));");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int olderVersion, int newVersion) {
		// TODO Auto-generated method stub
		db.execSQL("drop table if exists bookinfo");
		onCreate(db);
	}

}


book类:
package com.duduli.li.db;

public class Book {
	private int id;
	private String name;
	private Long position;
	private String line;
	private String filePlace;
	private int page;
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public String getFilePlace() {
		return filePlace;
	}
	public void setFilePlace(String filePlace) {
		this.filePlace = filePlace;
	}
	public String getLine() {
		return line;
	}
	public void setLine(String line) {
		this.line = line;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Long getPosition() {
		return position;
	}
	public void setPosition(Long position) {
		this.position = position;
	}
	public int getId() {
		return id;
	}
}



book的CRUD:
package com.duduli.li.db;

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

public class BookDAO {
	private SQLiteDatabase db;
	private DBHelper dbh;
	public  BookDAO(Context context){
		dbh = new DBHelper(context);
	}
	
	@SuppressWarnings("unused")
	public void add(Book book){
		db = dbh.getWritableDatabase();
		ContentValues cv = new ContentValues();
		cv.put("name", book.getName());
		cv.put("position", book.getPosition());
		cv.put("linemsg", book.getLine());
		cv.put("fileplace", book.getFilePlace());
		cv.put("page", book.getPage());
		db.insert("bookinfo", null, cv);
		db.close();
	}
	
	public Cursor show(){
		db = dbh.getReadableDatabase();
		Cursor cur = db.query("bookinfo",null, null, null, null, null, null);
		System.out.println("show get the cursor count is "+cur.getCount());
		db.close();
		return cur;
	}
	
	
	public void delete(String s){
		db = dbh.getWritableDatabase();
		db.delete("bookinfo", "_id=?", new String[]{s});
		db.close();
	}
	
	
	public void update(int _id,long position,int page,String listMsg){
		db = dbh.getWritableDatabase();
		ContentValues cv = new ContentValues();
		cv.put("position", position);
		cv.put("page", page);
		cv.put("linemsg", listMsg);
		String [] arg = {String.valueOf(_id)};
		db.update("bookinfo", cv, "_id=?", arg);
		db.close();
	}
	
}

你可能感兴趣的:(android,sqlite,DbHelper)