python 插入日期數(shù)據(jù)到Oracle實(shí)例
啥也不說(shuō)了,還是看代碼吧!
[root@yyjk DATABASE]# cat DBI.py# -*- coding: utf-8 -*- import cx_Oracle import timeimport datetimeclass 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 #獲取當(dāng)前時(shí)間 time_now = int(time.time()) #轉(zhuǎn)換成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 插入時(shí)間到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)}
補(bǔ)充拓展:python 插入uuid 和 時(shí)間字段 到oracle數(shù)據(jù)庫(kù)中
看代碼:
import sysimport getConnectionimport datetimeimport uuidimport osos.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() # 獲取數(shù)據(jù)連接 , 這是我自己封裝的單獨(dú)獲取數(shù)據(jù)庫(kù)鏈接的py文件cursor = conn.cursor() # 獲取游標(biāo)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 econn.commit() # 這里一定要commit才行,要不然數(shù)據(jù)是不會(huì)插入的cursor.close()conn.close()
以上這篇python 插入日期數(shù)據(jù)到Oracle實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. IntelliJ IDEA安裝插件的方法步驟2. Docker 部署 Prometheus的安裝詳細(xì)教程3. idea重置默認(rèn)配置的方法步驟4. idea導(dǎo)入maven項(xiàng)目的方法5. IntelliJ IDEA設(shè)置自動(dòng)提示功能快捷鍵的方法6. idea設(shè)置代碼格式化的方法步驟7. IntelliJ IDEA調(diào)整字體大小的方法8. 通過(guò)Django Admin+HttpRunner1.5.6實(shí)現(xiàn)簡(jiǎn)易接口測(cè)試平臺(tái)9. idea打開(kāi)多個(gè)窗口的操作方法10. idea給項(xiàng)目打war包的方法步驟

網(wǎng)公網(wǎng)安備