def close_connection(db): try: psycopg2.close(dbname=db, user=settings.DATABASE_USER, password=settings.DATABASES['default']['PASSWORD'], host=settings.DATABASES["default"]["HOST"], port=settings.DATABASES["default"]["PORT"]) except psycopg2.OperationalError: return False return True
def load_user(email): email = str(email) sql = "SELECT accesslevel FROM user_inf WHERE email = '%s'" % (email) db = connect_db() cur = db.cursor() cur.execute(sql) row = cur.fetchone() if row is None: db.close() return None accesslevel = row[0] db.close() return User(email, accesslevel)
def main(): db_connect = { 'host': 'dbapp.hanyang.ac.kr', 'post': '54321', 'user': '******', 'dbname': 'db_dblab', 'password': '******' } csv = get_csv('students.csv') print(csv) connect_string = "host={host} user={user} dbname={dbname} password={password} port={port}".format( **db_connect) print(db_connect) conn = pg.connect(connect_string) # 커서 작성 cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor) line_count = 0 for row in csv: if (line_count > 3): sql = f"INSERT INTO students VALUES ('{row[0]}', '{row[1]}', '{row[2]}', '{row[3]}','{row[4]}', '{row[5]}', '{row[6]}')" print(sql) cur.execute(sql) line_count += 1 conn.commit() sql = "SELECT * FROM students" # 수동커밋 필요 rows = cur.fetchall() print(type(rows)) print(rows) for row in rows: print(type(row['sid'])) print(row['sid']) print(row['sname']) pg.close()
import psycopg2 as dbc from sys import exit cfg = Config(op.dirname(__file__)) cfg.from_envvar('CONFIG') def connect_db(): """Connects to the database""" return dbc.connect(dbname=cfg['DBNAME'], user=cfg['USER'], password=cfg['PASSWORD'], host=cfg['HOST']) if __name__ == '__main__': name = input("""Enter admin's nickname: """) passwd = getpass(prompt='Enter password: '******'Repeat password: '******'t match.""") exit() passwd_hash = bcrypt.hashpw(passwd.encode(), bcrypt.gensalt()).decode() e_mail = input('Enter email: ') dbc = connect_db() cur = dbc.cursor() cur.execute( """INSERT INTO "user" (nickname, password, e_mail, rights) \ VALUES (%s, %s, %s, 'admin_')""", (name, passwd_hash, e_mail)) dbc.commit() dbc.close()
rows = cur.fetchall() cur.close() return rows ''' Used to create structures such as schemas or tables in the database. @param query: Query to create structures. ''' def create(self, query): cur = self.conn.cursor() cur.execute(query) self.conn.commit() cur.close() ''' Close the database when done using. ''' def close(self): self.conn.close() if __name__ == "__main__": db = Database('test') query = """select ca.fips, ca.st, dat.reducedtill_harv_ac, dat.reducedtill_prod, dat.reducedtill_yield from cs_data dat, """ + db.constantsSchema + """.county_attributes ca where dat.fips = ca.fips order by ca.fips asc""" data = db.output(query, db.productionSchema) db.close() print data
def close_db(e=None): db = g.pop('db', None) if db is not None: db.close()
Used to create structures such as schemas or tables in the database. @param query: Query to create structures. ''' def create(self, query): cur = self.conn.cursor() cur.execute(query) self.conn.commit() cur.close() ''' Close the database when done using. ''' def close(self): self.conn.close() if __name__ == "__main__": db = Database('test') query = """select ca.fips, ca.st, dat.reducedtill_harv_ac, dat.reducedtill_prod, dat.reducedtill_yield from cs_data dat, """ + db.constantsSchema + """.county_attributes ca where dat.fips = ca.fips order by ca.fips asc""" data = db.output(query, db.productionSchema) db.close() print data
def teardown_request(exception): db = getattr(g, 'db', None) if db is not None: db.close()
def dc(): return psycopg2.close()