数据库工具类
数据库工具类包含有帮助你管理数据库的函数
目录
初始化数据库工具类
重要提示: 初始化数据库工具类之前,你的数据库驱动必须已经运行,因为工具类依赖于此。
加载工具类:
$this->load->dbutil()
一旦初始化完毕,你可以通过 $this->dbutil 对象来访问成员函数:
$this->dbutil->some_function()
$this->dbutil->list_databases()
返回一个含有数据库名的数组:
$dbs = $this->dbutil->list_databases();
foreach($dbs as $db)
{
echo $db;
}
$this->dbutil->optimize_table('table_name');
注意: 此特性仅在MySQL/MySQLi数据库中可用。
允许你优化第一个参数为表名的表。基于操作的成功或失败返回TRUE或FALSE:
if ($this->dbutil->optimize_table('table_name'))
{
echo 'Success!';
}
注意: 并非所有数据库平台都支持表优化
$this->dbutil->repair_table('table_name');
注意: 此特性仅在MySQL/MySQLi数据库中可用。
允许你修复第一个参数为表名的表。基于操作的成功或失败返回TRUE或FALSE:
if ($this->dbutil->repair_table('table_name'))
{
echo 'Success!';
}
注意: 并非所有数据库平台都支持表修复
$this->dbutil->optimize_database();
注意: 此特性仅在MySQL/MySQLi数据库中可用。
Permits you to optimize the database your DB class is currently connected to. Returns an array containing the DB status messages or FALSE on failure.
$result = $this->dbutil->optimize_database();
if ($result !== FALSE)
{
print_r($result);
}
Note: Not all database platforms support table optimization.
$this->dbutil->csv_from_result($db_result)
Permits you to generate a CSV file from a query result. The first parameter of the function must contain the result object from your query. Example:
$this->load->dbutil();
$query = $this->db->query("SELECT * FROM mytable");
echo $this->dbutil->csv_from_result($query);
The second and third parameters allows you to set the delimiter and newline character. By default tabs are used as the delimiter and "\n" is used as a new line. Example:
$delimiter = ",";
$newline = "\r\n";
echo $this->dbutil->csv_from_result($query, $delimiter, $newline);
Important: This function will NOT write the CSV file for you. It simply creates the CSV layout. If you need to write the file use the File Helper.
$this->dbutil->xml_from_result($db_result)
Permits you to generate an XML file from a query result. The first parameter expects a query result object, the second may contain an optional array of config parameters. Example:
$this->load->dbutil();
$query = $this->db->query("SELECT * FROM mytable");
$config = array (
'root' => 'root',
'element' => 'element',
'newline' => "\n",
'tab' => "\t"
);
echo $this->dbutil->xml_from_result($query, $config);
Important: This function will NOT write the XML file for you. It simply creates the XML layout. If you need to write the file use the File Helper.
$this->dbutil->backup()
允许你备份整个数据库或者独立的表。备份数据可以压缩为Zip或Gzip格式。
注意: 该特性仅在使用MySQL数据库时有效。
备注:由于PHP执行时间和内存的限制,备份巨大的数据库可能不太容易成功。如果你的数据库非常大,你可能需要直接从命令行执行相关命令;或者如果你没有相应权限,你可能需要服务器管理员为你做这件事。
用法:
// Load the DB utility class
$this->load->dbutil();
// Backup your entire database and assign it to a variable
$backup =& $this->dbutil->backup();
// Load the file helper and write the file to your server
$this->load->helper('file');
write_file('/path/to/mybackup.gz', $backup);
// Load the download helper and send the file to your desktop
$this->load->helper('download');
force_download('mybackup.gz', $backup);
设置备份参数
Backup preferences are set by submitting an array of values to the first parameter of the backup function. Example:
$prefs = array(
'tables' => array('table1', 'table2'), // Array of tables to backup.
'ignore' => array(), // List of tables to omit from the backup
'format' => 'txt', // gzip, zip, txt
'filename' => 'mybackup.sql', // File name - NEEDED ONLY WITH ZIP FILES
'add_drop' => TRUE, // Whether to add DROP TABLE statements to backup file
'add_insert' => TRUE, // Whether to add INSERT data to backup file
'newline' => "\n" // Newline character used in backup file
);
$this->dbutil->backup($prefs);
备份参数说明
| 参数 | 默认值 | 选项 | 描述 |
|---|---|---|---|
| tables | 空数组 | 无 | 你想备份的数据表数组,如果留空将备份所有数据表. |
| ignore | 空数组 | 无 | 忽略备份的数据表数组 |
| format | gzip | gzip, zip, txt | 导出文件的格式 |
| filename | 当前日期/时间 | 无 | 备份文件名. 如果您使用了zip压缩这个名字是必填的. |
| add_drop | TRUE | TRUE/FALSE | 是否在您的输出的SQL文件里包含DROP TABLE声明. |
| add_insert | TRUE | TRUE/FALSE | 是否在您的输出的SQL文件里包含INSERT声明. |
| newline | "\n" | "\n", "\r", "\r\n" | 使用在您输出的SQL文件里的换行符类型. |