• 结对项目——数据库


    创建数据库表

    public class DataBaseHelper extends SQLiteOpenHelper {
    	
    	private static final String DATABASE_NAME = "MyBook.db";
    	
    	private static final int VERSION = 1;
    
    	public DataBaseHelper(Context context) {
    		super(context, DATABASE_NAME, null, VERSION);
    	}
    
    	
    	public DataBaseHelper(Context context, String name, CursorFactory factory, int version) {
    		super(context, name, factory, version);
    	}
    
    	@Override
    	public void onCreate(SQLiteDatabase db) {
    		StringBuilder sb = new StringBuilder();
    		sb.append("create table " + TABLE_NAME);
    		sb.append("(");
    		sb.append(BookColumn._ID + " integer primary key autoincrement,");
    		sb.append(NAME + " text,");
    		sb.append(PASSWORD + " text,");
    		sb.append(FILE_PATH + " text,");
    		sb.append(LAST_READ_TIME + " integer,");
    		sb.append(BEGIN + " integer,");
    		sb.append(PROGRESS + " text");
    		sb.append(")");
    		db.execSQL(sb.toString());
    	}
    
    	@Override
    	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    		db.execSQL("drop table " + TABLE_NAME);
    	}
    
    }
    

    数据库工具类实现增删改查功能

    public class BookManager {
    	private static final String TAG = "MyBook";
    	private DataBaseHelper dataBaseHelper;
    
    	public BookManager(Context context) {
    		dataBaseHelper = new DataBaseHelper(context);
    	}
    
    
    	public Long save(Book book) {
    		SQLiteDatabase sqLiteDatabase = dataBaseHelper.getWritableDatabase();
    		ContentValues values = bookToContentValues(book);
    		Long count = sqLiteDatabase.insert(TABLE_NAME, null, values);
    		sqLiteDatabase.close();
    		return count;
    	}
    
    	public long update(Book book) {
    		SQLiteDatabase sqLiteDatabase = dataBaseHelper.getWritableDatabase();
    
    		ContentValues values = bookToContentValues(book);
    		long count = sqLiteDatabase.update(TABLE_NAME, values, BookColumn._ID + "=?", new String[] { book.getId().toString() });
    		sqLiteDatabase.close();
    		return count;
    	}
    
    	public long delete(Long id) {
    		SQLiteDatabase sqLiteDatabase = dataBaseHelper.getWritableDatabase();
    		long count = sqLiteDatabase.delete(TABLE_NAME, BookColumn._ID + "=?", new String[] { id.toString() });
    		sqLiteDatabase.close();
    		return count;
    	}
    
    
    	public Book get(Long id) {
    		Book book = null;
    		SQLiteDatabase sqLiteDatabase = dataBaseHelper.getReadableDatabase();
    		Cursor cursor = sqLiteDatabase.query(TABLE_NAME, new String[] { BookColumn._ID, NAME, PASSWORD, FILE_PATH, LAST_READ_TIME, BEGIN, PROGRESS }, BookColumn._ID + "=?",
    				new String[] { id.toString() }, null, null, null);
    		if (cursor.moveToFirst()) {
    			book = cursorToBook(cursor);
    			cursor.close();
    		}
    		sqLiteDatabase.close();
    		return book;
    	}
    
    
    	public ArrayList<Book> getAll() {
    		ArrayList<Book> books = new ArrayList<Book>();
    		SQLiteDatabase sqLiteDatabase = dataBaseHelper.getReadableDatabase();
    		Cursor cursor = sqLiteDatabase.query(TABLE_NAME, new String[] { BookColumn._ID, NAME, PASSWORD, FILE_PATH, LAST_READ_TIME, BEGIN, PROGRESS }, null, null, null, null,
    				LAST_READ_TIME + " desc");
    		while (cursor.moveToNext()) {
    			Book book = cursorToBook(cursor);
    			books.add(book);
    		}
    		cursor.close();
    		sqLiteDatabase.close();
    		return books;
    	}
    
    	
    	
    	private Book cursorToBook(Cursor cursor) {
    		Long id = cursor.getLong(cursor.getColumnIndex(BookColumn._ID));
    		String name = cursor.getString(cursor.getColumnIndex(NAME));
    		String password = cursor.getString(cursor.getColumnIndex(PASSWORD));
    		String filePath = cursor.getString(cursor.getColumnIndex(FILE_PATH));
    		Date lastReadTime = new Date(cursor.getLong(cursor.getColumnIndex(LAST_READ_TIME)));
    		int begin = cursor.getInt(cursor.getColumnIndex(BEGIN));
    		String progress = cursor.getString(cursor.getColumnIndex(PROGRESS));
    		Book book = new Book(name, filePath, lastReadTime, begin, progress);
    		book.setId(id);
    		book.setPassword(password);
    		return book;
    	}
    }
    
  • 相关阅读:
    php 工厂模式实例
    nginx多虚拟主机配置
    PHP提高编程效率的方法
    PHP 多态
    锁机制之PHP文件锁
    深入认识javascript中的eval函数(转载)
    PHP&MYSQL 常用的一些性能检测
    寒假作业1:问答题
    软件测试基础知识总结
    七种测试驱动模式
  • 原文地址:https://www.cnblogs.com/lw0607/p/6992065.html
Copyright © 2020-2023  润新知