Yii - DAO
来源:互联网 发布:云计算在企业中的应用 编辑:程序博客网 时间:2024/06/05 07:55
http://www.yiiframework.com/doc/guide/1.1/en/database.dao
$connection=new CDbConnection($dsn,$username,$password);
$connection->active=true;
- SQLite:
sqlite:/path/to/dbfile
- MySQL:
mysql:host=localhost;dbname=testdb
- PostgreSQL:
pgsql:host=localhost;port=5432;dbname=testdb
- SQL Server:
mssql:host=localhost;dbname=testdb
- Oracle:
oci:dbname=//localhost:1521/testdb
'components'=>array( ...... 'db'=>array( 'class'=>'CDbConnection', 'connectionString'=>'mysql:host=localhost;dbname=testdb', 'username'=>'root', 'password'=>'password', 'emulatePrepare'=>true, // needed by some MySQL installations ), ),
$connection=Yii::app()->db;
$command=$connection->createCommand($sql);execute(): returns thenumber of rows that are affectedquery(): CDbDataReader instance from which one can traverse the resulting rows of dataqueryXXX()
methods are also implemented whichdirectly return the query results.
$rowCount=$command->execute(); // execute the non-query SQL$dataReader=$command->query(); // execute a query SQL$rows=$command->queryAll(); // query and return all rows of result$row=$command->queryRow(); // query and return the first row of result$column=$command->queryColumn(); // query and return the first column of result$value=$command->queryScalar(); // query and return the first field in the first row
$dataReader=$command->query();while(($row=$dataReader->read())!==false) { ... }foreach($dataReader as $row) { ... }$rows=$dataReader->readAll();
$transaction=$connection->beginTransaction();try{ $connection->createCommand($sql1)->execute(); $connection->createCommand($sql2)->execute(); //.... other SQL executions $transaction->commit();}catch(Exception $e) // an exception is raised if a query fails{ $transaction->rollback();}
// an SQL with two placeholders ":username" and ":email"$sql="INSERT INTO tbl_user (username, email) VALUES(:username,:email)";$command=$connection->createCommand($sql);// replace the placeholder ":username" with the actual username value$command->bindParam(":username",$username,PDO::PARAM_STR);// replace the placeholder ":email" with the actual email value$command->bindParam(":email",$email,PDO::PARAM_STR);$command->execute();// insert another row with a new set of parameters$command->bindParam(":username",$username2,PDO::PARAM_STR);$command->bindParam(":email",$email2,PDO::PARAM_STR);$command->execute();
6. Binding Columns
When fetching query results, one can also bind columns with PHP variablesso that they are automatically populated with the latest data each time arow is fetched.
$sql="SELECT username, email FROM tbl_user";$dataReader=$connection->createCommand($sql)->query();// bind the 1st column (username) with the $username variable$dataReader->bindColumn(1,$username);// bind the 2nd column (email) with the $email variable$dataReader->bindColumn(2,$email);while($dataReader->read()!==false){ // $username and $email contain the username and email in the current row}
- Yii - DAO
- YII访问数据库(Yii Dao)
- yii DAO层
- YII 数据处理DAO
- yii AR DAO数据库操作
- YII中的AR与DAO
- yii数据库存储对象DAO
- yii DAO增删改查
- Yii framwork crud 深入YII DAO 笔记三
- Yii框架之数据访问对象 (DAO)
- Yii数据访问对象DAO操作数据库
- Yii deleteByAttributs 用法,慎用Dao的delete
- PHP Yii使用DAO操作数据库
- 06. Yii 2.0 数据库交互 - DAO
- Yii的AR模式及DAO
- Yii Framework使用DAO方式查询分页代码
- Yii Framework 开发教程(24) 数据库-DAO 示例
- Yii Framework 开发教程(24) 数据库-DAO 示例
- Java List ArrayList用法详解
- 修改系统默认值
- 『FFMPEG』ffmpeg-0.11.1的命令行解析与控制
- sed c\命令,IP地址和16进制数互相转换等一些精彩shell代码
- 从键盘任意输入数字a和b,并计算出a的b次方等于多少
- Yii - DAO
- Android应用编程--基本概念
- 如何实现标准TCODE的屏幕增强(HOWTO:Implement a screen exit to a standard SAP transaction)
- oracle plsql developer
- MARS的《Android学习路线》
- 配置文件读取(二)
- 用C模拟异常
- qt creator报错error while building deploying project
- Qt Qss使用