之前學(xué)習(xí)oracle,簡單的認(rèn)為數(shù)據(jù)庫只存在服務(wù)器端,學(xué)習(xí)安卓之后才發(fā)現(xiàn)原來android和Ios本身是“攜帶”數(shù)據(jù)庫的――SQLite,是輕量級的、嵌入式的、關(guān)系型數(shù)據(jù)庫,是Android、IOS等廣泛使用的的數(shù)據(jù)庫系統(tǒng)。用于存儲本地的一直狀態(tài)。剛寫出來一個實現(xiàn)新聞收藏的功能,寫出來供大家參考。
在Android中我們通過SQLiteDatabase這個類的對象操作SQLite數(shù)據(jù)庫。由于SQLite數(shù)據(jù)庫并不需要像C/S數(shù)據(jù)庫那樣建立連接以及身份驗證的特性,以及SQLite數(shù)據(jù)庫單文件數(shù)據(jù)庫的特性,使得獲得SQLiteDatabase對象就像獲得操作文件的對象那樣簡單。
sqlite要經(jīng)過創(chuàng)建數(shù)據(jù)庫、創(chuàng)建表,然后進行增刪改查等功能。所以第一步,創(chuàng)建一個數(shù)據(jù)庫,SQliteOpenHelper是一個抽象類,來管理數(shù)據(jù)庫的創(chuàng)建和版本的管理。要使用它必須實現(xiàn)它的nCreate(SQLiteDatabase),onUpgrade(SQLiteDatabase, int, int)方法
//創(chuàng)建數(shù)據(jù)庫,建表privatestaticfinal String DBNAME="news.db";privatestaticfinalint VERSION=3; //設(shè)置版本號privatestaticfinal String TBL_DETAILNEWS="news"; //創(chuàng)建表名為news的表privatestaticfinal String TBL_DETAILNEWS_COLUMN_TITLE="_title"; privatestaticfinal String TBL_DETAILNEWS_COLUMN_URL="_url";privatestaticfinal String TBL_DETAILNEWS_COLUMN_DOCID="_docid";privatestaticfinal String TBL_DETAILNEWS_COLUMN_STATE="_state"; public NewsDBHelper(Context context){super(context,DBNAME,null,VERSION);}@Overridepublicvoid onCreate(SQLiteDatabase db) {// TODO Auto-generated method stub16 StringBuffer sb=new StringBuffer();sb.append("create table if not exists ");sb.append(TBL_DETAILNEWS+"(");sb.append(TBL_DETAILNEWS_COLUMN_DOCID +" varchar(100) primary key ,"); //設(shè)置主鍵sb.append(TBL_DETAILNEWS_COLUMN_TITLE+ " varchar(100) ,");sb.append(TBL_DETAILNEWS_COLUMN_URL+" varchar(100) ,");sb.append(TBL_DETAILNEWS_COLUMN_STATE+" integer ");sb.append(")");db.execSQL(sb.toString()); }@Overridepublicvoid onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {String sql2="drop table if exists "+TBL_DETAILNEWS;db.execSQL(sql2); //創(chuàng)建onCreate(db);}Android提供了一個名為SQLiteDatabase的類,它封裝了一些操作數(shù)據(jù)庫的API。使用它能實現(xiàn)基本的CRUD操作,通過getWritableDatabase()和getReadableDatabase()可以獲取數(shù)據(jù)庫實例,便可以寫一些dao層的方法來進行對表的操作:
publicclass DetailNewsDao {private NewsDBHelper helper;public DetailNewsDao(Context context){ helper=new NewsDBHelper(context); //與數(shù)據(jù)庫建立連接 }//插入數(shù)據(jù)publicvoid insertDetsilNews(News news){SQLiteDatabase db=helper.getWritableDatabase(); db.execSQL("insert into news(_title,_url,_docid)" + //將要收藏新聞的標(biāo)題title,標(biāo)識docid,詳細(xì)地址url傳入數(shù)據(jù)庫,便可以依此打開新聞詳顯 "values(?,?,?)",new String[]{news.getTitle(),news.getDetailUrl(),news.getDocid()});db.close();}//刪除數(shù)據(jù)publicvoid del(String docid){ //根據(jù)傳入?yún)?shù)docid刪除數(shù)據(jù) SQLiteDatabase db=helper.getReadableDatabase();db.execSQL("delete from news where _docid = ?",new Object[]{docid});db.close();}//查詢數(shù)據(jù)public List<News> findSelected(){SQLiteDatabase db=helper.getReadableDatabase();Cursor c=db.rawQuery("select * from news", null); //只有對數(shù)據(jù)進行查詢時,才用rawQuery(),增、刪、改和建表,都用execSQl() List<News> list=new ArrayList<News>();while(c.moveToNext()){News news=new News();news.setTitle(c.getString(c.getColumnIndex("_title")));news.setDetailUrl(c.getString(c.getColumnIndex("_url")));news.setDocid(c.getString(c.getColumnIndex("_docid")));list.add(news);}c.close();db.close();return list;}}
用actionbar做好菜單按鈕,點擊收藏,執(zhí)行代碼 :
if(item.getTitle().equals("收藏")){ Toast.makeText(this,"收藏成功", Toast.LENGTH_LONG).show();detailNewsDao.insertDetsilNews(news);item.setTitle("取消收藏");}else{detailNewsDao.del(news.getDocid());Toast.makeText(this,"取消收藏", Toast.LENGTH_LONG).show();item.setTitle("收藏");}以上所述是小編給大家介紹的Android開發(fā)中使用sqlite實現(xiàn)新聞收藏和取消收藏的功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對武林網(wǎng)網(wǎng)站的支持!
新聞熱點
疑難解答