def __close_connection(self, cur: sqlite3.Cursor, conn: sqlite3.Connection): if cur: cur.close() else: print("Warning: No cursor defined.") if conn: conn.close() else: print("Warning: No connection defined.")
def create_destination_table(route_table: dict, cursor: sqlite3.Cursor) -> None: """Creates destination table in database and fills it""" cursor.execute('''CREATE TABLE destination (destination text, preference integer, metric integer, next_hop text, interface text, age integer)''') for next_hop, dests_dict in route_table["route_table"]["next_hop"].items(): for dest, data in dests_dict.items(): cursor.execute("INSERT INTO destination VALUES (?, ?, ?, ?, ?, ?)", (dest, data["preference"], data["metric"], next_hop, data["via"], age_to_seconds(data["age"]))) cursor.close()
def create_from(self, input_file: TextIOWrapper, chunk_size: int, cur: sqlite3.Cursor): """Create a new database with the SWHIDs present inside the input file.""" try: self.create_table(cur) cur.execute("PRAGMA synchronous = OFF") cur.execute("PRAGMA journal_mode = OFF") self.add(self.iter_swhids(input_file), chunk_size, cur) cur.close() self.conn.commit() except sqlite3.Error as e: raise DBError(f"SQLite error: {e}")
def __delete_connection(self, cursor: sqlite3.Cursor, db_conn: sqlite3.Connection) -> None: """ This *private* method closes the database connection and cursor. :param cursor: the cursor object used to execute sql statements :param db_conn: the database connection :return: None """ # 6/28/21 - added if statements, should it be try-except? try: cursor.close() try: db_conn.close() except Error: db_conn = None except Error: cursor = None
def checkFast(cursor: sqlite3.Cursor, startID: int, endID: int, hashFunc=hashlib.sha256, closeCursor=True) -> int: """블록들의 해시를 검사합니다. 오류가 없을 시, startID, 오류가 있을 시, 해당 id를 반환합니다.""" cursor.execute(""" SELECT * FROM block WHERE {0} <= id AND id < {1} """.format(startID, endID)) retval = startID gen = blockSQL.sql.tool.fetch_module.fetchoneGenerator(cursor) previousHash = blockSQL.sql.tool.block_module.createHash( next(gen), hashFunc) for row in gen: if (row[4] != previousHash): retval = row[0] break previousHash = blockSQL.sql.tool.block_module.createHash(row, hashFunc) if (closeCursor): cursor.close() return retval
def _iterone(cur: Cursor) -> Sequence: row=cur.fetchone() cur.close() return row
def itercur(cur: Cursor) -> Generator: row=cur.fetchone() while row is not None: yield row row=cur.fetchone() cur.close()
def disconnect_db(conn: sqlite3.Connection, cursor: sqlite3.Cursor) -> None: """Safely close down a connection to a DB""" cursor.close() conn.commit() conn.close()
def encerraBD(cur: sqlite3.Cursor, db: sqlite3.Connection): cur.close() db.commit() db.close() return "BD Encerrado"
def create_next_hop_table(route_table: dict, cursor: sqlite3.Cursor) -> None: """Creates next_hop table in database and fills it""" cursor.execute("CREATE TABLE next_hop (address text)") for next_hop in route_table["route_table"]["next_hop"]: cursor.execute('INSERT INTO next_hop VALUES (?)', (next_hop, )) cursor.close()
def close_db(connection: sqlite3.Connection, cursor: sqlite3.Cursor): connection.commit() cursor.close() connection.close()