Exemplo n.º 1
0
def calculatenumberofstructuresinfragilityview(databasename):

    valor = -1

    try:
        import logging
        from Database.Connection import Connection
        logging.debug(" calculating number of structures in database: " +
                      databasename)

        import psycopg2
        import numpy

        con2 = Connection()

        strcon = con2.getStringToConnect(databasename)
        logging.debug(" string de connection")

        print "strcon"
        print strcon
        logging.debug(strcon)

        conn = psycopg2.connect(strcon)

        cur = conn.cursor()
        try:
            cur.execute(
                "SELECT count(gid) from view_fragility_curve_structure")
            #cur.execute("""SELECT gid, (7* vulfactor - 13.1)/2.3  from popoliforpostgres""")

            sqllist = []

            rows = cur.fetchall()
            print "\nRows: \n"
            for row in rows:

                try:

                    print "Row 1 " + str(row[0])

                    if str(row[0]) == 'None':
                        valor = 0
                    else:
                        valor = row[0]
                except:
                    valor = -1

        except:
            print "I can't SELECT from view_fragility_curve_structure"

    except:
        import sys
        print "I am unable to connect to the database.", sys.exc_info()[0]
        raise

    return valor
Exemplo n.º 2
0
def updatevulnerability():

    try:
        conn = psycopg2.connect(Connection.getStringToConnect(self))
    except:
        print "I am unable to connect to the database."

    # creating a Cursor
    cur = conn.cursor()

    # creating a Cursorupdate

    table = "popoliforpostgres"

    filename = "C:/Data/Python/archivoupdate.txt"

    jueego = "0"

    for idstructura in range(1, 55):

        sql = "update popoliforpostgres set vulnerab = (select  sum(paramvalue*(vkimportance-vkprotection))*(1/6)*(1/(select sum(paramvalue) from valutazione where idstructure= " + str(
            idstructura
        ) + "))+ 0.5 from valutazione where idstructure= " + str(
            idstructura) + " ) where ogc_fid= " + str(idstructura) + ""

        sql = "update popoliforpostgres set vulnerab = "
        sql = sql + "(select   (sum(paramvalue*(vkimportance-vkprotection))/sum(paramvalue))/6 + 0.5 "
        sql = sql + "from valutazione where idstructure='" + str(
            idstructura) + "'"
        sql = sql + ")"
        sql = sql + "where idstructure=" + str(idstructura) + "'"

        try:

            print sql
            cur.execute(sql)

        except:
            jueego = "1"
            #print " sentencia  otrosql erronea "

    #rs.close()
    conn.commit()
    conn.close()

    print " juego es " + jueego
Exemplo n.º 3
0
def checkdatabase(databasename):

    valor = 1
    try:
        # logging.debug(" calculating number of structures in database: " + databasename)

        import psycopg2
        import numpy

        con2 = Connection()

        con = con2.getConnection()

        #strcon = con2.getStringToConnect(con.database)

        name_data0base = "test"

        strcon = con2.getStringToConnect(databasename)

        logging.debug(" string de connection")

        print "strcon"
        print strcon
        logging.debug(strcon)

        try:

            conn = psycopg2.connect(strcon)
            '''
            cur = conn.cursor()

            cur.execute("SELECT count(gid) from p+")
            # cur.execute("""SELECT gid, (7* vulfactor - 13.1)/2.3  from popoliforpostgres""")

            sqllist = []

            rows = cur.fetchall()
            print "\nRows: \n"
            for row in rows:

                try:

                    print "Row 1 " + str(row[0])

                    if str(row[0]) == 'None':
                        valor = 0
                    else:
                        valor = row[0]
                except:
                    valor = -1
                '''

        except:
            print "I can't SELECT from popoli for postgres"
            valor = -1

    except:
        import sys
        print "I am unable to connect to the database.", sys.exc_info()[0]
        raise
        valor = -1

    return valor