二、Blob(大二进制对象)函数
Blob函数得到Blob数据类型的信息、完成数据类型转换以及操作Blob类型数据。
1、Blob()
功 能:将字符串转换成Blob类型数据。
语 法:Blob ( text )
参 数:text: string类型,指定要转换的数据。
返回值:Blob。函数执行成功时返回转换后的Blob类型数据;如果text参数的值为NULL,则Blob()函数返回NULL。
示 例:Blob B
B = Blob(“成都”)
2、BlobEdit()
功 能:将任意类型的数据插入到Blob类型变量的指定位置。
语 法:BlobEdit ( blobvariable, n, data )
参 数:blobvariable:Blob类型的已初始化变量,将向该变量中插入数据。
n:指定插入位置,有效值在1到4,294,967,295之间。
data:要插入到Blob类型变量中的数据,其数据类型可以是任何有效的PowerBuilder数据类型。
返回值:Unsigned long。函数执行成功时返回下次可以插入数据的位置;如果blobvariable变量中空间不够或任何参数的值为NULL,则BlobEdit()函数返回NULL。
示 例:下面的代码把一幅位图的数据复制到Blob类型变量emp_photo的起始位置,并把下次数据复制位置保存到变量nbr中,之后将当天的日期复制到emp_photo变量中位图数据的后面。
blob{1000} emp_photo
blob temp
date pic_date
ulong nbr
... // Read BMP file containing employee picture
... // into temp using FileOpen and FileRead.
pic_date = Today()
nbr = BlobEdit(emp_photo, 1, temp)
BlobEdit(emp_photo, nbr, pic_date)
UPDATEBLOB Employee
SET pic = :emp_photo
WHERE ...
3、BlobMid()
功 能:从Blob变量中提取出一段数据。提取之后,原Blob变量内容不变。
语 法:BlobMid ( data, n {, length } )
参 数:data:Blob类型的数据。
n:指定要提取数据的起始位置,有效值在1到4,294,967,295之间。
length:可选项,指定要提取数据的长度,以字节为单位,有效值在1到4,294,967,295之间。缺省时提取从指定位置n到末尾的所有数据
返回值:Blob。函数执行成功时返回指定字节数的数据。如果n的值大于data的字节数,函数返回空。如果n与length的和超过了data数据的字节数,那么BlobMid()函数返回剩下的数据,数据长度将小于参数length的值。如果任何参数的值为NULL,则BlobMid()函数返回NULL。
示 例:In this example, the first call to BlobMid stores 10 bytes of the blob datablob starting at position 5 in the blob data_1; the second call stores the bytes of datablob from position 5 to the end in data_2:
blob data_1, data_2, datablob
... // Read a blob datatype into datablob.
data_1 = BlobMid(datablob, 5, 10)
data_2 = BlobMid(datablob, 5)
This code copies a bitmap in the blob emp_photo starting at position 1, stores the position at which the next copy can begin in nbr, and then copies a date into the blob emp_photo after the bitmap data. Then, using the date's start position, it extracts the date from the blob and displays it in the StaticText st_1:
blob{1000} emp_photo
blob temp
date pic_date
ulong nbr
... // Read BMP file containing employee picture
... // into temp using FileOpen and FileRead.
pic_date = Today()
nbr = BlobEdit(emp_photo, 1, temp)
BlobEdit(emp_photo, nbr, pic_date)
st_1.Text = String(Date(BlobMid(emp_photo, nbr)))
4、Len()
功 能:得到Blob类型变量的数据长度,以字节为单位。
语 法:Len ( blob )
参 数:blob:Blob类型变量。
返回值:Long。函数执行成功时返回blob变量的长度,发生错误时返回-1。如果任何参数的值为NULL,则Len()函数返回NULL。
用 法:如果在说明Blob类型的变量时指定了变量长度,那么对该变量来说,Len()函数得到的就是这个指定的长度。如果在变量说明时未指定变量长度,那么PowerBuilder在赋值时调整长度,未赋值Blob类型变量的长度为0。
示 例:This statement returns 0:
Len("")
These statements store in the variable s_address_len the length of the text in the SingleLineEdit sle_address:
long s_address_len
s_address_len = Len(sle_address.Text)
The following scenarios illustrate how the declaration of blobs affects their length, as reported by Len.
In the first example, an instance variable called ib_blob is declared but not initialized with a size. If you call Len before data is assigned to ib_blob, Len returns 0. After data is assigned, Len returns the blob's new length.
The declaration of the instance variable is:
blob ib_blob
The sample code is:
long ll_len
ll_len = Len(ib_blob) // ll_len set to 0
ib_blob = Blob( "Test String")
ll_len = Len(ib_blob) // ll_len set to 11
In the second example, ib_blob is initialized to the size 100 when it is declared. When you call Len for ib_blob, it always returns 100. This example uses BlobEdit, instead of Blob, to assign data to the blob because its size is already established. The declaration of the instance variable is:
blob{100} ib_blob
The sample code is:
long ll_len
ll_len = Len(ib_blob) // ll_len set to 100
BlobEdit(ib_blob, 1, "Test String")
ll_len = Len(ib_blob) // ll_len set to 100
5、String()
功 能:将Blob类型变量的值转换成字符串类型。如果Blob类型变量的值不是文本型数据,String()函数将其值解释成字符。
语 法:String ( blob )
参 数:blob:要对其值进行类型转换的Blob类型变量。
返回值:String。函数执行成功时返回转换后的数据,失败时返回空字符串("")。如果blob变量中包含的数据不是字符串类型的数据,那么String()函数尽可能把数据解释为字符,并返回字符串类型数据。如果blob参数的值为NULL,则String()函数返回NULL。
示 例:This example converts the blob instance variable ib_sblob, which contains string data, to a string and stores the result in sstr:
string sstr
sstr = String(ib_sblob)
This example stores today's date and test status information in the blob bb. Pos1 and pos2 store the beginning and end of the status text in the blob. Finally, BlobMid extracts a "sub-blob" that String converts to a string. Sle_status displays the returned status text:
blob{100} bb
long pos1, pos2
string test_status
date test_date
test_date = Today()
IF DayName(test_date) = "Wednesday" THEN test_status = "Coolant Test"
IF DayName(test_date) = "Thursday" THEN test_status = "Emissions Test"
// Store data in the blob
pos1 = BlobEdit( bb, 1, test_date)
pos2 = BlobEdit( bb, pos1, test_status )
... // Some processing
// Extract the status stored in bb and display it
sle_status.text = String( BlobMid(bb, pos1, pos2 - pos1))