Skip to content

Instantly share code, notes, and snippets.

@avermeulen
Created September 4, 2018 14:39
Show Gist options
  • Select an option

  • Save avermeulen/fde44c4b96bdd7e9a045eae520e3833e to your computer and use it in GitHub Desktop.

Select an option

Save avermeulen/fde44c4b96bdd7e9a045eae520e3833e to your computer and use it in GitHub Desktop.
create table users(id serial primary key,
firstName text not null,
email text not null);
insert into users(firstName, email) values ('Andre', 'andre@gmail.com');
insert into users(firstName, email) values ('Andrew', 'andrew@gmail.com');
insert into users(firstName, email) values ('Bob', 'bob@gmail.com');
import psycopg2
from flask_api import FlaskAPI
try:
app = FlaskAPI(__name__)
connect_str = "dbname='leaderex-test' host='localhost' "
# # use our connection values to establish a connection
conn = psycopg2.connect(connect_str)
# # create a psycopg2 cursor that can execute queries
cursor = conn.cursor()
# # create a new table with a single column called "name"
# cursor.execute("""CREATE TABLE tutorials (name char(40));""")
# # run a SELECT statement - no data in there, but we can try it
cursor.execute("""SELECT * from users """)
rows = cursor.fetchall()
# print(rows)
for row in rows:
print row
@app.route('/example/<username>')
def example(username):
print username
cursor.execute("SELECT * from users where firstname = '" + username + "'")
rows = cursor.fetchall()
return {'data' : rows}
if __name__ == "__main__":
app.run(debug=True)
except Exception as e:
print("Uh oh, can't connect. Invalid dbname, user or password?")
print(e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment