【Android學習筆記】SQLite資料庫儲存

來源:互聯網
上載者:User

因為前面提到xml儲存變更檔很麻煩的緣故,最終還是選擇了使用資料庫儲存

一試才覺十分的方便,速度也快

上源碼:

public class DBHelper  extends SQLiteOpenHelper{    private final static String DATABASE_NAME="fanliao_db";    private final static int DATABASE_VERSION=1;    private final static String TABLE_NAME="fanliao_chat";    public final static String CHAT_ID="_id";     public final static String CHAT_Name="chatname";    public final static String CHAT_Info="chatinfo";    public final static String CHAT_Time="chattime";            public DBHelper(Context context)    {        super(context, DATABASE_NAME,null, DATABASE_VERSION);    }                 @Override    public void onCreate(SQLiteDatabase db) {       //CREATE TABLE fanliao_chat( _id  INTEGER PRIMARY KEY  AUTOINCREMENT,    // chatname TEXT, chattime TEXT, chatinfo TEXT);    String sql="CREATE TABLE "+TABLE_NAME+"("+CHAT_ID+"  INTEGER PRIMARY KEY  AUTOINCREMENT,"        +CHAT_Name+" TEXT, "+CHAT_Time+" TEXT, "+CHAT_Info+" TEXT);";        db.execSQL(sql);        System.out.println(sql);             }    @Override    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {        String sql=" DROP TABLE IF EXISTS "+TABLE_NAME;        db.execSQL(sql);        onCreate(db);        System.out.println(sql);    }    public Cursor select()    {        SQLiteDatabase db=this.getReadableDatabase();        Cursor cursor=db.query(TABLE_NAME, null, null, null, null, null,  " _id asc");        return cursor;    }        public long insert(String chatname, String chattime, String chatinfo)    {        SQLiteDatabase db=this.getWritableDatabase();        ContentValues cv=new ContentValues();         cv.put(CHAT_Name, chatname);        cv.put(CHAT_Time, chattime);        cv.put(CHAT_Info, chatinfo);        long row=db.insert(TABLE_NAME, null, cv);        return row;    }        public void delete(int id)    {        SQLiteDatabase db=this.getWritableDatabase();        String where=CHAT_ID+"=?";        String[] whereValue={Integer.toString(id)};        db.delete(TABLE_NAME, where, whereValue);    }        public void update(int id,String chatname,String chattime, String chatinfo)    {        SQLiteDatabase db=this.getWritableDatabase();        String where=CHAT_ID+"=?";        String[] whereValue={Integer.toString(id)};        ContentValues cv=new ContentValues();         cv.put(CHAT_Name, chatname);        cv.put(CHAT_Time, chattime);        cv.put(CHAT_Info, chatinfo);        db.update(TABLE_NAME, cv, where, whereValue);    }        public void delall(){    SQLiteDatabase db=this.getReadableDatabase();    String sql=" DROP TABLE IF EXISTS "+TABLE_NAME;        db.execSQL(sql);        onCreate(db);    }        }

用後才覺得經常修改的資料本就應用程式資料庫的,

形如“聊天記錄”這種雖沒有十分複雜的儲存結構,也是適宜存在表中,

而xml大概多是用以傳輸資料或儲存少量不常用改動的資料把~

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.