2017-06-03 6 views
-1

私の要件は次のとおりです。テーブルのカウント(*)を見つけ、存在しない場合は、テーブルを作成します。次に、問題を示すサンプルコードを示します。どちらの場合もelse条件が発生します。どのように私はこれを達成するかわからない、どんな助けもありがとう。python oracle他の行がフェッチする場合

import cx_Oracle 
import os 
import datetime 

ts = datetime.datetime.now() 
con = cx_Oracle.connect('xxxx/[email protected]: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 

答えて

0

SELECT COUNT(*)常に行を返す - のいずれか0でないオブジェクトがテーブルまたはオブジェクトの数で存在しない場合。したがって、常に1行あり、行の数ではなく、数をテストする必要があります。だからif (rows[0][0] >= 1):

rows[0]最初の行を返すrows[0][0]最初の行の最初の列を返します。 SELECT COUNT(*)の場合、最初の列はカウントであるため、カウントが0より大きいかどうかテストします。

+0

コードで表示されている場合は申し訳ありませんが、 – Shanker

+0

変更されたコードは役に立ちますか? – phd

+0

申し訳ありませんが、しばらくお待ちください、私はあなたを更新します。このように動作するようです。私はいくつかのテストをしましょう。前もって感謝します。 – Shanker

関連する問題