Beispiel #1
0
 def insertMany(self, sql_string, values):
     try:
         with self._get_cursor() as cur:
             cur.executemany(sql_string, values)
     except Exception, ex:
         logger.debug("Error while executing %s" % sql_string)
         logger.debug(traceback.print_exc())
         raise ex
Beispiel #2
0
 def submit(self, sql):
     try:
         with self._get_cursor() as cur:
             cur.execute(sql)
     except Exception, ex:
         logger.debug("Error while executing %s" % sql)
         logger.debug(traceback.print_exc())
         raise ex
Beispiel #3
0
 def insertMany(self, sql_string, values):
     try:
         with PGDbAccess(self.conn_string, self.echo) as pga:
             pga.cur.executemany(sql_string, values)
             pga.conn.commit()
     except Exception, ex:
         logger.debug("Error while executing %s" % sql_string)
         logger.debug(traceback.print_exc())
         raise ex
Beispiel #4
0
 def query(self, sql, values=None):
     try:
         with self._get_cursor() as cur:
             if values is None:
                 cur.execute(sql)
             else:
                 cur.execute(sql, values)
             results = cur.fetchall()
     except Exception, ex:
         logger.debug("Error while executing %s" % sql)
         logger.debug(traceback.print_exc())
         raise ex
Beispiel #5
0
 def submit(self, sql):
     # conn = connect(self.conn_string)
     # conn.autocommit = True
     # conn.set_isolation_level(0)
     # cur = conn.cursor(cursor_factory=DictCursor)
     try:
         with PGDbAccess(self.conn_string, self.echo) as pga:
             pga.cur.execute(sql)
             pga.conn.commit()
     except Exception, ex:
         logger.debug("Error while executing %s" % sql)
         logger.debug(traceback.print_exc())
         raise ex
Beispiel #6
0
 def query(self, sql, values=None):
     try:
         with PGDbAccess(self.conn_string, self.echo) as pga:
             if values is None:
                 pga.cur.execute(sql)
             else:
                 pga.cur.execute(sql, values)
             #pga.conn.commit()
             results = pga.cur.fetchall()
     except Exception, ex:
         logger.debug("Error while executing %s" % sql)
         logger.debug(traceback.print_exc())
         raise ex
Beispiel #7
0
    def insertAndGetId(self, sql_string, values):
        try:
            sql = sql_string + " RETURNING id"
            with self._get_cursor() as cur:
                cur.execute(sql, values)
                inserted_rows = cur.fetchone()

            if inserted_rows:
                _id = inserted_rows[0]
                return _id
            else:
                return None
        except Exception, ex:
            logger.debug("Error while executing %s" % sql_string)
            logger.debug(traceback.print_exc())
            raise ex
Beispiel #8
0
 def insertManyOpt(self, table_name, col_names, format_string, values):
     #see http://stackoverflow.com/questions/8134602/psycopg2-insert-multiple-rows-with-one-query
     str_col_names = ",".join(c for c in col_names)
     try:
         with self._get_cursor() as cur:
             args = ",".join(cur.mogrify(format_string, val) for val in values)
             cur.execute("INSERT INTO %s (%s) VALUES %s" % (table_name, str_col_names, args))
     except Exception, ex:
         with self._get_cursor() as cur:
             for val in values:
                 try:
                     str_val = cur.mogrify(format_string, val)
                     sql = "INSERT INTO %s (%s) VALUES %s" % (table_name, str_col_names, str_val)
                     cur.execute(sql)
                 except Exception, ex:
                     logger.debug("Error while inserting %s" % (",".join(str(v) for v in val)))
                     raise ex
Beispiel #9
0
    def insertAndGetId(self, sql_string, values):
        try:
            sql = sql_string + " RETURNING id"
            with PGDbAccess(self.conn_string, self.echo) as pga:
                pga.cur.execute(sql, values)
                pga.conn.commit()
                inserted_rows = pga.cur.fetchone()

            if inserted_rows:
                _id = inserted_rows[0]
                return _id
            else:
                return None
        except Exception, ex:
            logger.debug("Error while executing %s" % sql_string)
            logger.debug(traceback.print_exc())
            raise ex
Beispiel #10
0
        try:
            with self._get_cursor() as cur:
                args = ",".join(cur.mogrify(format_string, val) for val in values)
                cur.execute("INSERT INTO %s (%s) VALUES %s" % (table_name, str_col_names, args))
        except Exception, ex:
            with self._get_cursor() as cur:
                for val in values:
                    try:
                        str_val = cur.mogrify(format_string, val)
                        sql = "INSERT INTO %s (%s) VALUES %s" % (table_name, str_col_names, str_val)
                        cur.execute(sql)
                    except Exception, ex:
                        logger.debug("Error while inserting %s" % (",".join(str(v) for v in val)))
                        raise ex

            logger.debug(traceback.print_exc())
            raise ex

    def insertAndGetId(self, sql_string, values):
        try:
            sql = sql_string + " RETURNING id"
            with self._get_cursor() as cur:
                cur.execute(sql, values)
                inserted_rows = cur.fetchone()

            if inserted_rows:
                _id = inserted_rows[0]
                return _id
            else:
                return None
        except Exception, ex: