android中SQLite实现

SQLite操作类:

package com.example.administrator.myapplication;

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

public class BookmarkSQLiteOpenHelper extends SQLiteOpenHelper{
    public BookmarkSQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version){
        super(context,name,factory,version);
    }

    @Override
    public void onCreate(SQLiteDatabase db){
        String sql="create table bookmark(id integer primary key autoincrement,favicon varchar(100),title varchar(100),url varchar(100))";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){}
}

SQLite初始化:

bookmarkSQLiteOpenHelper=new BookmarkSQLiteOpenHelper(this,"browser.db",null,1);

SQLite插入数据:

void insertBookmark(Bookmark bookmark){
        SQLiteDatabase db=bookmarkSQLiteOpenHelper.getWritableDatabase();
        db.execSQL("insert into bookmark(favicon,title,url)values(?,?,?)",new String[]{bookmark.getFavicon(),bookmark.getTitle(),bookmark.getUrl()});
    }

 SQLite查询数据:

bookmarkSQLiteOpenHelper=new BookmarkSQLiteOpenHelper(this,"browser.db",null,1);
        SQLiteDatabase db=bookmarkSQLiteOpenHelper.getReadableDatabase();
        Cursor cursor=db.rawQuery("select * from bookmark",null);
        while(cursor.moveToNext()){
            String favicon=cursor.getString(cursor.getColumnIndex("favicon"));
            String title=cursor.getString(cursor.getColumnIndex("title"));
            String url=cursor.getString(cursor.getColumnIndex("url"));

            Bitmap bitmap=null;
            try{
                String sdcard_path=Environment.getExternalStorageDirectory().getCanonicalPath();
                String bitmap_path=sdcard_path+"/xinyuelantuBrowserFavicon/"+favicon+".jpg";
                bitmap=BitmapFactory.decodeFile(bitmap_path);
            }
            catch(IOException e){}
            Map<String,Object> map_bookmark=new HashMap<String,Object>();
            map_bookmark.put("favicon",bitmap);
            map_bookmark.put("title",title);
            map_bookmark.put("url",url);
            arraylist_bookmark.add(map_bookmark);
        }

 

posted @ 2016-05-05 17:02  zqxLonely  阅读(278)  评论(0编辑  收藏  举报