1, first to download the installation cx_oracle
Note: The downloaded version of Cx_oracle is consistent with its own version of the Python environment, or the database may be connected to an error.
2. Database table structure
3. Python Code
#-*-Mode:python; Coding:utf-8-*-## python operate Oracle, contain INSERT, delete, update, select.## @author Liyulin # @date 2014-11-07 Imp ORT cx_oracleclass pythonoralceutil:def __enter__ (self): Self.conn = Cx_oracle.connect (' TESTUSER/[EMAIL PR Otected]/chromebook ') Self.cursor = Self.conn.cursor () return self def __exit__ (self, type, value, tr Aceback): Self.cursor.close () self.conn.close () ############################################ # query all data in Reg_codes ############################################ def queryall (self): Self.cursor.execute (' sel ECT * from reg_codes ') results = Self.cursor.fetchall () for result in Results:print result ############################################ # Query A data in reg_codes based on an ordinal ############################################ def querybyseq (self, seq): Self.cursor.execute (' select * from Reg_codes where seq=:1 ', seq) result = Sel F.curSor.fetchone () if (result is not None): For index in range (0,6): Print Result[in Dex], ############################################ # inserts n data into Reg_codes ###################### ###################### def insertmanay (self, Insertvalue): Self.conn.begin () try:self.cursor. Executemany (' INSERT into reg_codes (DEVICE,UNIQUE_CODE,GROUP_CODE,INPUT_FILE,SN,INPUT_TS) VALUES (: 1,:2,:3,:4,:5, sysdate) ', insertvalue) except AssertionError:self.conn.rollback () raise Warning, "Invalid I Nsertvalue (%s) "% Insertvalue Self.conn.commit () ############################################ # Update re G_codes a data ############################################ def updateone (self, SQE, input_file): Updatevalue = [Input_file, SQE] self.cursor.execute (' Update reg_codes set input_file=:1 where Seq=:2 ', Updatevalue) ##### #################################### # # # Update reg_codes n data ############################################ def updatemanay (self, updatevalues): Self.conn.begin () try:self.cursor.executemany (' Update reg_codes set input_file=:1 where Seq=:2 ', update Values) except AssertionError:self.conn.rollback () raise Warning, "Invalid Insertvalue (%s)" % updatevalues Self.conn.commit () ############################################ # Delete a piece of data in Reg_codes # # # ######################################### def delete (self, SQE): Self.cursor.execute (' Delete from Reg_codes wher E seq=:1 ', SQE) ############################################ # Delete the n data in Reg_codes ############################# ############### def deletemanay (self, Seqs): Self.conn.begin () Try:self.cursor.executemany (' d Elete from Reg_codes where Seq=:1 ', seqs) except AssertionError:self.conn.rollback () raise W arning, "Invalid Seqs (%s) "% Seqs self.conn.commit () ############################################# Execute code ######################## #################### with Pythonoralceutil () as pythonoralceutil:# insertvalue = [[' Jerry ', ' unique_code2333 ', ' group_code2333 ', ' debug233 ', ' 1111111111122 '],# [' Jerry ', ' unique_code244 ', ' group_code244 ', ' debug244 ', ' 22222222233 '],# [' Jerry ', ' unique_code255 ', ' group_code255 ', ' debug255 ', ' 33333333344 '],# [' Jerry ', ' unique_code266 ', ' group_code266 ', ' debug266 ', ' 44444444455 '],# [' Jerry ', ' unique_code277 ', ' Group_ code277 ', ' debug277 ', ' 55555555566 ']]# Pythonoralceutil.insertmanay (insertvalue) # pythonoralceutil.updateone (' 27 ', ' debug_updated ') # pythonoralceutil.delete ([+]) # Pythonoralceutil.deletemanay ([[31],[44],[45]]) UpdateValues = [' debug_updated ', ' ['] ', ' [' debug_updated ', ' + '], [' debug_updated ', ' 48 '], [' Debug_updaTed ', ' Pythonoralceutil.updatemanay ']] (updatevalues) Pythonoralceutil.queryall () Pythonoralceutil.querybyseq ([ 27])
Python Operations Oracle Database