Checking if a postgresql table exists under python (and probably Psycopg2)

前端 未结 8 2005
我在风中等你
我在风中等你 2020-12-08 18:24

How can I determine if a table exists using the Psycopg2 Python library? I want a true or false boolean.

8条回答
  •  失恋的感觉
    2020-12-08 19:03

    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    
    import psycopg2
    import sys
    
    
    con = None
    
    try:
    
        con = psycopg2.connect(database='testdb', user='janbodnar') 
        cur = con.cursor()
        cur.execute('SELECT 1 from mytable')          
        ver = cur.fetchone()
        print ver    //здесь наш код при успехе
    
    
    except psycopg2.DatabaseError, e:
        print 'Error %s' % e    
        sys.exit(1)
    
    
    finally:
    
        if con:
            con.close()
    

提交回复
热议问题