D
D
des1roer2015-06-16 07:07:26
Python
des1roer, 2015-06-16 07:07:26

Python find out if there is a table?

question - how to correctly find out if there is a table in the database?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
des1roer, 2015-06-16
@des1roer

from here zetcode.com/db/postgresqlpythontutorial
kind of like an off tutorial

#!/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()

A
Andrey Mokhov, 2015-06-16
@mokhovcom

you can run a query and, based on its results, find out if there is a table:

SELECT *
FROM pg_tables
WHERE tablename = 'search_table_name'

A
Andrew, 2015-06-16
@Ashlst

Look here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question