The easiest way to use SQL in any version of Python, including Pythonista and most likely Replit, is to use SQLite. SQLite3 has been included with Python since version 2.5.
Here is a very simple example of a widget in Pythonista 3 that reads from a SQLite database of quotes and displays a random daily quote.
#!python3
'''
This widget script shows a daily quote
Jerry Stratton astoundingscripts.com
'''
import appex, ui
import random, sqlite3, time
def main():
v = ui.View(frame=(0, 0, 320, 220))
v.background_color = 0.16, 0.55, 0.52
hPadding = 30
quoteBox = ui.Label(frame=(hPadding, 0, 320 - 44 - hPadding, 220), flex='wh')
quoteBox.font = ('Menlo', 14)
quoteBox.text_color = 'white'
quoteBox.number_of_lines = 0
v.add_subview(quoteBox)
appex.set_widget_view(v)
quoteBox.text = dailyFortune
def getDailyFortune():
quote = "Exercise builds strong code."
quote += "\n—John M. Nevison"
connection = sqlite3.connect('../data/quotes.sqlite')
cursor = connection.cursor()
quotes = list(cursor.execute('SELECT quote, speaker FROM quotes WHERE LENGTH(quote) < 220 ORDER BY added DESC'))
connection.close()
if quotes:
# seed that switches at midnight local time
dailySeed = time.time() - time.timezone
# make it switch at 4am
dailySeed -= 4*60*60
dailySeed //= (24 * 60 * 60)
random.seed(dailySeed)
quote = random.choice(quotes)
quote, speaker = quote
if speaker:
quote += '—' + speaker
return quote
if __name__ == '__main__':
dailyFortune = getDailyFortune()
main()
The important lines are:
import sqlite3
…
connection = sqlite3.connect(FILEPATH)
cursor = connection.cursor()
quotes = list(cursor.execute(SQLSTATEMENT))
connection.close()
Which makes a standard Python SQL connection to an existing database and reads all matching rows as a list. You can read more about SQLite and Python in the documentation.