首页 技术 正文
技术 2022年11月21日
0 收藏 783 点赞 3,361 浏览 1885 个字

TP+ORACLE插入数据BUG修复以及获取自增Id支持getLastInsID方法 
这些天在做Api接口时候,发现用TP操作Oracle数据库,发现查询修改删除都能执行, 
但一旦执行插入操作老是报错。类似问题比如: http://www.thinkphp.cn/bug/3286.html 
花了点时间仔细研究一下,发现是BUG. 
下面是我的解决办法: 
针对版本:ThinkPHP3.2.3 
BUG修复: 
修改文件:Db\Driver\Oracle.class.PHP

找到 execute方法, 
找到:this−>initConnect(true);这句前面加上bind = $this->bind; 这句:

public function execute($str,$fetchSql=false) {
$bind = $this->bind; //新增这句
$this->initConnect(true);

找到:foreach ($this->bind as $key => $val) { 这句 
前面加上 $this->bind = $this->bind ? $this->bind : $bind; 这句:

$this->bind = $this->bind ? $this->bind : $bind; //新增这句
foreach ($this->bind as $key => $val) {

找到 $this->lastInsID = $this->_linkID->lastInsertId(); 这句 
将其修改为:

 //修改:
//$this->lastInsID = $this->_linkID->lastInsertId();
$this->lastInsID = $this->lastInsertId($this->table);

Oracle.class.php文件中新增以下代码:

/**
* 取得Oracle最近插入的ID
* @access public
*/
public function lastInsertId($sequence = '') {
try {
$lastInsID = $this->_linkID->lastInsertId();
} catch(\PDOException $e) {
//对于驱动不支持PDO::lastInsertId()的情况
try {
$lastInsID = 0;
$seqPrefix = C("DB_SEQUENCE_PREFIX") ? C("DB_SEQUENCE_PREFIX") : 'seq_';
$sequence = $sequence ? $sequence : $seqPrefix.$this->table;
$q = $this->query("SELECT {$sequence}.CURRVAL as t FROM DUAL");
if($q) {
$lastInsID = $q[0]['t'];
}
} catch(\Exception $e) {
//print "Error!: " . $e->getMessage() . "</br>";
//exit;
}
}
return $lastInsID;
}

调用方法: 
1.数据库配置:

 'DB_PREFIX'=>'tb_',//表名前缀
'DB_SEQUENCE_PREFIX' => 'seq_',//序列名前缀,每个表对应的序列应为: 序列名前缀+表名
'DB_TRIGGER_PREFIX' => 'tig_',//触发器名前缀

2.先创建user数据表 
表字段:id, username, password

3.然后创建[序列+触发器]

----创建序列
create sequence seq_user
increment by 1
start with 1
nomaxvalue
nominvalue
nocache;
----创建触发器
create or replace trigger "tig_user"
before insert on tb_user
for each row when(new.id is null)
begin
select seq_user.nextval into :new.id from dual;
end;

4.最后一步,在UserAction中写插入数据代码如下:

$data = array(
'phone'=>$phone,
'password'=>md5($password)
);
$r = M('user')->field(true)->add($data); //执行插入并返回上次插入Id
if($r){
//$r = M('user')->getLastInsID(); //获取上次插入Id
echo '上次插入记录:'.$r;
}else{
$this->error('操作失败');
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,031
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,148
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,860