# -*- coding: utf-8 -*- import sqlalchemy db_conf = {"user":"postgres", "passwd":"", "host" :"localhost", "port" :"5432", "db" :"saawo" } def main(): sql = sqlalchemy.text("SELECT * FROM city WHERE code = :code") engine = sqlalchemy.create_engine( "postgresql://{user}:{passwd}@{host}:{port}/{db}".format(**db_conf) ) sql_vals = {"code":"11002"} with engine.connect() as db_conn: results = db_conn.execute( sql.bindparams( **sql_vals ) ).mappings() for result in results: print( result ) if __name__ == '__main__': main()
↑こう書くと、↓こう表示されます
{'code': '11002', 'pref': '北海道', 'city': '札幌市', 'lng': 42.9853361, 'lat': 141.2479725}