我正在尝试将当前日期时间插入到Codeigniter中的oracle数据库中,但出现如下错误:
ORA-01858: a non-numeric character was found where a numeric was expected
INSERT INTO "USER_LOGS" ("USER_ID", "IP_ADDRESS", "LOGIN_DATE", "LOG_TYPE", "USERNAME") VALUES ('1', '::1', 'TO_DATE(''2018-07-17 03:33:03'',''yyyy/mm/dd hh24:mi:ss'')', 'LOG OUT', 'admin')
我的代码:
date_default_timezone_set("Asia/Dhaka");
$timestamp = date('Y-m-d h:i:s');
$timestamp = "TO_DATE('$timestamp','yyyy/mm/dd hh24:mi:ss')";
$user_log_data = array(
'USER_ID' => $ses_data['USER_ID'],
'IP_ADDRESS' => $this->input->ip_address(),
'LOGIN_DATE' => $timestamp,
'LOG_TYPE' => 'LOG OUT',
'USERNAME' => $ses_data['USERNAME']
);
$this->utilities->insertData($user_log_data, 'USER_LOGS');
我的插入语句:
function insertData($post, $tableName)
{
$this->db->trans_start();
$this->db->insert($tableName, $post);
$this->db->trans_complete();
if ($this->db->trans_status() == TRUE) {
return TRUE;
} else {
return FALSE;
}
}
对于您的种类信息,字段(LOGIN_DATE)的数据类型为TIMESTAMP
谢谢
答案 0 :(得分:1)
您可以这样构建INSERT语句:
$sql =
"INSERT INTO USER_LOGS (USER_ID, IP_ADDRESS, LOGIN_DATE, LOG_TYPE, USERNAME)
VALUES (?, ?, TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'), ?, ?)";
$timestamp = date('Y-m-d h:i:s');
$user_log_data = array(
$ses_data['USER_ID'],
$this->input->ip_address(),
$timestamp,
'LOG OUT',
$ses_data['USERNAME']
);
$this->db->query($sql, user_log_data);
或者,如果您要求“当前日期时间”,则只需:
$sql =
"INSERT INTO USER_LOGS (USER_ID, IP_ADDRESS, LOGIN_DATE, LOG_TYPE, USERNAME)
VALUES (?, ?, SYSDATE, ?, ?)";
$user_log_data = array(
$ses_data['USER_ID'],
$this->input->ip_address(),
'LOG OUT',
$ses_data['USERNAME']
);
$this->db->query($sql, user_log_data);
但是,我不知道db->query()
是否也适用于DML语句。
答案 1 :(得分:0)
请尝试从$ timestamp变量中删除单引号。
date_default_timezone_set("Asia/Dhaka");
$timestamp = date('Y-m-d h:i:s');
$timestamp = "TO_DATE($timestamp,'yyyy/mm/dd hh24:mi:ss')";
$user_log_data = array(
'USER_ID' => $ses_data['USER_ID'],
'IP_ADDRESS' => $this->input->ip_address(),
'LOGIN_DATE' => $timestamp,
'LOG_TYPE' => 'LOG OUT',
'USERNAME' => $ses_data['USERNAME']
);
$this->utilities->insertData($user_log_data, 'USER_LOGS');
答案 2 :(得分:0)
最后,我通过使用CI日期帮助程序并在 system / core / Controller.php 中制作了一个自定义Oracle日期格式方法来做到这一点,或者您可以创建自己的继承CI_Controller的控制器,例如
//system/core/Controller.php
class CI_Controller {
function __construct() {
parent::__construct();
}
public function oracle_date($timestamp='')
{
$this->load->helper('date');
if($timestamp=='date'){
$datestring = '%d-%M-%Y';
}
else
{
$datestring = '%d-%M-%Y %h.%i.%s %a';
}
$time = time();
$timestamp = strtoupper(mdate($datestring, $time));
return $timestamp;
}
}
//OR
//application/core
class MY_Controller extends CI_Controller {
function __construct() {
parent::__construct();
}
public function oracle_date($timestamp='')
{
$this->load->helper('date');
if($timestamp=='date'){
$datestring = '%d-%M-%Y';
}
else
{
$datestring = '%d-%M-%Y %h.%i.%s %a';
}
$time = time();
$timestamp = strtoupper(mdate($datestring, $time));
return $timestamp;
}
}
//Then use as follows:
//application/controllers
class User extends MY_Controller {
public function __construct(){
parent::__construct();
}
$timestamp = $this->oracle_date('timestamp'); //if parameter is 'date' it will return only date like DD-MM-YY and 'timestamp' it will return full timestamp
}
//OR
//application/controllers
class User extends CI_Controller {
public function __construct(){
parent::__construct();
}
$timestamp = $this->oracle_date('timestamp'); //if parameter is 'date' it will return only date like DD-MM-YY and 'timestamp' it will return full timestamp
}
如果创建自定义控制器,还可以在config.php文件中更改前缀MY_,例如$ config ['subclass_prefix'] ='MY_';。到$ config ['subclass_prefix'] ='Your _';