In this code example, all the data from a database table named dml7y9613kpms_subscriptions
is retrieved using a SQL Select statement. The data is appended to a list as a JSON string and output.
The SQL SELECT command is the elementary component for data retrieval. The SELECT command is used to select and display the contents of one or more tables. Thus, to select data with the SELECT command, you need the column name to be selected and the corresponding table.
import json
import pyodbc
import mariadb
def main():
data = getData(connection)
print(json.dumps(data))
def getData(connection):
data = []
cursor = connection.cursor()
cursor.execute("SELECT * FROM dml7y9613kpms_subscriptions")
records = cursor.fetchall()
for row in records:
id = row[0]
userId = row[1]
beforeSubscriptionPlanId = row[2]
afterSubscriptionPlanId = row[3]
startDate = row[4]
expirationDate = row[5]
status = row[6]
apiKeys = {"id": id, "user-id": userId, "old-subscription": beforeSubscriptionPlanId, "new-subscription": afterSubscriptionPlanId}
data.append(apiKeys)
return data