python oracle如果其他排出以获取行



我的要求正在遵循。找到一个表(*(的计数(*(,如果不存在,请创建一个。遵循是显示问题的示例代码。在这两种情况下,其他条件都会到来。不确定我是如何实现这一目标的,任何帮助将不胜感激。

import cx_Oracle
import os
import datetime
ts = datetime.datetime.now()
con = cx_Oracle.connect('xxxx/xxxx@xxxxx:1521/xxxxx')
print con.version
cur = con.cursor()
cur.execute('select count(*) from AAA.AAA_TEST')
rows = cur.fetchall();
print rows
print len(rows)
if (rows ==1):
  print 'there is a row'
else:
  print 'there is no row'

#result 1 where the row exists
11.2.0.4.0
[(1,)]
1
there is no row
Process finished with exit code 0
#result 2 where the row do not exists
11.2.0.4.0
[(0,)]
1
there is no row
Process finished with exit code 0

SELECT COUNT(*)总是返回一行 - 如果表中没有对象或对象的数量,则使用0返回。因此,总有1行,您应该测试计数,而不是行数。因此,请使用if (rows[0][0] >= 1):

rows[0]返回第一行,rows[0][0]给出了第一行的第一列;对于您的SELECT COUNT(*),第一列是计数,因此您测试计数是否为0,如果是> =1。

最新更新