SQLite的insert方法參數(shù)是怎樣的

這篇文章給大家介紹SQLite的insert方法參數(shù)是怎樣的,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

成都創(chuàng)新互聯(lián)是一家專注網(wǎng)站建設(shè)、網(wǎng)絡(luò)營銷策劃、小程序制作、電子商務(wù)建設(shè)、網(wǎng)絡(luò)推廣、移動互聯(lián)開發(fā)、研究、服務(wù)為一體的技術(shù)型公司。公司成立十載以來,已經(jīng)為超過千家會所設(shè)計各業(yè)的企業(yè)公司提供互聯(lián)網(wǎng)服務(wù)。現(xiàn)在,服務(wù)的超過千家客戶與我們一路同行,見證我們的成長;未來,我們一起分享成功的喜悅。

看到一個API的參數(shù)不懂的時候,我們會做什么呢!Baidu,Google。很少回去看源碼的吧,我也是,太懶了,批評一下。啪啪~

先看一段創(chuàng)建數(shù)據(jù)庫的代碼吧。

db.execSQL("CREATE TABLE users (" + "_id INTEGER PRIMARY KEY, " + "username TEXT, " + "realname TEXT, "
				+ "nsid TEXT, " + "buddy_icon BLOB," + "last_update INTEGER);");

我們能看到其中有一個類型是我們不常用的,BLOB,學(xué)過數(shù)據(jù)庫的人肯定都知道了,但是我還是要說一下,這個表示的是二進(jìn)制數(shù)據(jù)類型,比如我們可以將一個圖片的數(shù)據(jù)存入其中,當(dāng)然小圖可以,大圖就算了嘛。for example:

addUser(db, "Bob Lee", "Bob Lee", "45701389@N00", R.drawable.boblee_buddyicon);
private void addUser(SQLiteDatabase db, String userName, String realName, String nsid, int icon) {

		final ContentValues values = new ContentValues();
		values.put(COLUMN_USERNAME, userName);
		values.put(COLUMN_REALNAME, realName);
		values.put(COLUMN_NSID, nsid);
		values.put(COLUMN_LAST_UPDATE, System.currentTimeMillis());

		final Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), icon);
		writeBitmap(values, COLUMN_BUDDY_ICON, bitmap);

		db.insert(TABLE_USERS, COLUMN_LAST_UPDATE, values);
	}

	static void writeBitmap(ContentValues values, String name, Bitmap bitmap) {
		if (bitmap != null) {
			// Try go guesstimate how much space the icon will take when
			// serialized
			// to avoid unnecessary allocations/copies during the write.
			int size = bitmap.getWidth() * bitmap.getHeight() * 2;
			ByteArrayOutputStream out = new ByteArrayOutputStream(size);
			try {
				bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
				out.flush();
				out.close();

				values.put(name, out.toByteArray());
			} catch (IOException e) {
				// Ignore
			}
		}
	}

好了,閑話不多說了,今天要說的重點是其中的:

db.insert(TABLE_USERS, COLUMN_LAST_UPDATE, values);

參數(shù)問題,這里貼一下API說明:

public long insert (String table, String nullColumnHack, ContentValues values)

Added in API level 1
Convenience method for inserting a row into the database.

Parameters
table	the table to insert the row into
nullColumnHack	optional; may be null. SQL doesn't allow inserting a completely empty row without naming at least one column name. If your provided values is empty, no column names are known and an empty row can't be inserted. If not set to null, the nullColumnHack parameter provides the name of nullable column name to explicitly insert a NULL into in the case where your values is empty.
values	this map contains the initial column values for the row. The keys should be the column names and the values the column values
Returns
the row ID of the newly inserted row, or -1 if an error occurred

好吧,我相信英文不好的同學(xué)肯定會與我一樣,對第二個參數(shù)的說明還是有點糊涂的,行吧,那就Baidu,有道orGoogle翻譯,不過這些終究是翻譯工具,只能讓你有個一知半解,最終還是需要實踐才能得出最終答案的。

這里用國語詳細(xì)解釋下這三個參數(shù)的意義:

table:數(shù)據(jù)的表的名稱

nullColumnHack:當(dāng)values參數(shù)為空或者里面沒有內(nèi)容的時候,我們insert是會失敗的(底層數(shù)據(jù)庫不允許插入一個空行),為了防止這種情況,我們要在這里指定一個列名,到時候如果發(fā)現(xiàn)將要插入的行為空行時,就會將你指定的這個列名的值設(shè)為null,然后再向數(shù)據(jù)庫中插入。

values:ContentValues對象,差不多就是一個map.通過鍵值對的形式存儲值,鍵表示的是Column,值就是對應(yīng)Column的值咯。

關(guān)于SQLite的insert方法參數(shù)是怎樣的就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

分享標(biāo)題:SQLite的insert方法參數(shù)是怎樣的
文章轉(zhuǎn)載:http://muchs.cn/article44/ispoee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站網(wǎng)站導(dǎo)航、手機網(wǎng)站建設(shè)、面包屑導(dǎo)航、營銷型網(wǎng)站建設(shè)定制開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計公司