SELECT query example

    db1cursor.execute("""SELECT * FROM my_table 
                      WHERE some_column = ?
                      """,(my_value1,))
    rows = db1cursor.fetchall()
    for row in rows:
        #print(row)
        print("my_column0: ", row[0])       #Results are returned in the order used within the SELECT statement, or within the table for *
        print("my_column1: ", row[1])
        print("my_column2: ", row[2])

Single row result

    db1cursor.execute("""SELECT * FROM my_table 
                      WHERE some_column = ?
                      """,(my_value1,))
    row = db1cursor.fetchone()
    if row != None:
        print("my_column0: ", row[0])
        print("my_column1: ", row[1])
        print("my_column2: ", row[2])

Returning results as a dictionary

    db1cursor.execute("""SELECT my_column_b, my_column_c, my_column_d FROM my_table 
                      WHERE some_column = ?
                      """,(my_value1,))
    row = db1cursor.fetchone()

    result = {"my_column_b": row[0], "my_column_c": row[1], "my_column_d": row[2]}
    #print(result)
    return(result)
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *