def remove_casts(self, ssp): if not isinstance(ssp, ProfileList): raise RuntimeError("not passed a ProfileList, but %s" % type(ssp)) if not self.conn: logger.error("missing db connection") return False try: with self.conn: for i, self.tmp_data in enumerate(ssp.l): # logger.info("got a new SSP to store:\n%s" % self.tmp_data) if not self._get_ssp_pk(): raise sqlite3.Error("unable to get ssp pk: %s" % self.tmp_ssp_pk) if not self._delete_old_ssp(): raise sqlite3.Error("unable to clean ssp") return True except sqlite3.Error as e: logger.error("during adding casts, %s: %s" % (type(e), e)) return False
def get_all_urls(self): try: if self.conn: cursor = self.conn.cursor() cursor.execute('select title, url from szse') res = cursor.fetchall() cursor.close() return res else: raise sqlite3.Error('no sqllite connection') except: raise sqlite3.Error('query error ')
def _handle_statement(self, key, stmt): """Handles user and database statements defined in config.py""" msg = '' if key in STATEMENTS.keys(): match = re.fullmatch(STATEMENTS[key], stmt, re.IGNORECASE | re.VERBOSE) if match: if key in {'create-database', 'use-database', 'drop-database'}: dbname = match.group('dbname') if key == 'create-database': try: self.server.insert_new_database(dbname) msg = SUCCESS['database-created'] % dbname except sqlite3.IntegrityError: raise sqlite3.Error(ERROR['database-exists'] % dbname) elif key == 'use-database': if not self.server.is_database_exist(dbname): raise sqlite3.Error(ERROR['unknown-database'] % dbname) else: if self._old_dbname != dbname: self._old_dbname = dbname self._connect_db(dbname) msg = SUCCESS['database-changed'] elif key == 'drop-database': if not self.server.is_database_exist(dbname): raise sqlite3.Error(ERROR['no-database'] % dbname) else: self.server.delete_database_entry(dbname) path = DATABASES_DIR + dbname + DATABASE_EXT if os.path.exists(path): os.remove(os.path.relpath(path)) msg = SUCCESS['database-deleted'] % dbname elif key in {'add-user', 'delete-user'}: username = match.group('username') if key == 'add-user': password = match.group('pass') try: self.server.insert_new_user(username, password) msg = SUCCESS['user-added'] % username except: raise else: # delete-user self.server.delete_user(username) msg = SUCCESS['user-deleted'] % username elif key == 'show-databases': table = utils.TextTable() table.add_rows(self.server.select_databases()) msg = str(table) msg = "\n%s\n" % msg if len(msg) > 0 else " " return msg
def insert_by_sql(self, sql, *args, **kwargs): try: if self.conn: cursor = self.conn.cursor() cursor.execute(sql, args) cursor.close() self.conn.commit() else: raise sqlite3.Error('no sqllite connection') except: raise sqlite3.Error('insert error')
def if_url_exists(self, url): try: if self.conn: cursor = self.conn.cursor() url_md5 = hashlib.md5(url.encode('utf-8')).hexdigest() res = cursor.execute('select count(url) as url_count from szse where url_md5 = ?', url_md5) cursor.close() return res else: raise sqlite3.Error('no sqllite connection') except: raise sqlite3.Error('query error ')
def run(self): while True: time.sleep(15) URL = "http://" + self.ip + "/api/v1/receive" params = {'id_dest': self.id} r = requests.get(url=URL, params=params) try: sqliteConnection = sqlite3.connect('client.db') cursor = sqliteConnection.cursor() if len(r.json()) != 0: for i in r.json(): cursor.execute( f"INSERT INTO receivedMessages (text,sender,received_at,alreadyRead) VALUES ('{i[1]}','{i[0]}','{i[3]}',{False})" ) sqliteConnection.commit() except sqlite3.Error(): print('Errore !!') finally: if (sqliteConnection): sqliteConnection.close()
def run(self): while True: time.sleep(5) params = {'id_dest': self.id} URL = "http://" + self.ip + "/api/v1/receive" r = requests.get(url=URL, params=params) if r.status_code == 200: try: sqliteConnection = sqlite3.connect('clientDB.db') cursor = sqliteConnection.cursor() if len(r.json()) != 0: for i in r.json(): cursor.execute( f"INSERT INTO receivedMessages (text,sender,received_at,alreadyRead) VALUES ('{i[1]}',{i[2]},'{i[3]}',{False})" ) sqliteConnection.commit() except sqlite3.Error() as e: print('Error: ' + e) finally: if (sqliteConnection): sqliteConnection.close() else: print('status code error')
def download_bot_result_button(user_name, days_to_keep=180): """ Actions took when the "Check bot" button is clicked. :param user_name: :return: """ # Check if target account name was given if user_name == "": st.error("You need to specify a target account name.") raise TypeError("user_name is empty") # Load followers table database = "temp/" + user_name + "_followers.db" try: conn = create_connection(database) except sqlite3.Error: st.error("Cannot find cached database. " "Either run Retrieve Twitter followers function first or" "upload a database.") raise sqlite3.Error("Cannot connect to database") # Show download link followers_df = pd.read_sql_query("SELECT * FROM followers", conn) followers_csv = "temp/" + user_name + "_followers.csv" followers_df.to_csv(followers_csv) st.sidebar.markdown(get_download_link(followers_csv, 'followers csv table'), unsafe_allow_html=True)
def _execute(self, query, params=''): res = None error_msg = '' select = query.lstrip().partition(' ')[0].lower() == 'select' params = list(map(self.sanitize, params)) logger.debug(query) logger.debug(params) for _ in range(self._retries): try: cur = self._connection.cursor() cur.execute(query, params) if select: res = cur.fetchone() else: self._connection.commit() break except sqlite3.Error as error: logger.exception(error) error_msg = str(error) try: self._connection.close() except sqlite3.Error: pass time.sleep(1) # Intentionally not catching exceptions here self._connection = sqlite3.connect(self._filename) self._connection.row_factory = row_factory else: raise sqlite3.Error(error_msg) return res
def refresh(self): self.connect() if not self.__con: raise sqlite3.Error('Database %s not connected' % self.__db) if self.__properties[self.__keyField] != None: self.doQuery('SELECT %s FROM `%s` WHERE `%s` = %d;' % (','.join("`%s`" % f for f in self.__fields), self.__table, self.__keyField, self.__properties[self.__keyField])) row = self.__cur.fetchone() if row == None: #raise sqlite3.Error('No record found for field "%s" in table "%s"' % (self.__primaryKey, self.__table) self.__isNew = True self.__dirtyFields = self.__getWritableFields() else: for f in self.__fields: self.__properties[f] = row[f] self.__isNew = False self._dirtyFields = [] self.__dataLoaded = True else: self.__isNew = True self.__dataLoaded = False self.disconnect()
def __init__(self, path): self.path = path try: self.db = sqlite3.connect(self.path) except sqlite3.Error as err: raise sqlite3.Error( "%s: %s\nCheck if the user running BroControl has both write and search permission to\nthe directory containing the database file and has both read and write\npermission to the database file itself." % (err, path)) self.c = self.db.cursor() try: self.setup() except sqlite3.Error as err: raise sqlite3.Error("%s: %s" % (err, path))
def getLastStateHistory(self): """ Return every entry in the stateHistory table Args: None Returns: entrys: The list of states in the state history table Raises: DatabaseException: If the database is empty sqlite3.Error: If the program cannot connect to the database """ try: conn = sqlite3.connect(self.fileName) cursor = conn.cursor() cursor.execute("SELECT * FROM stateHistory") entrys = cursor.fetchall() conn.close() except sqlite3.Error as exception: raise sqlite3.Error("Could not connect to database! (%s)" % exception) return entrys
def check_login(password): global key # pass digest password_digest = hashlib.sha256(password.encode('utf-8')).hexdigest() # open db try: conn = sqlite3.connect('pyauth/pyauthentication.db') c = conn.cursor() # Check credentials c.execute( " SELECT * FROM User WHERE password_digest = '{password_digest}'". format(password_digest=password_digest)) if not c.fetchall(): raise sqlite3.Error() conn.close() # Create token token = jwt.encode( { 'user': hashlib.sha1(password.encode('UTF-8')).hexdigest(), 'exp': datetime.datetime.utcnow() + datetime.timedelta(hours=24) }, key).decode('UTF-8') # token = jwt.encode({'user' : hashlib.sha1(password.encode('UTF-8')).hexdigest(), 'exp' : datetime.datetime.utcnow() + datetime.timedelta(seconds = 10)} , key).decode('UTF-8') return {"status": "success", "auth-token": token} except sqlite3.Error: conn.close() return {"status": "error", "message": "Invalid credential"}
def index_sword_module(self, mod) -> None: """Index a sword module """ logger.debug("ThekeIndexBuilder - Index %s", mod.get_name()) # Add the module to the index sourceId = self.add_source(mod.get_name(), SOURCETYPE_SWORD, mod.get_type(), mod.get_version(), mod.get_lang()) if sourceId is None: raise sqlite3.Error("Fails to index the module {}".format( mod.get_name())) # Add the module description to the index self.index.execute_returning_id( """INSERT INTO sourceDescriptions (id_source, description) VALUES(?, ?); """, (sourceId, mod.get_description())) self.index.commit() # Next indexing steps depend of the module type if mod.get_type() == theke.sword.MODTYPE_BIBLES: self.index_sword_biblical_module(sourceId, mod) elif mod.get_type() == theke.sword.MODTYPE_GENBOOKS: self.index_sword_book_module(sourceId, mod) else: logger.debug("ThekeIndexBuilder - Unknown type (%s) of %s", mod.get_type(), mod.get_name())
def insert_oui_data(cursor): """ Insert the oui data in the oui table. """ start_time = time.time() # Declare sql insert query. insert_query = "INSERT OR IGNORE INTO oui (mac_prefix, vendor_name) VALUES(?, ?)" try: # Start sql transaction cursor.execute("BEGIN TRANSACTION") # Count of sql rows. rows = 1 # Bulk insert. for mac_prefix, vendor_name in read_nmap_file(): cursor.execute(insert_query, (mac_prefix, vendor_name)) rows += 1 # Close transaction. cursor.execute("COMMIT") print("[*] Took {0:3f} sec to insert {1} rows.".format((time.time() - start_time), rows)) except sqlite3.Error as sqliteError: raise sqlite3.Error(sqliteError) except Exception as e: raise Exception(e) finally: cursor.close()
def getLastStateEntry(self): """ Return the last state entry added to the stateHistory table Args: None Returns: entry: The last state entry added to the table Raises: DatabaseException: If the database is empty sqlite3.Error: If the program cannot connect to the database """ try: conn = sqlite3.connect(self.fileName) cursor = conn.cursor() cursor.execute( "SELECT * FROM stateHistory ORDER BY updateID DESC LIMIT 1") entry = cursor.fetchone() conn.close() except sqlite3.Error as exception: raise sqlite3.Error("Could not connect to database! (%s)" % exception) return entry
def __init__(self): self.__logger = Logger() self._request_exceptions = [type(item) for item in [requests.ConnectionError(), requests.HTTPError(), requests.TooManyRedirects(), requests.Timeout(), requests.TooManyRedirects(), requests.RequestException(), requests.ConnectTimeout(), requests.ReadTimeout()]] self._system_errors = [type(item) for item in [KeyError(), AttributeError(), IndexError(), ZeroDivisionError(), SystemError(), ValueError(), AssertionError()]] self._file_errors = [type(item) for item in [FileExistsError(), FileNotFoundError()]] self._database_errors = [type(item) for item in [sqlite3.Error(), sqlite3.DataError(), sqlite3.ProgrammingError(), sqlite3.DatabaseError(), sqlite3.NotSupportedError(), sqlite3.IntegrityError(), sqlite3.InterfaceError(), sqlite3.InternalError(), sqlite3.OperationalError()]] self._speech_recognizer_errors = [type(item) for item in [sr.RequestError(), sr.UnknownValueError(), sr.WaitTimeoutError(), sr.RequestError()]] self.__logger.info('ExceptionsHandler was successfully initialized.', __name__)
def _handle_error(error, sql_phrase, variables): log.critical("%s - with SQL --> %s and args[%s]", str(error), sql_phrase, pformat(variables)) if 'syntax error' in str(error): raise SyntaxError(error) raise sqlite.Error(error)
def getUserEntryFromUsername(self, username): """ Checks if an entry with given an username exists in the database Args: username: The username of the current user Returns: entry: A list containing the every item of the entry corresponding to the given username if it exists in the database Raises: DatabaseException: If an entry with the provided username is not found in the database sqlite3.Error: If the program cannot connect to the database """ try: conn = sqlite3.connect(self.fileName) cursor = conn.cursor() cursor.execute("SELECT * from userLogins WHERE username = (?)", ( username, )) # Use parametised queries to protect against SQLi entry = cursor.fetchone() conn.close() except sqlite3.Error as exception: raise sqlite3.Error("Could not connect to database! (%s)" % exception) if entry is None: raise DatabaseException( "Could not find entry in userLogins table!") return entry
def create_table_for_img_info(dbcr, img_info): 'Creates a database table, in a database cursor, to store for the input img_info' create_command = 'CREATE TABLE {}({} TEXT PRIMARY KEY,'.format( SQLITE_IMG_INFO_TABLE, SQLITE_IMG_INFO_FNAME) for key in list(img_info.keys()): create_command += ' "{}" TEXT,'.format(info_key_to_db_name(key)) create_command = create_command[0:-1] + ')' # Can make a rare race condition if multiple processes try to create the file at the same time; # If that happens, the error is: # sqlite3.OperationalError: table img_info already exists for file ....... try: dbcr.execute(create_command) except sqlite3.OperationalError as op_err: if 'table {} already exists'.format( SQLITE_IMG_INFO_TABLE) in op_err.message: # another process has just created the table, so sleep(1) # This is only when a db file is created so isn't called often # (and the race condition is rare!) time.sleep(1) else: # everything else needs to be reported and raised immediately: raise sqlite3.OperationalError(op_err.message) except sqlite3.Error as sq_err: # everything else needs to be reported and raised immediately: raise sqlite3.Error(sq_err.message)
def test_push_handles_connection_exception(self): with utils.NamedTemporaryFile() as fh: with utils.mock.patch( 'wakatime.offlinequeue.Queue.get_db_file') as mock_db_file: mock_db_file.return_value = fh.name response = Response() response.status_code = 500 self.patched[ 'wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response now = u(int(time.time())) entity = 'tests/samples/codefiles/twolinefile.txt' config = 'tests/samples/configs/good_config.cfg' with utils.mock.patch( 'wakatime.offlinequeue.Queue.connect') as mock_connect: mock_connect.side_effect = sqlite3.Error('') args = [ '--file', entity, '--config', config, '--time', now ] execute(args) response.status_code = 201 execute(args) queue = Queue() saved_heartbeat = queue.pop() self.assertEquals(None, saved_heartbeat)
def test_push_handles_exception_on_close(self, logs): logging.disable(logging.NOTSET) with NamedTemporaryFile() as fh: with mock.patch('wakatime.offlinequeue.Queue._get_db_file') as mock_db_file: mock_db_file.return_value = fh.name response = Response() response.status_code = 500 self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response now = u(int(time.time())) entity = 'tests/samples/codefiles/twolinefile.txt' config = 'tests/samples/configs/good_config.cfg' mock_conn = mock.MagicMock() mock_conn.close.side_effect = sqlite3.Error('') mock_c = mock.MagicMock() mock_c.fetchone.return_value = None with mock.patch('wakatime.offlinequeue.Queue.connect') as mock_connect: mock_connect.return_value = (mock_conn, mock_c) args = ['--file', entity, '--config', config, '--time', now] execute(args) response.status_code = 201 execute(args) queue = Queue(None, None) saved_heartbeat = queue.pop() self.assertEquals(None, saved_heartbeat) self.assertNothingPrinted()
def connect(dbapi_connection, connection_record): """ Called once by SQLAlchemy for each new SQLite DB-API connection. Here is where we issue some PRAGMA statements to configure how we're going to access the SQLite database. @param dbapi_connection: A newly connected raw SQLite DB-API connection. @param connection_record: Unused by this method. """ try: cursor = dbapi_connection.cursor() try: cursor.execute("PRAGMA foreign_keys = ON;") cursor.execute("PRAGMA foreign_keys;") if cursor.fetchone()[0] != 1: raise Exception() finally: cursor.close() except Exception: dbapi_connection.close() raise sqlite3.Error()
def run(self): """Handle client - server session.""" with self.conn: # 1. client's connection (login) data = self.conn.recv(1024) data = data.decode(encoding="utf-8") if data: if self.server.is_user_exist(*data.split(DATA_SEPARATOR)): self.conn.send(ACCESS_GRANTED) self._client_connected = True else: self.conn.send(ACCESS_DENIED) # 2. main activity if self._client_connected: table = utils.TextTable() message = '' while self.conn: try: sql = self.conn.recv(1024) sql = sql.decode(encoding="utf-8") if sql: # Try to execute custom statements (on user, database) parts = sql.split(DATA_SEPARATOR) if len(parts) == 2: message = self._handle_statement(*parts) else: if not self.db_conn: raise sqlite3.Error( ERROR['no-database-seleted']) cursor = self.db_conn.cursor() start = time.time() cursor.execute(sql) end = time.time() elapsed_time = end - start time_passed = "(%.3f sec)" % elapsed_time if _is_select_statement(sql): table.clear() results = cursor.fetchall() # table.header(_extract_fields(sql)) if results: table.add_rows(results) message = "%s\n %d rows in set %s\n" % ( str(table), len(results), time_passed) else: message = f"\nEmpty set {time_passed}\n" else: self.db_conn.commit() message = "\nQuery done %s\n" % time_passed except sqlite3.Error as e: message = f"\nERROR: {str(e)}\n" if self.db_conn: self.db_conn.rollback() try: self.conn.sendall(message.encode("utf-8")) except BrokenPipeError: break self._close_db_connection() self.conn.close()
def test_enable_error(): """ Test enabled a bundle ID that throws a command error """ with patch.object(assistive.TccDB, "enable", side_effect=sqlite3.Error("Foo")): pytest.raises(CommandExecutionError, assistive.enable_, "foo")
def test_delete_file_fails(self, mocker): connection = mocker.MagicMock() connection \ .cursor.return_value \ .execute.side_effect = sqlite3.Error() indexer = Indexer(connection, mocker.Mock()) assert indexer.delete(File("/full/path.log", "path.log")) is False
def test_remove_assistive_error(): """ Test removing an assitive bundle. """ with patch.object(assistive.TccDB, "remove", side_effect=sqlite3.Error("Foo")): pytest.raises(CommandExecutionError, assistive.remove, "foo")
def test_install_assistive_error(): """ Test installing a bundle ID as being allowed to run with assistive access """ with patch.object(assistive.TccDB, "install", side_effect=sqlite3.Error("Foo")): pytest.raises(CommandExecutionError, assistive.install, "foo")
def get_voter(thread: SQLThread, condition: Condition, *, uid: int, vid: int): if uid is not None: sql_thread_logger.debug("Thread {} requesting Member with UID {}'s data".format(get_ident(), uid)) return thread.request(("SELECT * FROM Members WHERE uid = ?;", (uid, )), condition) elif vid is not None: sql_thread_logger.debug("Thread {} requesting Member with VID {}'s data".format(get_ident(), uid)) return thread.request(("SELECT * FROM Members WHERE vid = ?;", (vid, )), condition) raise sqlite3.Error("No arguments provided to voter get function")
def _check_db_format(self): """Checks whether database file is valid SQLite database.""" with open(self._path, "r", encoding="Latin1") as f: ima = codecs.encode(str.encode(f.read(16)), 'hex_codec') if ima and ima != b"53514c69746520666f726d6174203300": message = "Specified file is not a valid SQLite database." raise sqlite.Error(message)