首页 > 编程语言 > python 插入日期数据到Oracle实例
2020
09-24

python 插入日期数据到Oracle实例

啥也不说了,还是看代码吧!

 [root@yyjk DATABASE]# cat DBI.py
# -*- coding: utf-8 -*- 
import cx_Oracle 
import time
import datetime
class DBI (object):
 def LoadDB(self,a,b): 
  print self
  conn = cx_Oracle.connect('tlcbuser/tlcbuser@1.1.1.1/tlyy') 
  cursor = conn.cursor()
  #coding:UTF-8
  #获取当前时间
  time_now = int(time.time())
  #转换成localtime
  #time_local = time.localtime(time_now)
  #print time_local
  #dt = time.strftime("%Y-%m-%d %H:%M:%S",time_local)
  dt=datetime.datetime.now()
  print dt
  param={'stime':dt,'message':a,'phone':b}
  print param;
  cursor.execute('insert into tlcb_python_sms values(:stime,:message,:phone)',param)
  conn.commit()
  cursor.close
  
  
  python 插入时间到oracle
  
  
SQL> desc tlcb_python_sms
 Name            Null?  Type
 ----------------------------------------- -------- ----------------------------
 STIME              DATE
 MESSAGE            VARCHAR2(1000)
 PHONE              VARCHAR2(20)
 
 [root@yyjk flask]# python test.py 
lib.Mojo.Client
<lib.Mojo.Client.New object at 0x7f080e371050>
2017-11-10 11:37:40.173295
{'phone': '222', 'message': '111', 'stime': datetime.datetime(2017, 11, 10, 11, 37, 40, 173295)}

补充拓展:python 插入uuid 和 时间字段 到oracle数据库中

看代码:

import sys
import getConnection
import datetime
import uuid
import os
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
entId=str(uuid.uuid1()).replace("-","")
lastDate=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
conn = getConnection.getConnOracle114() # 获取数据连接 , 这是我自己封装的单独获取数据库链接的py文件
cursor = conn.cursor() # 获取游标
try:
  cursor.execute("insert into cp_entinfo(id,last_date) values ('"+entId+"',to_timestamp('"+lastDate+"','yyyy-mm-dd hh24:mi:ss:ff'))")
except Exception, e:
  print e
conn.commit() # 这里一定要commit才行,要不然数据是不会插入的
cursor.close()
conn.close()

以上这篇python 插入日期数据到Oracle实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持自学编程网。

编程技巧