As you can see MDB::createLob() is passed an associative array. The value for the Type key may be one of the following: data, inputfile or outputfile. The first two are used when you want to write a LOB into the database. If you have the LOB stored in a variable you should use data while inputfile should be used to read the LOB directly from a file. Finally, outputfile should be used when you want to retrieve a LOB from the database. Depending on if you are using data or inputfile you need to specify a value for the Filename key or the Data key as seen in the above example. Now, we will store the above LOB's in the database.
如你能看到的,MDB::createLob() 被传递一个关系数组。Type 键的值可能是以下中的一个:data, inputfile 或者 outputfile。前两个用于你想要把 LOB 写入数据库的时候。如果你有一个储存在变量中的 LOB,你应当在 需要使用 inputfile 时从文件直接读取 LOB。最后,outpufile 应当在你想要从数据库中读取 LOB 时使用。取决于你是否使用数据或者 inputfile 你需要给 Filename 键或者 Data 键指定一个值,像上面的例子那样。现在,我们将把前面的 LOB 储存到数据库中去。
$p_query = $mdb->prepareQuery('INSERT INTO files (id, b_data, c_data) VALUES (1, ?, ?)');
$mdb->setParamBlob($p_query, 1 , $blob, 'b_data');
$mdb->setParamClob($p_query, 2 , $clob, 'c_data');
$result = $mdb->executeQuery($p_query);
In order to fetch the above file from the database we will need to first select the data from the database and create a LOB object using MDB::createLob(). This time we will set `Type' to `outputfile'.
为了从数据库中获取上面的文件,我们需要首先从数据库中选择数据并且使用 MDB::createLob() 创建 LOB 对象。这次我们将设置 'Type' 为 'outputfile'
$mdb->query('SELECT b_data FROM files WHERE id = 1');
$binary_lob = array(
'Type' => 'outputfile',
'Result' => $result,
'Row' => 0,
'Field' => 'b_data',
'Binary' => 1,
'FileName' => './myfile2.gif'
);
$blob = $mdb->createLob($binary_lob);
Now we can read the LOB from the result set using MDB::readLob(). Passing a length of 0 to MDB::readLob() means that the entire LOB is read and stored in the file we specified above. Once we are done we can free the resources. Alternatively, you can set any length larger than zero and read the LOB using a while loop checking MDB::endofLob().
现在我们能够使用 MDB::readLob() 从结果集中读取 LOB。传递长度 0 给 MDB::readLob() 意味着整个 LOB 被读取和储存在我们前面指定的文件中。一旦任务完成了,我们可以把资源释放了。你也可以设置任何大于零的长度并且使用一个 while 循环检查 MDB::endofLob() 来读取 LOB。
$mdb->readLob($blob, $data, 0);
It is important to note that you may not mix this method of fetching with the bulk fetching methods like MDB::fetchAll() as this will cause problems in most PHP database extensions. At some point MDB may be able to retrieve LOB's using the bulk fetching methods.
注意你不要把这个获取函数和 bulk 获取函数像 MDB::fetchAll()搞混了,因为这将在大部分 PHP 数据库扩展中导致问题。在一些时候,MDB 可能能够使用 bulk 获取函数获得 LOB。
As we have seen in this section MDB features its own set of native data types that are automatically mapped to native data types in the database. This ensures that no matter what data we send or retrieve from the database it will always be in the same format no matter what RDBMS is used. As I have mentioned in the opening paragraph of this section this obviously requires that the data types used in the database are what MDB expects. This requirement was made to ensure that the mapping is done with a minimal performance loss. The next section will teach us how MDB assists with using the correct data types in the database.
如我们在这节所见,MDB 特性本身的原生数据类型集自动映射于数据库中的原生数据类型。这保证了无论我们发送和从数据库接收什么样的数据,它都能与使用的 RDBMS 无关的使用相同的格式。如我在本节开篇已经提到的,这明显需要数据库使用的数据类型是 MDB 预期的。这种需要被用于确保映射所耗费的代价很小。下一节将教给我们 MDB 如何辅助在数据库中使用正确的数据类型。
Making use of XML schema files
使用 XML schema 文件