当前位置:网站首页>Kohana 数据库
Kohana 数据库
2022-07-06 13:45:00 【全栈程序员站长】
大家好,又见面了,我是全栈君。
只要不使用官方网站的教程,自己摸索出来的,有一个错误,当我们指了出来,哦,,好吧共同进步~
首先配置:modules\database\config\database.php
<?php
'default' => array(
'type' => 'pdo',
'connection' => array(
/**
* The following options are available for PDO:
*
* string dsn Data Source Name
* string username database username
* string password database password
* boolean persistent use persistent connections?
*/
'dsn' => 'mysql:host=localhost;dbname=kohana',
'username' => '******',//
'password' => '******',//
'persistent' => FALSE,
),
/**
* The following extra options are available for PDO:
*
* string identifier set the escaping identifier
*/
'table_prefix' => 'ko_',
'charset' => 'utf8',
'caching' => FALSE,
'profiling' => TRUE,
),能够配置多个数据库配置哦~配置好了之后就能够使用咯
配置完了就能够使用咯
在你的控制器中就能够这样
数据库实例
有两种数据库实例
1.DB数据库实例
2. database数据库实例
当中DB是对database的再次封装
以下描写叙述两个数据库实例的使用
Database
取得方法:
<?php
$database=Database::instance();//能够取得database实例
#例外在模型中,dababase做为模型构造函数的唯一一个传递參数,并在模型中有$this->_db属性
$database=$this->_db;用法:(如果在模型中)插入数据:<?php$sql="INSERT INTO `kohana`.`user` (`name` ,`age` )VALUES ( 'test', '1'), ( 'test2', '2')";
$dat=$this->_db->query(Database::INSERT,$sql,false);# return 返回的两个值中,第一个是自己主动增长的ID,假设有的话,第二是影响的行数 更新数据:
<?php
$sql="UPDATE `ko_users` SET `user_name` = 'test111q1' ";
$dat=$this->_db->query(Database::UPDATE,$sql,false);
#return 返回影响行数删除数据:<?php
$sql="DELETE FROM `kohana`.`user` WHERE `user`.`id` = 1";
$dat=$this->_db->query(Database::DELETE,$sql,false);
#return 返回影响行数查询数据:<?php $sql="select * from ko_com_var"; $res=$this->_db->query(Database::SELECT,$sql,false); #得到全部查询数据 $res->as_array(); #得到一条查询结果 $res->offsetGet(0); #取得特定记录的指定字段值 $res->get("name"); #移动指针并取得指定字段 $res->next()->get("name"); $res->prev()->get("name"); #计算取得结果总数 $res->count(); #还有其它方法不在一一罗列,请查看手冊其它经常使用帮助函数:
<?php
#过滤字符串用,不知道为什么放到这个单例中,该是公用才对~,也许是每中数据库的过滤的东西有区别吧
$str=$this->_db->escape("ddddd ddd");
#表前缀,这个经常使用~
$str=$this->_db->table_prefix();
#还有其它查看帮助,不介绍咯DB实例使用(下面演示在Kohana环境就可以)
有两种方式:
第一种:
下面的execute(); 有一个数据库适配器參数,当有多个数据连接的时候指定操作那个数据库,就是配置文件的那个KEY值
插入数据:
<?php $sql="INSERT INTO `kohana`.`user` (`name` ,`age` )VALUES ( 'test', '1'), ( 'test2', '2')"; #事实上也能够用Database::UPDATE,结果仅仅返回影响行数,只是还是按规范好.呵呵~,上述Database也能够 $dat=DB::query(Database::INSERT,$sql); $row=$dat->execute(); #返回的两个值中,第一个是自己主动增长的ID,假设有的话,第二是影响的行数 Kohana::debug($row);数据更新:
<?php
$sql="UPDATE `user` SET `name` = 'test2' WHERE `user`.`id` =1 ";
$dat=DB::query(Database::UPDATE,$sql);
$row=$dat->execute();
#返回影响行数
echo Kohana::debug($row);数据删除:<?php $sql="DELETE FROM `kohana`.`user` WHERE `user`.`id` = 1"; $dat=DB::query(Database::DELETE,$sql); $row=$dat->execute(); #返回影响行数 echo Kohana::debug($row);数据查询:
<?php$sql="select * from user";$dat=DB::query(Database::SELECT,$sql);#指定数据库取数据$row=$dat->execute($database)->offsetGet(0);#默认数据库取数据,和上面的Database一样,都是返回Database_Result_Cached对象,实现了迭代器模式$rus=$dat->execute();#取得部分结果$row=$rus->offsetGet(0);#取得全部结果$allrow=$rus->as_array();#取得特定记录的指定字段值$no1name=$rus->get("name");#移动数组指针,并取指定字段值$no2name=$rus->next()->get("name");#当前指针echo $rus->key();#移动数组指针,并取指定字段值echo $no1name=$rus->prev()->get('name');#取行数echo $rus->count();另外一种:(官网文档称之为查询器模式,不好用,呵呵,简单的能够用下)
插入数据:
<?php
$query = DB::insert('user', array('user', 'age'))
->values(array('test1', '11'));
$query->execute();
#返回和上面一样更新数据:
<?php
$query = DB::update('user')
->set(array('age' => '100'))
->where('user', '=', 'test1');
$query->execute();
#返回和上面一样删除数据:
<?php
$query = DB::delete('user')
->where('age', 'IN', array('100', '11'));
$query->execute();
#返回和上面一样查询数据:
<?php
$query = DB::select()->from('user')->where("id","=","1");
$res=$query->execute();
#和上面一样,$res是Database_Result_Cached对象
$res->as_array();
#其它方法不演示了~附注:数据绑定,抄官方实例一个,该非常easy,比較容易看懂<?php
$query = DB::query(Database::INSERT, 'INSERT INTO users (username, password) VALUES (:user, :pass)')
->bind(':user', $username)
->bind(':pass', $password);
foreach ($new_users as $username => $password){
$query->execute();
}基本上它,我不经常使用,我不形容了,只是主要介绍了完整的,
版权声明:本文博主原创文章,博客,未经同意不得转载。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/117064.html原文链接:https://javaforall.cn
边栏推荐
- b站视频链接快速获取
- Leetcode topic [array] -118 Yang Hui triangle
- Dialogue with Jia Yangqing, vice president of Alibaba: pursuing a big model is not a bad thing
- 十一、服务介绍及端口
- Five wars of Chinese Baijiu
- 20 large visual screens that are highly praised by the boss, with source code templates!
- C language char, wchar_ t, char16_ t, char32_ Relationship between T and character set
- Reinforcement learning - learning notes 5 | alphago
- [Li Kou brush questions] 32 Longest valid bracket
- 互联网快讯:吉利正式收购魅族;胰岛素集采在31省全面落地
猜你喜欢
![[Digital IC manual tearing code] Verilog automatic beverage machine | topic | principle | design | simulation](/img/75/c0656c4890795bd65874b4f2b16462.jpg)
[Digital IC manual tearing code] Verilog automatic beverage machine | topic | principle | design | simulation

Numpy download and installation

抖音将推独立种草App“可颂”,字节忘不掉小红书?

Earned value management EVM detailed explanation and application, example explanation

Internet News: Geely officially acquired Meizu; Intensive insulin purchase was fully implemented in 31 provinces

Michael smashed the minority milk sign

Why rdd/dataset is needed in spark

Reset Mikrotik Routeros using netinstall

用aardio写一个旋转验证码标注小工具

What can one line of code do?
随机推荐
14年本科毕业,转行软件测试,薪资13.5K
Leveldb source code analysis series - main process
Five wars of Chinese Baijiu
Quick news: the flybook players' conference is held online; Wechat payment launched "education and training service toolbox"
C# 如何在dataGridView里设置两个列comboboxcolumn绑定级联事件的一个二级联动效果
What about the spectrogram
LeetCode学习记录(从新手村出发之杀不出新手村)----1
[Yu Yue education] reference materials for surgical skills teaching in Tongji University
Fzu 1686 dragon mystery repeated coverage
首批入选!腾讯安全天御风控获信通院业务安全能力认证
Yuan Xiaolin: safety is not only a standard, but also Volvo's unchanging belief and pursuit
The relationship between root and coefficient of quadratic equation with one variable
Description of web function test
Huawei has launched attacks in many industries at the same time, and its frightening technology has made European and American enterprises tremble
Redistemplate common collection instructions opsforset (V)
抖音将推独立种草App“可颂”,字节忘不掉小红书?
Sql: stored procedures and triggers - Notes
Web开发小妙招:巧用ThreadLocal规避层层传值
Redistemplate common collection instructions opsforlist (III)
mysql根据两个字段去重