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; 这句:

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

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

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

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

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

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

  1. /**
  2. * 取得Oracle最近插入的ID
  3. * @access public
  4. */
  5. public function lastInsertId($sequence = '') {
  6. try {
  7. $lastInsID = $this->_linkID->lastInsertId();
  8. } catch(\PDOException $e) {
  9. //对于驱动不支持PDO::lastInsertId()的情况
  10. try {
  11. $lastInsID = 0;
  12. $seqPrefix = C("DB_SEQUENCE_PREFIX") ? C("DB_SEQUENCE_PREFIX") : 'seq_';
  13. $sequence = $sequence ? $sequence : $seqPrefix.$this->table;
  14. $q = $this->query("SELECT {$sequence}.CURRVAL as t FROM DUAL");
  15. if($q) {
  16. $lastInsID = $q[0]['t'];
  17. }
  18. } catch(\Exception $e) {
  19. //print "Error!: " . $e->getMessage() . "</br>";
  20. //exit;
  21. }
  22. }
  23. return $lastInsID;
  24. }

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

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

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

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

  1. ----创建序列
  2. create sequence seq_user
  3. increment by 1
  4. start with 1
  5. nomaxvalue
  6. nominvalue
  7. nocache;
  8. ----创建触发器
  9. create or replace trigger "tig_user"
  10. before insert on tb_user
  11. for each row when(new.id is null)
  12. begin
  13. select seq_user.nextval into :new.id from dual;
  14. end;

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

  1. $data = array(
  2. 'phone'=>$phone,
  3. 'password'=>md5($password)
  4. );
  5. $r = M('user')->field(true)->add($data); //执行插入并返回上次插入Id
  6. if($r){
  7. //$r = M('user')->getLastInsID(); //获取上次插入Id
  8. echo '上次插入记录:'.$r;
  9. }else{
  10. $this->error('操作失败');
  11. }

ThinkPHP 3.2.3+ORACLE插入数据BUG修复及支持获取自增Id的上次记录的更多相关文章

  1. mybatis 插入数据 在没有commit时 获取主键id

      <insert id="insert" parameterType="Mail" useGeneratedKeys="true" ...

  2. Oracle 插入数据效率对比

    oracle插入数据有多种方式: 将从多个表中查出来的数据插入到临时表中 数据行数 5189597 1.传统方式:直接将数据插入到表中 insert into LLB_BASIC_USER_D_TEM ...

  3. 获取刚刚插入表格的这条信息的自增ID

    获取刚刚插入表格的这条信息的自增ID var conn=getConnection(); var msql="INSERT INTO " + table +" (&quo ...

  4. ThinkPHP增加数据库字段后插入数据为空的解决办法

    今天用ThinkPHP做了一个简单的商品发布系统,数据库本来只有四个字段id,name,url,image.id是主键,name是商品名称,url是商品链接,image是商品图片,做的差不多了,发现还 ...

  5. thinkPHP 无法create,无法插入数据,提示非法数据对象

    4.thinkPHP 无法create,提示非法数据对象解决方法:不要create+add,而用 data[]= '';+add$m_r_fa_account = D('R_fa_account'); ...

  6. JDBC向oracle插入数据

    public static void main(String[] args) throws SQLException { 2 3 4 String driver="oracle.jdbc.d ...

  7. oracle插入数据的时候报错:ORA-00928: 缺失 SELECT 关键字

    比如:插入数据的时候是这样的insert into a value('哈哈'); 报的是这样的错误:ORA-00928: 缺失 SELECT 关键字 其实就是value少了一个s,在oracle中,插 ...

  8. java批量向oracle插入数据

    由于项目需要,需要将一个6M的txt中的数据插入到oracle数据表中.txt中的数据是每行一个词.经过统计,词总数是505040.为了看起来方便,我将我的所有方法写在类入口中,数据库的信息我会用te ...

  9. oracle插入数据

    插入数据 insert into comm_error_code_def (ID, ERR_MESSAGE, ERR_CODE, ERR_DESC, NAME, MISC_DESC, STATUS, ...

随机推荐

  1. swift 加载 本地html 和 网络路径

    先上代码: xcode 9.4  ios 11.4 import UIKit import WebKit class RootViewController: UIViewController, WKN ...

  2. V-REP与C++初步通信测试

    打开vrep,在上方操作栏找到help选项打开,选择help topics.此时浏览器打开了vrep的操作手册user manual. 在user manual左侧目录中找到writing code ...

  3. 【398】COMP9021 - Polynomial

    构建 Polynomial 类,实现 +, -, , / and +=, -=, =, /= 参考:如何用python编程求解二元一次方程组.如x+y=3;x-y=1 参考:python对重载运算符的 ...

  4. 【396】python 递归练习题(COMP9021)

    Merging two strings into a third one Say that two strings s1 and s2 can be merged into a third strin ...

  5. 百度地图点聚合MarkerClusterer性能优化

    公司要求做个百度地图点聚合的性能优化,需一次性加载9万条数据. 记录下自己的优化过程.(只想看优化代码的可直接移步:步骤三) 一.引入百度地图 vue项目中,在index.html文件中用script ...

  6. 快速干掉Windows Defender

    1.快捷键Win+R,调出"运行"对话框,输入"gpedit.msc",打开组策略编辑器: 2.展开"计算机配置"→"管理模板&q ...

  7. 模仿input闪烁光标

    模仿闪烁的光标 <span class="cursor-blink"> </span> 样式代码: .cursor-blink { display: inl ...

  8. Server Memory Server Configuration Options 服务器内存服务配置选项

    Server Memory Server Configuration Options https://docs.microsoft.com/en-us/sql/database-engine/conf ...

  9. docker原理(转)

    可能是把Docker的概念讲的最清楚的一篇文章 [编者的话]本文只是对Docker的概念做了较为详细的介绍,并不涉及一些像Docker环境的安装以及Docker的一些常见操作和命令. Docker是世 ...

  10. wpf改变网格字体颜色

    1.创建个转换器 public class ColorConvertor : IValueConverter { public object Convert(object value, Type ta ...