pythonsqlitesqlite3-python

Extract information through SQLite and Python


Table SQLite

enter image description here

I am working with a DataBase in Sqlite and Python with the table that is attached. I am interested in get a query that give me a list with an unique item in IDFrom, for those unique items, the VoltMag, and the kV. I am no interested in other information in the table. I have tried with the following query, but I have not get the expected result.

I appreciate any help.

Regards,

IDFrom Type kV VoltMag IDTo LFMW
LVBus Load 0.47999999 97.353271484375 Sub3 Swgr -0.476513087749481
LVBus 0 0 0 Bus5 0.123355090618134
Main Bus Swng 34.5 100 Sub2A 3.65306186676025
Main Bus 0 0 0 Sub2B -1.56734025478363
Main Bus XF3W 0 0 Sub 3 0
Sub2A Load 13.8000001907349 99.2764282226562 Bus7 2.71204876899719
Sub2A 0 0 0 Main Bus 1.79826784133911
Sub2B Gen 13.8000001907349 101 Bus6 1.79826784133911
Sub2B 0 0 0 Sub 3 1.6838458776474
Sub2B XF3W 0 0 Main Bus 0
Sub22 Load 3.45000004768372 96.6335220336914 Bus7 -2.69958877563477
Sub23 Load 3.45000004768372 100.332984924316 Bus23A 0.524362742900848
Sub23 0 0 0 Bus6 -1.79444992542267
Sub23 Load 3.45000004768372 100.332984924316 VFD2 1.27008724212647
UPS-1 Swng 0.479999989271164 100 Bus4 0.0918000042438507
VFD2 Swng 3.45000004768372 100 Bus8 1.27008724212647
def Vmag(conn):
    cur = conn.cursor()
    cur.execute(r"SELECT VoltMag FROM LFR WHERE TYPE !=0 AND kV !=0;")
    rows = cur.fetchall()
    return rows

def NomkV(conn):
    cur = conn.cursor()
    cur.execute(r"SELECT kV FROM LFR WHERE TYPE !=0 AND kV !=0;")
    rows = cur.fetchall()
    return rows

def BusName(conn):
    cur = conn.cursor()
    cur.execute(r"SELECT IDFrom FROM LFR WHERE TYPE !=0 AND kV !=0;")
    rows = cur.fetchall()
    return rows

I would like to have three list as result:

IDFrom
LVBus
Main Bus
Sub2A
Sub2B
Sub22
Sub23
UPS-1
VFD2
kV
0.479999989
34.5
13.80000019
13.80000019
3.450000048
3.450000048
0.479999989
3.450000048
VoltMag
97.35327148
100
99.27642822
101
96.63352203
100.3329849
100
100

Solution

  • I think you want SELECT DISTINCT and use it in a subquery to display only the column you want, or just use it alone in one function (AsOneQuery below) to fetch all the interesting unique column values:

    import sqlite3
    
    def create_db():
        conn = sqlite3.connect(':memory:')
        cur = conn.cursor()
        cur.execute('CREATE TABLE LFR (IDFrom,Type,kV,VoltMag,IDTo,LFMWE);')
        cur.execute("INSERT INTO LFR VALUES('LVBus','Load',0.47999998999999998749,97.353271484374999996,'Sub3 Swgr',-0.47651308774948097912);")
        cur.execute("INSERT INTO LFR VALUES('LVBus',0,0,0,'Bus5',0.123355090618134);")
        cur.execute("INSERT INTO LFR VALUES('Main Bus','Swng',34.5,100,'Sub2A',3.65306186676025);")
        cur.execute("INSERT INTO LFR VALUES('Main Bus',0,0,0,'Sub2B',-1.56734025478363);")
        cur.execute("INSERT INTO LFR VALUES('Main Bus','XF3W',0,0,'Sub 3',0);")
        cur.execute("INSERT INTO LFR VALUES('Sub2A','Load',13.8000001907349,99.2764282226562,'Bus7',2.71204876899719);")
        cur.execute("INSERT INTO LFR VALUES('Sub2A',0,0,0,'Main Bus',1.79826784133911);")
        cur.execute("INSERT INTO LFR VALUES('Sub2B','Gen',13.8000001907349,101,'Bus6',1.79826784133911);")
        cur.execute("INSERT INTO LFR VALUES('Sub2B',0,0,0,'Sub 3',1.6838458776474);")
        cur.execute("INSERT INTO LFR VALUES('Sub2B','XF3W',0,0,'Main Bus',0);")
        cur.execute("INSERT INTO LFR VALUES('Sub22','Load',3.45000004768372,96.6335220336914,'Bus7',-2.69958877563477);")
        cur.execute("INSERT INTO LFR VALUES('Sub23','Load',3.45000004768372,100.332984924316,'Bus23A',0.524362742900848);")
        cur.execute("INSERT INTO LFR VALUES('Sub23',0,0,0,'Bus6',-1.79444992542267);")
        cur.execute("INSERT INTO LFR VALUES('Sub23','Load',3.45000004768372,100.332984924316,'VFD2',1.27008724212647);")
        cur.execute("INSERT INTO LFR VALUES('UPS-1','Swng',0.479999989271164,100,'Bus4',0.0918000042438507);")
        cur.execute("INSERT INTO LFR VALUES('VFD2','Swng',3.45000004768372,100,'Bus8',1.27008724212647);")
        conn.commit()
        return conn
    
    def Vmag(conn):
        cur = conn.cursor()
        cur.execute(r"SELECT VoltMag FROM (SELECT DISTINCT IDFrom,VoltMag from LFR WHERE TYPE !=0 AND kV !=0);")
        rows = cur.fetchall()
        return rows
    
    def NomkV(conn):
        cur = conn.cursor()
        cur.execute(r"SELECT kV FROM (SELECT DISTINCT IDFrom,kV FROM LFR WHERE TYPE !=0 AND kV !=0);")
        rows = cur.fetchall()
        return rows
    
    def BusName(conn):
        cur = conn.cursor()
        cur.execute(r"SELECT DISTINCT IDFrom FROM LFR WHERE TYPE !=0 AND kV !=0;")
        rows = cur.fetchall()
        return rows
    
    def AsOneQuery(conn):
        cur = conn.cursor()
        cur.execute(r"SELECT DISTINCT IDFrom,kV,VoltMag FROM LFR WHERE TYPE !=0 AND kV !=0;")
        rows = cur.fetchall()
        return rows
    
    conn = create_db()
    for row, in BusName(conn):
        print(row)
    print()
    for row, in NomkV(conn):
        print(row)
    print()
    for row, in Vmag(conn):
        print(row)
    print()
    for IDFrom, kv, VoltMag in AsOneQuery(conn):
        print(f'{IDFrom:9}{kv:18}{VoltMag:18}')
    

    Output:

    LVBus
    Main Bus
    Sub2A
    Sub2B
    Sub22
    Sub23
    UPS-1
    VFD2
    
    0.47999999
    34.5
    13.8000001907349
    13.8000001907349
    3.45000004768372
    3.45000004768372
    0.479999989271164
    3.45000004768372
    
    97.353271484375
    100
    99.2764282226562
    101
    96.6335220336914
    100.332984924316
    100
    100
    
    LVBus            0.47999999   97.353271484375
    Main Bus               34.5               100
    Sub2A      13.8000001907349  99.2764282226562
    Sub2B      13.8000001907349               101
    Sub22      3.45000004768372  96.6335220336914
    Sub23      3.45000004768372  100.332984924316
    UPS-1     0.479999989271164               100
    VFD2       3.45000004768372               100