example of SQLite using a local database file through SQLalchemy
Archive - Originally posted on "The Horse's Mouth" - 2015-10-14 20:27:59 - Graham EllisHere's an example of SQLite using a local database file through SQLalchemy. The SQL statements are hidden from the user - that's the "Object to Relation Mapping" - although you can ask what's going on by runing my program with a -v option. ... example program source [here], seeding data download from [here].
We start by seeing if the database file exists:
database = "stations.db"
populated = os.path.exists(database)
So that we only have to populate the tables if they don't exist (we have a seeding data file).
Here's output from a typical run:
WomanWithCat:flask grahamellis$ python orm_sqlite
Station (code='MKM', postcode='SN12 8BN', footfall='23930', fullname='Melksham')
(u'TEA', 8, u'Tees-Side Airport')
(u'SPP', 12, u'Shippea Hill')
(u'RDS', 26, u'Reddish South')
(u'BYL', 40, u'Barry Links')
(u'COE', 42, u'Coombe')
(u'BRC', 64, u'Breich')
(u'BUC', 80, u'Buckenham')
(u'PIL', 88, u'Pilning')
(u'GOF', 90, u'Golf Street')
(u'DTN', 110, u'Denton')
(u'KTL', 120, u'Kirton Lindsey')
(u'ABC', 138, u'Altnabreac')
(u'KIL', 144, u'Kildonan')
(u'ELO', 166, u'Elton & Orston')
(u'ACK', 176, u'Acklington')
(u'AAT', 228, u'Achanalt')
(u'CPN', 232, u'Chapelton')
(u'SUG', 240, u'Sugar Loaf')
(u'HEL', 276, u'Hensall')
(u'HVN', 278, u'Havenhouse')
(u'CLI', 298, u'Clifton')
(u'SNT', 314, u'Stanlow & Thornton')
(u'RWC', 314, u'Rawcliffe')
(u'HBB', 334, u'Hubberts Bridge')
(u'TPC', 340, u'Thorpe Culvert')
(u'NCE', 348, u'New Clee')
(u'SCT', 376, u'Scotscalder')
(u'LAK', 378, u'Lakenheath')
(u'SPN', 388, u'Spooner Row')
(u'FOC', 498, u'Falls Of Cruachan')
WomanWithCat:flask grahamellis$