銆愯浆銆?android SQLite 鎬荤粨

鎬荤粨SQLite鑺变簡濂藉鏃堕棿锛屽弬鑰冧簡涓嶅皯璧勬枡銆備互涓嬬煡璇嗗鏋滆寰楃溂鐔燂紝涓嶈鎬€鐤戯紝杩欎簺鎬荤粨涓殑浠g爜锛岃櫧鐒跺緢澶氫笉鏄垜鑷繁鍐欑殑锛屼絾鏄繖浜涗唬鐮侀兘鏄€氱敤鐨勩€係QLite瀛樺偍骞朵笉闅撅紝灏辨槸楹荤儲鑰屽凡锛屽笇鏈涗笅闈㈢殑鎬荤粨鑳藉瀵逛綘浠湁甯姪銆?/p>

Android涓瘡涓€涓暟鎹簱瀵瑰垱寤哄畠鐨勫簲鐢ㄧ▼搴忓寘濂椾欢鏉ヨ閮芥槸绉佹湁鐨勶紝榛樿鎯呭喌涓嬪叾浠栧簲鐢ㄧ▼搴忔棤娉曠洿鎺ヨ闂绉佹湁鏁版嵁搴撱€傛墍鏈夌殑鏁版嵁搴撴枃浠跺瓨鏀惧湪鎵嬫満涓殑/data/data/package_name/databases璺緞涓嬶紝浠ヤ笅鏄父鐢ㄧ殑涓庢暟鎹簱鐩稿叧鐨勫嚱鏁帮細

杩斿洖鍊?/strong>

鍑芥暟

澶囨敞

static SQLiteDatabase

create(SQLiteDatabase.CursorFactory factory)

鍒涘缓涓€涓暟鎹簱

factory锛氬彲閫夌殑鏁版嵁搴撴父鏍囧伐鍘傜被锛屽綋鏌ヨ(query)琚彁浜ゆ椂锛岃瀵硅薄浼氳璋冪敤鏉ュ疄渚嬪寲涓€涓父鏍囥€?/p>

static SQLiteDatabase

openDatabase(String path,SQLiteDatabase.CursorFactory factory,int flags)

鏍规嵁鎻愪緵鐨勬ā寮忔墦寮€涓€涓暟鎹簱

path锛氭墦寮€鎴栧垱寤虹殑鏁版嵁搴撴枃浠?/p>

factory锛氬彲閫夌殑鏁版嵁搴撴父鏍囧伐鍘傜被锛屽綋鏌ヨ(query)琚彁浜ゆ椂锛岃瀵硅薄浼氳璋冪敤鏉ュ疄渚嬪寲涓€涓父鏍囥€傞粯璁や负null銆?/p>

flags锛氭帶鍒舵暟鎹簱鐨勮闂ā寮忋€備富瑕佹湁浠ヤ笅鍑犵妯″紡锛?/p>

l OPEN_READWRITE

l OPEN_READONLY

l CREATE_IF_NECESSARY

l NO_LOCALIZED_COLLATORS

static SQLiteDatabase

openOrCreateDatabase(File file, SQLiteDatabase.CursorFactory factory)

绛夊悓浜巓penDatabase(file.getPath(), factory, CREATE_IF_NECESSARY)

static SQLiteDatabase

openOrCreateDatabase(String path, SQLiteDatabase.CursorFactory factory)

绛夊悓浜巓penDatabase(path,factory, CREATE_IF_NECESSARY)

void

close()

鍏抽棴鏁版嵁搴?/p>

boolean

deleteDatabase(String name)

鍒犻櫎鎸囧畾鐨勬暟鎹簱

name锛氳鍏抽棴鐨勬暟鎹簱鐨勫悕瀛?/p>

绀轰緥浠g爜濡備笅锛?/p>

//鍒涘缓鏁版嵁搴?/p>

SQLiteDatabase mydataBase=SQLiteDatabase.create(new CursorFactory(){

//宸ュ巶绫伙紝涓€涓彲閫夊伐鍘傜被锛屽綋鏌ヨ鏃惰皟鐢ㄦ潵瀹炰緥鍖栦竴涓父鏍?/p>

@Override

public Cursor newCursor(SQLiteDatabase db,

SQLiteCursorDriver masterQuery, String editTable,

SQLiteQuery query) {

// TODO Auto-generated method stub

return null;

}

});

//鍒涘缓鎴栨墦寮€鏁版嵁搴?/p>

SQLiteDatabase myDataBase=this.openOrCreateDatabase("myDataBase.db",

MODE_PRIVATE, new CursorFactory(){

//鍒涘缓鏂扮殑鏁版嵁搴擄紝鍚嶇ОmyDatabase锛屾ā寮廙ODE_PRIVATE锛屽彲閫夌殑娓告爣宸ュ巶绫?/p>

@Override

public Cursor newCursor(SQLiteDatabase db,

SQLiteCursorDriver masterQuery, String editTable,

SQLiteQuery query) {

// TODO Auto-generated method stub

return null;

}

});

SQLiteDatabase myDataBase=this.openOrCreateDatabase("myDataBase.db",MODE_PRIVATE,null);

//鍏抽棴鏁版嵁搴?/p>

myDataBase.close();

//鍒犻櫎鎸囧畾鍚嶇О鐨勬暟鎹簱

this.deleteDatabase(鈥渕yDatabase.db鈥?;

鍦ㄥ鏁版嵁搴撲腑鐨勮〃杩涜鐩稿叧鎿嶄綔鏃讹紝鍙互浣跨敤闈炴煡璇?/strong>鐨別xecSQL(String sql)鏉ユ墽琛屻€傜ず渚嬩唬鐮佸涓嬶細

String CREATE_TABLE = 鈥渃reate table table1 (_id integer primary key autoincrement,number integer,data text)鈥? //鍒涘缓琛?/p>

myDataBase.execSQL(CREATE_TABLE);

Android鎻愪緵浜嗛潪SQL鎸囦护鐨勫鏁版嵁琛ㄨ繘琛屾搷浣滅殑鏂规硶锛屼互涓嬭〃鏍兼槸涓€浜涘父鐢ㄦ柟娉曪細

杩斿洖鍊?/strong>

鍑芥暟

澶囨敞

long

insert(String table, String nullColumnHack, ContentValues values)

鍚戣〃涓彃鍏ヤ竴鏉℃暟鎹?/p>

table锛氭兂瑕佹彃鍏ユ暟鎹殑琛ㄥ悕

nullColumnHack锛歋QL涓嶅厑璁告彃鍏ョ┖琛岋紝鍒濆鍖栧€间负绌烘椂锛岃繖涓€鍒楀皢浼氳鏄剧ず鍦拌祴涓€涓猲ull鍊?/p>

values锛氳鎻掑叆鐨勫€硷紝鍏蜂綋瑙i噴瑙佽〃鏍煎悗鐨勮В閲?/p>

int

delete(String table, String whereClause, String[] whereArgs)

浠庤〃涓垹闄や竴琛屾暟鎹?/p>

table锛氭兂瑕佸垹闄ゆ暟鎹殑琛ㄥ悕

whereClause锛氬彲閫夌殑where瀛愬彞锛屽鏋滃叾鍊间负null锛屽皢浼氬垹闄ゆ墍鏈夌殑琛屻€?/p>

whereArgs锛氬綋鍦╳hereClause涓寘鍚€?鈥濇椂锛屽鏋渨hereArgs鐨勫€间笉涓簄ull锛屽垯杩欎釜鏁扮粍涓殑鍊煎皢渚濇鏇挎崲whereClause涓嚭鐜扮殑鈥?鈥?/p>

Cursor

query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)

鏌ヨ鏁版嵁琛?/p>

table锛氳鏌ヨ鏁版嵁鐨勮〃鍚?/p>

columns锛氳杩斿洖鐨勫垪鐨勫垪鍚嶆暟缁?/p>

selection锛氬彲閫夌殑where瀛愬彞 锛屽鏋滃叾鍊间负null锛屽皢浼氳繑鍥炴墍鏈夌殑琛?/p>

selectionArgs锛氬綋鍦╯election涓寘鍚€?鈥濇椂锛屽鏋渟electionArgs鐨勫€间笉涓簄ull锛屽垯杩欎釜鏁扮粍涓殑鍊煎皢渚濇鏇挎崲selection涓嚭鐜扮殑鈥?鈥?/p>

groupBy锛氬彲閫夌殑group by瀛愬彞锛屽鏋滃叾鍊间负null锛屽皢涓嶄細瀵硅杩涜鍒嗙粍

having锛氬彲閫夌殑having瀛愬彞锛屽鏋滃叾鍊间负null锛屽皢浼氬寘鍚墍鏈夌殑鍒嗙粍

orderBy锛氬彲閫夌殑order by瀛愬彞锛屽鏋滃叾鍊间负null锛屽皢浼氫娇鐢ㄩ粯璁ょ殑鎺掑簭瑙勫垯

limit锛氬彲閫夌殑limit瀛愬彞锛屽鏋滃叾鍊间负null锛屽皢涓嶄細鍖呭惈limit瀛愬彞

鍏充簬杩斿洖鍊肩被鍨婥ursor灏嗗湪琛ㄦ牸鍚庨潰杩涜瑙i噴

int

update(String table, ContentValues values, String whereClause, String[] whereArgs)

淇敼琛ㄤ腑鐨勬暟鎹?/p>

table锛氭兂瑕佷慨鏀规暟鎹殑琛ㄥ悕

values锛氳鏇存柊鐨勫€硷紝鍏蜂綋瑙i噴瑙佽〃鏍煎悗鐨勮В閲?/p>

whereClause锛氬彲閫夌殑where瀛愬彞锛屽鏋滃叾鍊间负null锛屽皢浼氫慨鏀规墍鏈夌殑琛屻€?/p>

whereArgs锛氬綋鍦╳hereClause涓寘鍚€?鈥濇椂锛屽鏋渨hereArgs鐨勫€间笉涓簄ull锛屽垯杩欎釜鏁扮粍涓殑鍊煎皢渚濇鏇挎崲whereClause涓嚭鐜扮殑鈥?鈥?/p>

ContentValues涓昏鏄瓨鏀捐〃涓殑鏁版嵁娈碉紝浠ュ強鍏跺搴旂殑鍊硷紝涓嶩ashtable涓€鏍烽噰鐢ㄥ悕鍊煎鐨勫舰寮忓瓨鍌紝浣嗘槸瀹冨瓨鍌ㄧ殑鍚嶅€煎涓紝鍚嶆槸涓€涓猄tring绫诲瀷锛屽€兼槸鍩烘湰鏁版嵁绫诲瀷銆傚叾浣跨敤绀轰緥濡備笅锛?/p>

ContentValues args = new ContentValues();

args.put(KEY_TITLE, title);

args.put(KEY_BODY, body);

myDataBase.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null);

Android閲囩敤娓告爣瀵逛粠鏁版嵁搴撲腑鏌ヨ鍑烘潵鐨勭粨鏋滆繘琛岄殢鏈虹殑璇诲啓璁块棶锛屽湪鏌ヨ鏁版嵁搴撳悗锛屽皢缁撴灉杩斿洖缁欐父鏍?鍗砤ndroid.database.Cursor)锛岃繖鏄煡璇㈢粨鏋滅殑璁板綍闆嗭紝绀烘剰鍥惧涓嬶細

_id

someNumber

1

8

2

10

3

2

Cursor绫诲父瑙佺殑鏂规硶濡備笅锛?/p>

杩斿洖鍊?/strong>

鍑芥暟

澶囨敞

boolean

move(int offset)

浠ュ綋鍓嶄綅缃负鍙傝€冿紝灏咰ursor绉诲姩鎸囧畾鏁扮洰鐨勪綅缃?鐩稿浣嶇疆)

boolean

moveToPosition(int position)

灏咰ursor绉诲姩鍒版寚瀹氫綅缃?缁濆浣嶇疆)

boolean

moveToNext()

灏咰ursor鍚戝墠绉诲姩涓€涓綅缃?/p>

boolean

moveToLast()

灏咰ursor鍚戝悗绉诲姩涓€涓綅缃?/p>

boolean

moveToFirst()

灏咰ursor绉诲姩鍒扮涓€琛?/p>

boolean

isBeforeFirst()

杩斿洖Cursor鏄惁鎸囧悜绗竴椤规暟鎹箣鍓?/p>

boolean

isAfterLast()

杩斿洖Cursor鏄惁鎸囧悜鏈€鍚庝竴椤规暟鎹箣鍚?/p>

boolean

isClosed()

杩斿洖Cursor鏄惁鍏抽棴

boolean

isFirst()

杩斿洖Cursor鏄惁鎸囧悜绗竴椤规暟鎹?/p>

boolean

isLast()

杩斿洖Cursor鏄惁鎸囧悜鏈€鍚庝竴椤规暟鎹?/p>

boolean

isNull(int columnIndex)

杩斿洖鎸囧畾浣嶇疆鐨勫€兼槸鍚︿负null

int

getCount()

杩斿洖鎬荤殑椤圭洰鎬绘暟

int

getInt(int columnIndex)

杩斿洖鎸囧畾鍒椾腑鐨勬暟鎹殑int鍨嬭〃绀?/p>

int

getColumnIndex(String columnName)

鎸夌粰瀹氱殑鍒楃殑鍚嶅瓧杩斿洖鍒楃殑绱㈠紩鍊硷紝濡傛灉涓嶅瓨鍦ㄥ垯杩斿洖 -1

瑕佸垱寤轰竴涓狢ursor(娓告爣)锛屽繀椤绘墽琛屼竴涓煡璇紝閫氳繃SQL浣跨敤rawQuery()鏂规硶鎴栨槸鏇寸簿蹇冪殑query()鏂规硶锛岃€屼笉鑳戒娇鐢╡xecSQL(String sql)鏂规硶銆備互涓嬩负Cursor鐨勪娇鐢ㄧず渚嬶細

Cursor cur=myDataBase.rawQuery("select * from test", null);

if(cur!=null){//娓告爣涓嶄负绌?/p>

//杩斿洖缁欏畾鍚嶇О鐨勫垪鐨勫熀浜?寮€濮嬬殑index锛屽鏋滆灞炴€у垪涓嶅瓨鍦ㄥ垯杩斿洖-1

//閫氳繃瀹冧滑鐨刬ndex鏉ユ绱㈠睘鎬у€?/p>

int numColumn=cur.getColumnIndex("someNumber");

if(cur.moveToFirst()){ //cur.moveToFirst()璁╂父鏍囨寚鍚戠涓€琛岋紝濡傛灉娓告爣鎸囧悜绗竴琛岋紝鍒欒繑鍥瀟rue

do {

int num=cur.getInt(numColumn);//鑾峰緱褰撳墠琛岃灞炴€х殑鍊?/p>

/*Cursor鎻愪緵浜嗕笉鍚岀殑鏂规硶鏉ュ洖绱笉鍚岀殑鏁版嵁绫诲瀷锛屼緥濡俫etInt(int index)/getString(int index)绛夌瓑*/

/*鍋氫竴浜涗簨鎯?/

} while (cur.moveToNext());

/*娓告爣绉诲姩鍒颁笅涓€琛岋紝濡傛灉娓告爣宸茬粡鍒拌揪缁撴灉闆嗕腑鐨勬渶鍚庯紝鍗虫病鏈夎鍙互绉诲姩鏃讹紝鍒欒繑鍥瀎alse*/

//鍏朵粬鍙兘绉诲姩鐨勬槸 moveToPrevious() 鍜宮oveToFirst()鏂规硶

}

}

鍦ㄥ疄闄呬娇鐢ㄧ殑鏃跺€欙紝閫氬父閮借灏佽涓€涓户鎵縎QLiteOpenHelper绫荤殑鏁版嵁搴撴搷浣滅被銆係QLiteOpenHelper绫绘槸涓€涓娊璞$殑杈呭姪绫伙紝涓昏鐢ㄦ潵鐢熸垚涓€涓暟鎹簱骞跺鏁版嵁搴撶殑鐗堟湰杩涜绠$悊锛屽湪SQLiteOpenHelper绫荤殑鏋勯€犳柟娉曚腑鍒嗗埆浼犲叆Context銆佹暟鎹簱鍚嶇О锛孋ursorFactory(涓€鑸紶鍏ull锛屽惁鍒欎负榛樿鏁版嵁搴?锛屾暟鎹簱鐗堟湰鍙?涓嶈兘涓鸿礋鏁?銆傚湪SQLiteOpenHelper涓鍏堟墽琛岀殑鏄痮nCreate鏂规硶(褰撴暟鎹簱绗竴娆″垱寤烘椂)锛屼竴鑸湪杩欎釜鏂规硶閲岀敓鎴愭暟鎹〃銆傝娉ㄦ剰鐨勬槸锛屽湪鏋勯€犲嚱鏁版椂骞舵病鏈夌湡姝e垱寤烘暟鎹簱锛岃€屾槸鍦ㄨ皟鐢╣etWriteableDatabase()鎴栬€単etReadableDatabase()鏂规硶鐨勬椂鍊欑郴缁熸墠浼氱湡姝e垱寤烘暟鎹簱锛屽鏋滃綋鏃剁郴缁熶腑涓嶅瓨鍦ㄨ繖涓暟鎹簱锛岀郴缁熶細鑷姩鐢熸垚涓€涓暟鎹簱锛岀劧鍚庤繑鍥濻QLiteDatabase瀵硅薄銆傚湪缁ф壙杩欎釜绫荤殑鏃跺€欙紝瑕佸疄鐜伴噷闈㈢殑3涓柟娉曪紝鍏朵腑鍓嶄袱涓柟娉曟槸蹇呴』閲嶅啓鐨勩€傝濡備笅琛ㄦ牸锛?/p>

杩斿洖鍊?/strong>

鍑芥暟

澶囨敞

public void

onCreate(SQLiteDatabase db)

鍦ㄦ暟鎹簱绗竴娆$敓鎴愭椂浼氳皟鐢ㄨ繖涓柟娉曪紝涓€鑸垜浠湪杩欎釜鏂规硶閲岀敓鎴愭暟鎹〃

public void

onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)

褰撴暟鎹簱闇€瑕佸崌绾ф椂锛岀郴缁熶細鑷姩璋冪敤杩欎釜鏂规硶锛屼竴鑸垜浠湪杩欎釜鏂规硶閲屽垹闄ゆ暟鎹〃锛屽苟寤虹珛鏂扮殑鏁版嵁琛紝骞舵牴鎹疄闄呴渶姹傚仛鍏朵粬鐨勬搷浣?/p>

public void

onOpen(SQLiteDatabase db)

杩欐槸鎵撳紑鏁版嵁搴撴椂鐨勫洖璋冨嚱鏁帮紝涓€鑸笉浼氱敤鍒?/p>

浠ヤ笅绀轰緥浠g爜璇︾粏缁欏嚭涓€灏佽濂界殑鏁版嵁搴撴搷浣滅被锛?/p>

public class MyDbHelper {
    public static final String KEY_TITLE = "title";    //鏁版嵁琛ㄥ瓧娈?/span>
    public static final String KEY_BODY = "body";    //鏁版嵁琛ㄥ瓧娈?/span>
    public static final String KEY_ROWID = "_id";    //鏁版嵁琛ㄥ瓧娈?/span>
 
    private DatabaseHelper mDbHelper;    //SQLiteOpenHelper瀹炰緥瀵硅薄
    private SQLiteDatabase mDb;    //鏁版嵁搴撳疄渚嬪璞?/span>
    
    //鏁版嵁琛ㄥ垱寤鸿鍙?/span>
    private static final String DATABASE_CREATE  = "create table notes (_id integer primary key autoincrement, "
                    + "title text not null, body text not null);";
 
    private static final String DATABASE_NAME = "data";    //鏁版嵁搴撳悕
    private static final String DATABASE_TABLE = "notes";    //鏁版嵁搴撹〃鍚?/span>
    private static final int DATABASE_VERSION = 2;    //鏁版嵁搴撶増鏈彿
 
    private final Context mCtx;    //涓婁笅鏂囧疄渚?/span>
 
    private static class DatabaseHelper extends SQLiteOpenHelper {    //鏁版嵁搴撹緟鍔╃被
        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(DATABASE_CREATE);
        }
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS notes");
            onCreate(db);
        }
    }
 
    /**
     * Constructor - takes the context to allow the database to be opened/created
     * 
     * @param ctx the Context within which to work
     */
    public MyDbHelper(Context ctx) {
        this.mCtx = ctx;
    }
 
    /**
     * Open the notes database. If it cannot be opened, try to create a new instance of the database. 
* If it cannot be created, throw an exception to signal the failure
     * 
     * @return this (self reference, allowing this to be chained in an initialization call)
     * @throws SQLException if the database could be neither opened or created
     */
    public MyDbHelper open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }
    
    public void close() {
        mDbHelper.close();
    }
 
 
    /**
     * Create a new note using the title and body provided. If the note is successfully created 
* return the new rowId for that note, otherwise return a -1 to indicate failure.
     * 
     * @param title the title of the note
     * @param body the body of the note
     * @return rowId or -1 if failed
     */
    public long createNote(String title, String body) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_TITLE, title);
        initialValues.put(KEY_BODY, body);
        return mDb.insert(DATABASE_TABLE, null, initialValues);
    }
 
    /**
     * Delete the note with the given rowId
     * 
     * @param rowId id of note to delete
     * @return true if deleted, false otherwise
     */
    public boolean deleteNote(long rowId) {
        return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }
 
    /**
     * Return a Cursor over the list of all notes in the database
     * 
     * @return Cursor over all notes
     */
    public Cursor fetchAllNotes() {
        return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE,KEY_BODY}, null, null, null, null, null);
    }
 
    /**
     * Return a Cursor positioned at the note that matches the given rowId
     * 
     * @param rowId id of note to retrieve
     * @return Cursor positioned to matching note, if found
     * @throws SQLException if note could not be found/retrieved
     */
    public Cursor fetchNote(long rowId) throws SQLException {
 
        Cursor mCursor = mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,KEY_TITLE, 
KEY_BODY}, KEY_ROWID + "=" + rowId, null, null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }
 
    /**
     * Update the note using the details provided. The note to be updated is specified using the rowId, 
*and it is altered to use the title and body values passed in
     * 
     * @param rowId id of note to update
     * @param title value to set note title to
     * @param body value to set note body to
     * @return true if the note was successfully updated, false otherwise
     */
    public boolean updateNote(long rowId, String title, String body) {
        ContentValues args = new ContentValues();
        args.put(KEY_TITLE, title);
        args.put(KEY_BODY, body);
        return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
    }
}

杞嚜锛?a href="http://www.cnblogs.com/jerrychoi/archive/2010/04/15/1712704.html">http://www.cnblogs.com/jerrychoi/archive/2010/04/15/1712704.html

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Android(安卓)对话框
  2. android软键盘隐藏总结
  3. android4.2上获取应用程序大小的变更点
  4. Android(安卓)解决Handler在运行时加载报
  5. Android中MediaButtonReceiver广播监听器
  6. Android中各种onTouch事件
  7. Android修炼之道—时间测量
  8. Get the Android(安卓)SDK---获取Android
  9. php直播源码安卓自定义Dialog设置自动消
  10. Android(安卓)onMeasure、Measure、measu