关于PEAR DB的使用

  通过 Pear DB可以从查询结果获得更多有用的数据信息 。这些方法有:
numRows(): 通过一个"SELECT" 查询返回所有数据的数量。
numCols():通过一个"SELECT" 查询返回所有的列。
affectedRows(): 通过("INSERT", "UPDATE" or "DELETE")操作返回所有受影响的数据行数。
tableInfo():通过一个"SELECT" 查询返回一个包含数据信息的数组。


可用方法列表:
<?php
/*
* From the DB_(driver) objects
*/
// get the object with, ie:
$db = DB::connect('mysql://user:pass@localhost/my_db');

// Set options
$db->setErrorHandling();
$db->setFetchmode();
// Information
$db->affectedRows();
$db->tableInfo();
// Database manipulation
$db->query();
// Data fetch
$db->nextId();
$db->getOne();
$db->getRow();
$db->getCol();
$db->getAssoc();
$db->getAll();
// Place holders and execute related
$db->quote();
$db->prepare();
$db->execute();
$db->executeMultiple();
// Transactions
$db->autoCommit();
$db->commit();
$db->rollback();
// Disconnection
$db->disconnect();

/*
* From DB_result objects
*/
// get the object with, ie:
$res = $db->query('select * from foo');

// Data fetch
$res->fetchRow();
$res->fetchInto();
// Result Info
$res->numCols();
$res->numRows();
$res->tableInfo();
// Free
$res->free();

/*
* From DB_error objects
*/
// get the object with, ie:
$error = $db->query('select * from no_table');

$error->getMessage();
$error->getDebugInfo();
$error->toString();
?>
//
///其他详细使用

///
PEAR DB 的连接和断开

<?php
// The pear base directory must be in your include_path
require_once 'DB.php';
$user = 'foo';
$pass = 'bar';
$host = 'localhost';
$db_name = 'clients_db';

// Data Source Name: This is the universal connection string
$dsn = "mysql://$user:$pass@$host/$db_name";

// DB::connect will return a Pear DB object on success
// or a Pear DB Error object on error
// You can also set to TRUE the second param
// if you want a persistent connection:
// $db = DB::connect($dsn, true);
$db = DB::connect($dsn);

// With DB::isError you can differentiate between an error or
// a valid connection.
if (DB::isError($db)) {
       die (
$db->getMessage());
}
....
// You can disconnect from the database with:
$db->disconnect();
?>
=======================

执行数据库并获得数据
<?php
// Once you have a valid DB object
...
$sql = "select * from clients";
// If the query is a "SELECT", $db->query will return
// a DB Result object on success.
// Else it simply will return a DB_OK
// On failure it will return a DB Error object.
$result = $db->query($sql);
// Always check that $result is not an error
if (DB::isError($result)) {
       die (
$result->getMessage());
}

// Once you have a valid DB Result object
...
// Get each row of data on each iteration until
// there is no more rows
while ($row = $result->fetchRow()) {
   
$id = $row[0];
}

?>

===============================
选择获取数据的格式
<?php
$res
= $db->query('select id, name, email from users');
$row = $res->fetchRow($mode);

//With $mode = DB_FETCHMODE_ORDERED
//The default behavior is to return an ordered array.
$row = array (
   
0 => <column "id" data>,
   
1 => <column "name" data>,
   
2 => <column "email" data>
);

$id = $row[0];

//With $mode = DB_FETCHMODE_ASSOC
//Returns an associative array with column names as array keys:
$row = array (
   
'id'    => <column "id" data>,
   
'name'  => <column "name" data>,
   
'email' => <column "email" data>
);

$id = $row['id'];

//With $mode = DB_FETCHMODE_OBJECT
//Returns a DB_row object with column names as properties:
$row = db_row Object
(
   [
id]    => <column "id" data>,
   [
name]  => <column "name" data>,
   [
email] => <column "email" data>
)

$id = $row->id;
?>
=============================
控制获取数据的数量
<?php
...
// the row to start fetching
$from = 50;
// how many results per page
$res_per_page = 10;
// the last row to fetch for this page
$to = $from + $res_per_page;
foreach (
range($from, $to) as $rownum) {
   if (!
$row = $res->fetchrow($fetchmode, $rownum)) {
       break;
   }
   
$id = $row[0];
   ....
}
?>
===========================
清楚结果释放变量
<?php
...
$result = $db->query('SELECT * FROM clients');
while (
$row = $result->fetchRow()) {
   ...
}
$result->free();
?>
===========================
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Pear Admin是一套基于Flask框架的后台管理系统,如果需要进行二次开发,可以按照以下步骤进行: 1. 安装Pear Admin 首先,需要安装Pear Admin。可以使用pip安装: ``` pip install pear-admin ``` 2. 创建项目 使用`pear-admin create`命令创建项目: ``` pear-admin create project_name ``` 这个命令会生成一个项目模板,包含Flask的基本架构和Pear Admin的默认配置。 3. 定义数据模型 在`app/models.py`中定义数据模型,比如: ```python from pear.models import db, BaseModel class User(BaseModel): __tablename__ = 'users' name = db.Column(db.String(64), nullable=False) age = db.Column(db.Integer, nullable=False) ``` 这个例子中,我们定义了一个名为`User`的模型,包含了`name`和`age`两个字段。 4. 编写视图函数 在`app/views.py`中编写视图函数,比如: ```python from pear.views import admin from .models import User @admin.route('/users') def user_list(): users = User.query.all() return admin.render('user_list.html', users=users) ``` 这个例子中,我们编写了一个名为`user_list`的视图函数,用来展示所有的用户信息。 5. 编写模板 在`app/templates`中编写模板文件,比如: ```html {% extends 'admin/layout.html' %} {% block content %} <table> <thead> <tr> <th>姓名</th> <th>年龄</th> </tr> </thead> <tbody> {% for user in users %} <tr> <td>{{ user.name }}</td> <td>{{ user.age }}</td> </tr> {% endfor %} </tbody> </table> {% endblock %} ``` 这个例子中,我们编写了一个名为`user_list.html`的模板文件,用来展示用户信息。 6. 运行项目 使用以下命令运行项目: ``` python manage.py runserver ``` 打开浏览器,在地址栏输入`http://localhost:5000/admin/users`,即可看到所有的用户信息。 以上是Pear Admin Flask二次开发的基本流程,具体实现可根据实际需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值