Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import psycopg2
- from sqlalchemy import create_engine
- from sqlalchemy.ext.declarative import declarative_base
- from sqlalchemy import Column, Integer, String
- from sqlalchemy.orm import sessionmaker
- from passlib.hash import pbkdf2_sha256
- engine = create_engine('postgresql://youruser:yourpass@192.168.1.103:5432/mydb')
- Base = declarative_base()
- Session = sessionmaker(bind=engine)
- session = Session()
- class MyStuff(Base):
- __tablename__ = 'mytable'
- id = Column(Integer, primary_key=True)
- stuff = Column(String)
- def __repr__(self):
- return 'MyStuff: %s, %s' % (self.id, self.stuff)
- def add_w_orm(stuff_to_add):
- newstuff = MyStuff()
- newstuff.stuff = stuff_to_add
- session.add(newstuff)
- session.commit()
- print 'added'
- def query_w_orm():
- contents = session.query(MyStuff).all()
- print [i for i in contents]
- def func():
- conn = psycopg2.connect(dbname='mydb', user='youruser', host='192.168.1.103', password='yourpass')
- cursor = conn.cursor()
- cursor.execute('SELECT * FROM mytable')
- print [i for i in cursor]
- # print [i for i in cursor]
- # try:
- # cursor.execute('INSERT INTO mytable(stuff) VALUES(%s)', ('masodik sor',))
- #
- # cursor.execute('SELECT * FROM mytable')
- #
- # conn.commit()
- #
- # print [i for i in cursor]
- #
- # except Exception as e:
- # print str(e)
- cursor.close()
- conn.close()
- if __name__ == '__main__':
- func()
- query_w_orm()
- add_w_orm('inserted with ORM')
- query_w_orm()
Add Comment
Please, Sign In to add comment