android SQLite的使用

dingcheng998發表於2016-11-17


public class NewsDBApi extends SQLiteOpenHelper{

    private static final String DB_NAME = "newsData.db"; //資料庫名稱
    private static final int version = 1; //資料庫版本
    private SQLiteDatabase database;
    private ArrayList<Data_Map_News> list = new ArrayList<Data_Map_News>();
    public NewsDBApi(Context context){
        super(context,DB_NAME,null,version);
    }

    /*
   **返回資料表所有的資料
   * @params tablename 資料庫表名
   * @return list
   */
    public ArrayList<Data_Map_News> queryData(String tablename){
        database = this.getReadableDatabase();
        //Log.i("查詢資料庫路徑:",database.getPath());
        Cursor c = database.query(tablename,null,null,null,null,null,"mTime desc ");//查詢並獲得遊標

        while (c.moveToNext()){
                int id = c.getInt(c.getColumnIndex("_id"));
                String mTitle = c.getString(c.getColumnIndex("mTitle"));
                String mFrom = c.getString(c.getColumnIndex("mFrom"));
                String mImage = c.getString(c.getColumnIndex("mImage"));
                String mUrl = c.getString(c.getColumnIndex("mUrl"));
                long mTime = c.getLong(c.getColumnIndex("mTime"));
                list.add(new Data_Map_News(mTitle,new Date(mTime),mFrom,mImage,mUrl));
            }

        c.close();
        return  list;
    }
    /*
    **返回資料表最近更新的資料
    * @params tablename 資料庫表名
    * @params lasttime 上次最後插入的一條資料
    * @return list
    */
    public ArrayList<Data_Map_News> queryUpdateData(String tablename,long lasttime){
        database = this.getReadableDatabase();
        //Log.i("查詢資料庫路徑:",database.getPath());
        Cursor c = database.rawQuery("select * from "+tablename+" where "+lasttime+"> mTime order by mTime desc",null);

        while (c.moveToNext()){
            int id = c.getInt(c.getColumnIndex("_id"));
            String mTitle = c.getString(c.getColumnIndex("mTitle"));
            String mFrom = c.getString(c.getColumnIndex("mFrom"));
            String mImage = c.getString(c.getColumnIndex("mImage"));
            String mUrl = c.getString(c.getColumnIndex("mUrl"));
            long mTime = c.getLong(c.getColumnIndex("mTime"));
            list.add(new Data_Map_News(mTitle,new Date(mTime),mFrom,mImage,mUrl));
        }

        c.close();
        return  list;
    }
    /*
     **返回資料表的數量
     * @params tablename 資料庫表名
     * @return int
     */
    public int querysum(String tablename){
        database = this.getReadableDatabase();
        Cursor c = database.rawQuery("select * from "+tablename,null);
        Log.i("查詢資料數量:",c.getCount()+"");
        return  c.getCount();
    }
    /*
     **查詢最新插入資料庫的一條資料
     * @params tablename 資料庫表名
     * @return long
     */
    public long querylast(String tablename){
        ArrayList<Data_Map_News> list1 = queryData(tablename);
       // Log.i("查詢資料庫:",list1.get(0).getmTime().getTime()+"");
        return list1.get(0).getmTime().getTime();
    }
    /*
   **插入資料
   * @params tablename 資料庫表名
   * @params mTime和strings 需要插入的資料
   * @return int
   */
    public void insertData(String tablename,long mTime,String... strings){
        database = this.getWritableDatabase();
        //Log.i("插入資料庫路徑:",database.getPath());
        //例項化一個ContentValues用來裝載待插入的資料cv.put("username","Jack Johnson");//新增使用者名稱
        ContentValues cv = new ContentValues();
            cv.put("mTitle",strings[0]);
            cv.put("mFrom",strings[1]);
            cv.put("mImage",strings[2]);
            cv.put("mUrl",strings[3]);
            cv.put("mTime",mTime);
            database.insert(tablename,null,cv);//執行插入操作

    }
    /*
      ** 根據mTime欄位刪除資料
     */
    public void deleteData(long mTime, String tablename){
        database = this.getWritableDatabase();
        String sql = "delete from "+tablename+" where mTime <= "+mTime;//刪除操作的SQL語句
        database.execSQL(sql);//執行刪除操作
    }
    /*
     **建立多個資料表
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table t_sectors( _id integer primary key autoincrement" +
                ",mTitle not null,mFrom not null,mTime not null,mImage not null,mUrl not null)");
        db.execSQL("create table t_domestic( _id integer primary key autoincrement" +
                ",mTitle not null,mFrom not null,mTime not null,mImage not null,mUrl not null)");
        
    }
    /*
      **更新資料庫
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//        String sql = "DROP TABLE IF EXISTS " + TABLE_NAME;
//        db.execSQL(sql);
//        onCreate(db);
    }
    /*
     * 刪除資料庫
     */
    public static void  deleteDB(String dbName,Context context){
        File file = new File("/data/data/"+context.getPackageName()+"/databases/"+dbName);
        file.delete();
    }
}

相關文章