android数据库


组织contract类的一个好方法是在类的根层级定义一些全局变量,然后为每一个table来创建内部类。

Note:通过实现 BaseColumns 的接口,内部类可以继承到一个名为_ID的主键,这个对于Android里面的一些类似cursor adaptor类是很有必要的。这么做不是必须的,但这样能够使得我们的DB与Android的framework能够很好的相容。

例如,下面的例子定义了表名与该表的列名:


public final class FeedReaderContract {

    public FeedReaderContract(){}
    public static abstract class FeedEntry implements BaseColumns{
        public static final String TABLE_NAME = "entry";
        public static final String COLUMN_NAME_ENTRY_ID = "entryid";
        public static final String COLUMN_NAME_TITLE = "title";
        public static final String COLUMN_NAME_SUBTITLE = "subtitle";
    }
}
继承SQLiteOpenHelp抽象类,借助该类可以对数据库进行创建和升级,SQLite中有两个重要的方法,
getReadableDatabase()和getWriteableDatabase(),这两个方法都可以打开或者创建数据库(如果数据库存在就打开,否则就创建新的数据库)
public class MyDataBaseHelper extends SQLiteOpenHelper {
    private static final String TEXT_TYPE = "text";
    private static final String SQL_CREATE_ENTRIES =
            "create table " + FeedReaderContract.FeedEntry.TABLE_NAME + "("
                    + FeedReaderContract.FeedEntry._ID + " integer primary key autoincrement,"
                    + FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID +" " +TEXT_TYPE + ","
                    + FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE + " "+TEXT_TYPE + ")";
    private static final String SQL_DELETE_ENTRIES="drop table is exists"+FeedReaderContract.FeedEntry.TABLE_NAME;
    public static final String DATABASE_NAME="FeedReader.db";
    private static final int VERSION = 1;
    public MyDataBaseHelper(Context context) {
        super(context,DATABASE_NAME,null,VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL(SQL_CREATE_ENTRIES);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        sqLiteDatabase.execSQL(SQL_DELETE_ENTRIES);
        onCreate(sqLiteDatabase);
    }
}
判断数据库是否创建
  /* file = getExternalCacheDir();*///这句可以获得数据库,            
file=getCacheDir();//
if (file != null && file.exists())
根据点击的按钮不同执行不同的操作
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.create:
            //dbSQLiteDatabase,借助db可以对数据库进行增删改查操作
            
            db = dataBaseHelper.getWritableDatabase();
           /* file = getExternalCacheDir();*///这句可以获得数据库,
            file=getCacheDir();
            if (file.exists()) {
                Toast.makeText(this, "create success", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "create failed", Toast.LENGTH_SHORT).show();
            }
            break;
        case R.id.delete:
            if (file != null && file.exists()) {
                db.delete(FeedReaderContract.FeedEntry.TABLE_NAME, FeedReaderContract.FeedEntry._ID + "=?", new String[]{"1"});
                Toast.makeText(this, "delete success", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "delete failed", Toast.LENGTH_SHORT).show();
            }
            break;
        case R.id.query:
            if (file != null && file.exists()) {
                String[] columns = new String[]{
                        FeedReaderContract.FeedEntry._ID,
                        FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE,
                        FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID};
                Cursor cursor = db.query(FeedReaderContract.FeedEntry.TABLE_NAME, columns, null, null, null, null, null);
                if (cursor.moveToFirst()) {
                    do {
                        int _id = cursor.getInt(cursor.getColumnIndex(FeedReaderContract.FeedEntry._ID));
                        String title = cursor.getString(cursor.getColumnIndex(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE));
                        String entryId = cursor.getString(cursor.getColumnIndex(FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID));
                        Log.d("test", "-id:" + _id + ",title:" + title + ",entryId:" + entryId);
                    } while (cursor.moveToNext());
                }


            } else {
                Toast.makeText(this, "query failed", Toast.LENGTH_SHORT).show();
            }
            break;
        case R.id.insert:
            if (file != null && file.exists()) {
                ContentValues values = new ContentValues();
                values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID,"li");
                values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE,"花海");
                db.insert(FeedReaderContract.FeedEntry.TABLE_NAME, null, values);
                values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID,"zhang");
                values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE, "超人不会飞");
                db.insert(FeedReaderContract.FeedEntry.TABLE_NAME, null, values);
                values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID, "hua");
                values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE, "说好的幸福呢");
                db.insert(FeedReaderContract.FeedEntry.TABLE_NAME, null, values);
                Toast.makeText(this,"insert success",Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(this,"insert failed",Toast.LENGTH_SHORT).show();
            }
            break;

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值