ContentProvider灵活性并不是很好,这时我们可以直接使用SQLiteOpenHelper来进行数据库操作。个人觉得这才是我知道的数据库,棒棒哒!这里记录一下关联表查询。
1.两个数据结构:BookDao和AuthorDao
/** * Created by travis on 2016/3/8. */ public class BookDao { public static final String TABLE_NAME = "book"; public static final String ID = "id"; public static final String BOOK_NAME = "book_name"; public static final String PRICE = "price"; public static final String ISBN = "isbn"; public static final String AUTHOR = "author"; public static final String CREATE_SQL = "CREATE TABLE " + TABLE_NAME + " (" + ID + " INTEGER PRIMARY KEY," + BOOK_NAME + " VARCHAR(32)," + PRICE + " VARCHAR(10)," + ISBN + " VARCHAR(16)," + AUTHOR + " VARCHAR(32)" + ");"; public static final String DROP_SQL = "DROP TABLE IF EXISTS " + TABLE_NAME; }
/** * Created by hsji on 2016/3/8. */ public class AuthorDao { public static final String TABLE_NAME = "author"; public static final String ID = "id"; public static final String AUTHOR_NAME = "author_name"; public static final String GENDER = "gender"; public static final String ADDRESS = "address"; public static final String CREATE_SQL = "CREATE TABLE " + TABLE_NAME + " (" + ID + " INTEGER PRIMARY KEY," + AUTHOR_NAME + " VARCHAR(32)," + GENDER + " VARCHAR(1)," + ADDRESS + " VARCHAR(64)" + ");"; public static final String DROP_SQL = "DROP TABLE IF EXISTS " + TABLE_NAME; }
2.数据库和java对象的纽带SQLiteOpenHelper
public class DatabaseHelper extends SQLiteOpenHelper { public static final String DATABBASE_NAME = "monster.db"; public static final int DATABASE_VERSION = 3; /** * 如果如果数据库不存在,创建数据库,如果数据库已经存在,则打开数据库。 * * @param context */ public DatabaseHelper(Context context) { super(context, DATABBASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(BookDao.CREATE_SQL); db.execSQL(AuthorDao.CREATE_SQL); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL(BookDao.DROP_SQL); db.execSQL(AuthorDao.DROP_SQL); onCreate(db); } }
3.在程序当中直接使用SQLiteOpenHelper进行数据库操作。
//获取SQLiteOpenHelper对象,如果数据库已经存在则打开数据库,如果不存在则创建并打开 DatabaseHelper dBHelper = new DatabaseHelper(TestActivity.this); SQLiteDatabase db = dBHelper.getWritableDatabase(); //往book表里添加一本书 ContentValues cv = new ContentValues(); cv.put(BookDao.ID, 100); cv.put(BookDao.BOOK_NAME, "如何和傻逼相处"); cv.put(BookDao.AUTHOR, "travis"); cv.put(BookDao.PRICE, "¥8888"); cv.put(BookDao.ISBN, "9000123"); //首先检查数据库中有没有这本书,有的话不再插入,没有的话执行插入操作 Cursor tem = db.rawQuery("select id from book where id = ?", new String[]{"100"}); if (tem.getCount() != 0) { Log.d(TAG, "Book 已经存在该键值"); } else { db.insert(BookDao.TABLE_NAME, null, cv); } //往author表里添加一个作者 ContentValues cv1 = new ContentValues(); cv1.put(AuthorDao.ID, 100); cv1.put(AuthorDao.AUTHOR_NAME, "travis"); cv1.put(AuthorDao.GENDER, "M"); cv1.put(AuthorDao.ADDRESS, "上海"); Cursor tem1 = db.rawQuery("select id from author where id = ?", new String[]{"100"}); if (tem1.getCount() != 0) { Log.d(TAG, "Author 已经存在该键值"); } else { db.insert(AuthorDao.TABLE_NAME, null, cv1); } //通过rawQuery()方法实现关联表查询 Cursor cursor = db.rawQuery("select book.id as id,book_name,author_name from " + BookDao.TABLE_NAME + "," + AuthorDao.TABLE_NAME + " where book.author = author.author_name", new String[]{}); //输出查询到的数据 cursor.moveToNext(); while (!cursor.isAfterLast()) { Log.d(TAG, "book_name=" + cursor.getString(cursor.getColumnIndex(BookDao.BOOK_NAME))); Log.d(TAG, "author_name=" + cursor.getString(cursor.getColumnIndex(AuthorDao.AUTHOR_NAME))); cursor.moveToNext(); }