github.com/emcfarlane/larking@v0.0.0-20220605172417-1704b45ee6c3/starlib/starlarksql/testdata/sql.star (about) 1 # Tests of Starlark 'sql' extension. 2 load("sql.star", "sql") 3 4 def test_sql(t): 5 # db resource is shared, safe for concurrent access. 6 db = sql.open("sqlite:file::memory:?cache=shared") 7 t.true(db) # check database active. 8 9 db.ping() 10 11 # create 12 def create(): 13 db.exec("CREATE TABLE projects(mascot VARCHAR(10), release SMALLINT, category TEXT NOT NULL)") 14 15 # insert 16 def insert(): 17 projects = [ 18 ("tux", 1991), 19 ("duke", 1996), 20 ("gopher", 2009), 21 ("moby dock", 2013), 22 ] 23 24 stmt = "INSERT INTO projects(mascot, release, category) VALUES( ?, ?, ? )" 25 for project in projects: 26 db.exec(stmt, project[0], project[1], "open source") 27 28 # query 29 def query(after): 30 rows = db.query("SELECT rowid, * FROM projects WHERE release > ? ORDER BY release ASC", after) 31 return [row for row in rows] # copy values out of rows loop 32 33 create() 34 insert() 35 36 query_rows = query(2008) 37 t.eq(len(query_rows), 2) 38 print("projects:", query_rows) 39 40 sel_row = query_rows[0] 41 one_row = db.query_row("SELECT * FROM projects WHERE mascot = ?", sel_row.mascot) 42 print("row:", one_row) 43 44 t.eq(one_row.mascot, sel_row.mascot) 45 t.eq(one_row.release, sel_row.release) 46 t.eq(one_row.category, sel_row.category)