python操作oracle数据库

 

1、首先要下载安装cx_Oracle

      注意:下载的cx_Oracle版本要与自己的python环境版本一致,否则可能连接数据库时报错。

2、数据库表结构

      

3、python代码

      

 
  1. # -*- mode: python; coding: utf-8 -*-

  2. #

  3. # python operate oracle, contain insert、delete、update、select.

  4. #

  5. # @author liyulin

    扫描二维码关注公众号,回复: 11816605 查看本文章
  6. # @date 2014-11-07

  7.  
  8. import cx_Oracle

  9.  
  10. class PythonOralceUtil:

  11. def __enter__(self):

  12. self.conn = cx_Oracle.connect('testuser/testpwd@localhost/CHROMEBOOK')

  13. self.cursor = self.conn.cursor()

  14. return self

  15.  
  16. def __exit__(self, type, value, traceback):

  17. self.cursor.close()

  18. self.conn.close()

  19.  
  20. ############################################

  21. # 查询reg_codes中的所有数据

  22. ############################################

  23. def queryAll(self):

  24. self.cursor.execute('select * from reg_codes')

  25. results = self.cursor.fetchall()

  26. for result in results:

  27. print result

  28.  
  29. ############################################

  30. # 根据序号查询reg_codes中的一条数据

  31. ############################################

  32. def queryBySeq(self, seq):

  33. self.cursor.execute('select * from reg_codes where seq=:1', seq)

  34. result = self.cursor.fetchone()

  35. if (result is not None):

  36. for index in range(0,6):

  37. print result[index],

  38.  
  39. ############################################

  40. # 向reg_codes中插入N条数据

  41. ############################################

  42. def insertManay(self, insertValue):

  43. self.conn.begin()

  44. try:

  45. 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)

  46. except AssertionError:

  47. self.conn.rollback()

  48. raise Warning, "invalid insertValue (%s)" % insertValue

  49. self.conn.commit()

  50.  
  51. ############################################

  52. # 更新reg_codes中一条数据

  53. ############################################

  54. def updateOne(self, sqe, input_file):

  55. updateValue = [input_file, sqe]

  56. self.cursor.execute('update reg_codes set input_file=:1 where seq=:2', updateValue)

  57.  
  58. ############################################

  59. # 更新reg_codes中N条数据

  60. ############################################

  61. def updateManay(self, updateValues):

  62. self.conn.begin()

  63. try:

  64. self.cursor.executemany('update reg_codes set input_file=:1 where seq=:2', updateValues)

  65. except AssertionError:

  66. self.conn.rollback()

  67. raise Warning, "invalid insertValue (%s)" % updateValues

  68. self.conn.commit()

  69.  
  70. ############################################

  71. # 删除reg_codes中一条数据

  72. ############################################

  73. def delete(self, sqe):

  74. self.cursor.execute('delete from reg_codes where seq=:1', sqe)

  75.  
  76. ############################################

  77. # 删除reg_codes中N条数据

  78. ############################################

  79. def deleteManay(self, seqs):

  80. self.conn.begin()

  81. try:

  82. self.cursor.executemany('delete from reg_codes where seq=:1', seqs)

  83. except AssertionError:

  84. self.conn.rollback()

  85. raise Warning, "invalid seqs (%s)" % seqs

  86. self.conn.commit()

  87.  
  88. ############################################

  89. # 执行代码

  90. ############################################

  91. with PythonOralceUtil() as pythonOralceUtil:

  92. # insertValue = [['jerry', 'unique_code2333', 'group_code2333', 'debug233', '1111111111122'],

  93. # ['jerry', 'unique_code244', 'group_code244', 'debug244', '22222222233'],

  94. # ['jerry', 'unique_code255', 'group_code255', 'debug255', '33333333344'],

  95. # ['jerry', 'unique_code266', 'group_code266', 'debug266', '44444444455'],

  96. # ['jerry', 'unique_code277', 'group_code277', 'debug277', '55555555566']]

  97. # pythonOralceUtil.insertManay(insertValue)

  98. # pythonOralceUtil.updateOne('27', 'debug_updated')

  99. # pythonOralceUtil.delete([27])

  100. # pythonOralceUtil.deleteManay([[31],[44],[45]])

  101. updateValues = [['debug_updated', '46'],

  102. ['debug_updated', '47'],

  103. ['debug_updated', '48'],

  104. ['debug_updated', '34']]

  105. pythonOralceUtil.updateManay(updateValues)

  106. pythonOralceUtil.queryAll()

  107. pythonOralceUtil.queryBySeq([27])

猜你喜欢

转载自blog.csdn.net/xixi20200/article/details/108772504