Python sqlite3.connect() Examples

The following are 30 code examples for showing how to use sqlite3.connect(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module sqlite3 , or try the search function .

Example 1
Project: Starx_Pixiv_Collector   Author: SuzukiHonoka   File: start.py    License: MIT License 7 votes vote down vote up
def ip_latency_test(ip, port=443):
    tag = 'IP_Latency_TEST'
    print_with_tag(tag, ['Prepare IP latency test for ip', ip, 'Port', str(port)])
    s_test = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s_test.settimeout(10)
    s_start = time.time()
    try:
        s_test.connect((ip, port))
        s_test.shutdown(socket.SHUT_RD)
    except Exception as e:
        print_with_tag(tag, ['Error:', e])
        return None
    s_stop = time.time()
    s_runtime = '%.2f' % (1000 * (s_stop - s_start))
    print_with_tag(tag, [ip, 'Latency:', s_runtime])
    return float(s_runtime) 
Example 2
Project: iSDX   Author: sdn-ixp   File: rib.py    License: Apache License 2.0 6 votes vote down vote up
def __init__(self,ip,name):
        with lock:
            # Create a database in RAM
            self.db = sqlite3.connect('/home/vagrant/iSDX/xrs/ribs/'+ip+'.db',check_same_thread=False)
            self.db.row_factory = sqlite3.Row
            self.name = name

            qs = ', '.join(['?']*len(labels))
            self.insertStmt = 'insert into %s values (%s)' % (self.name, qs)

            stmt = (
                    'create table if not exists '+self.name+
                    ' ('+ ', '.join([l+' '+t for l,t in zip(labels, types)])+')'
                    )

            cursor = self.db.cursor()
            cursor.execute(stmt)
            self.db.commit() 
Example 3
Project: CAMISIM   Author: CAMI-challenge   File: ref_seq.py    License: Apache License 2.0 6 votes vote down vote up
def getGenomeWgsCount(self, ncbid, threshold):
        """
            Gets the number of genomes/wgs available in the directory.

            @param threshold: it returns max. threshold genomes/wgs (after this threshold is reached, it returns)
            @param dir: directory that contains genomes/wgs in the form: "ncbid.[0-9]*.f[an][sa]"

            @return: the number of genomes/wgs from different species that are subclades of the input ncbid
        """
        try:
            conn = sqlite3.connect(os.path.normpath(self._databaseFile))
            cursor = conn.cursor()

            speciesIdsList = []
            self._collectSpecies(speciesIdsList, cursor, ncbid, threshold)
            return len(speciesIdsList)
        except Exception:
            print "Failed to create connection to a database:", self._databaseFile
            raise
        finally:
            cursor.close()
            conn.close() 
Example 4
Project: git2net   Author: gotec   File: extraction.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def _process_repo_serial(git_repo_dir, sqlite_db_file, commits, extraction_settings):
    """ Processes all commits in a given git repository in a serial manner.

    Args:
        git_repo_dir: path to the git repository that is mined
        sqlite_db_file: path (including database name) where the sqlite database will be created
        commits: list of commits that have to be processed
        extraction_settings: settings for the extraction

    Returns:
        sqlite database will be written at specified location
    """

    git_repo = pydriller.GitRepository(git_repo_dir)

    con = sqlite3.connect(sqlite_db_file)

    for commit in tqdm(commits, desc='Serial'):
        args = {'git_repo_dir': git_repo_dir, 'commit_hash': commit.hash, 'extraction_settings': extraction_settings}
        result = _process_commit(args)

        if not result['edits'].empty:
            result['edits'].to_sql('edits', con, if_exists='append', index=False)
        if not result['commit'].empty:
            result['commit'].to_sql('commits', con, if_exists='append', index=False) 
Example 5
Project: ANGRYsearch   Author: DoTheEvo   File: angrysearch_update_database.py    License: GNU General Public License v2.0 6 votes vote down vote up
def new_database(table):
    temp_db_path = TEMP_PATH + '/angry_database.db'

    if os.path.exists(temp_db_path):
        os.remove(temp_db_path)

    con = sqlite3.connect(temp_db_path, check_same_thread=False)
    cur = con.cursor()

    if fts5_pragma_check():
        cur.execute('''CREATE VIRTUAL TABLE angry_table
                        USING fts5(directory, path, size, date)''')
        cur.execute('''PRAGMA user_version = 4;''')
    else:
        cur.execute('''CREATE VIRTUAL TABLE angry_table
                        USING fts4(directory, path, size, date)''')
        cur.execute('''PRAGMA user_version = 3;''')

    cur.executemany('''INSERT INTO angry_table VALUES (?, ?, ?, ?)''', table)

    con.commit()
    replace_old_db_with_new() 
Example 6
Project: ANGRYsearch   Author: DoTheEvo   File: angrysearch_update_database.py    License: GNU General Public License v2.0 6 votes vote down vote up
def new_database_lite(table):
    temp_db_path = TEMP_PATH + '/angry_database.db'

    if os.path.exists(temp_db_path):
        os.remove(temp_db_path)

    con = sqlite3.connect(temp_db_path, check_same_thread=False)
    cur = con.cursor()

    if fts5_pragma_check():
        cur.execute('''CREATE VIRTUAL TABLE angry_table
                        USING fts5(directory, path)''')
        cur.execute('''PRAGMA user_version = 4;''')
    else:
        cur.execute('''CREATE VIRTUAL TABLE angry_table
                        USING fts4(directory, path)''')
        cur.execute('''PRAGMA user_version = 3;''')

    cur.executemany('''INSERT INTO angry_table VALUES (?, ?)''', table)

    con.commit()
    replace_old_db_with_new() 
Example 7
Project: ANGRYsearch   Author: DoTheEvo   File: angrysearch.py    License: GNU General Public License v2.0 6 votes vote down vote up
def replace_old_db_with_new(self):
        global con
        global DATABASE_PATH

        temp_db_path = TEMP_PATH + '/angry_database.db'

        dir_path = os.path.dirname(DATABASE_PATH)

        if not os.path.exists(temp_db_path):
            return
        if not os.path.exists(dir_path):
            os.makedirs(dir_path)

        if con:
            con.close()
        shutil.move(temp_db_path, DATABASE_PATH)

        con = sqlite3.connect(DATABASE_PATH, check_same_thread=False)
        con.create_function("regexp", 2, regexp) 
Example 8
Project: ANGRYsearch   Author: DoTheEvo   File: angrysearch.py    License: GNU General Public License v2.0 6 votes vote down vote up
def contextMenuEvent(self, event):
        right_click_menu = Qw.QMenu(self)

        act_open = right_click_menu.addAction('Open')
        act_open.triggered.connect(self.parent().parent().right_clk_open)

        act_open_path = right_click_menu.addAction('Open Path')
        act_open_path.triggered.connect(self.parent().parent().right_clk_path)

        right_click_menu.addSeparator()

        act_copy_path = right_click_menu.addAction('Copy Path')
        act_copy_path.triggered.connect(self.parent().parent().right_clk_copy)

        right_click_menu.exec_(event.globalPos())


# THE PRIMARY GUI DEFINING INTERFACE WIDGET, THE WIDGET WITHIN THE MAINWINDOW 
Example 9
Project: clashroyale   Author: cgrok   File: utils.py    License: MIT License 6 votes vote down vote up
def connection(self, commit_on_success=False):
        with self._lock:
            if self._bulk_commit:
                if self._pending_connection is None:
                    self._pending_connection = sqlite.connect(self.filename)
                con = self._pending_connection
            else:
                con = sqlite.connect(self.filename)
            try:
                if self.fast_save:
                    con.execute("PRAGMA synchronous = 0;")
                yield con
                if commit_on_success and self.can_commit:
                    con.commit()
            finally:
                if not self._bulk_commit:
                    con.close() 
Example 10
Project: clashroyale   Author: cgrok   File: utils.py    License: MIT License 6 votes vote down vote up
def connection(self, commit_on_success=False):
        with self._lock:
            if self._bulk_commit:
                if self._pending_connection is None:
                    self._pending_connection = sqlite.connect(self.filename)
                con = self._pending_connection
            else:
                con = sqlite.connect(self.filename)
            try:
                if self.fast_save:
                    con.execute("PRAGMA synchronous = 0;")
                yield con
                if commit_on_success and self.can_commit:
                    con.commit()
            finally:
                if not self._bulk_commit:
                    con.close() 
Example 11
Project: razzy-spinner   Author: rafasashi   File: chat80.py    License: GNU General Public License v3.0 6 votes vote down vote up
def sql_query(dbname, query):
    """
    Execute an SQL query over a database.
    :param dbname: filename of persistent store
    :type schema: str
    :param query: SQL query
    :type rel_name: str
    """
    import sqlite3
    try:
        path = nltk.data.find(dbname)
        connection =  sqlite3.connect(str(path))
        cur = connection.cursor()
        return cur.execute(query)
    except (ValueError, sqlite3.OperationalError):
        import warnings
        warnings.warn("Make sure the database file %s is installed and uncompressed." % dbname)
        raise 
Example 12
Project: custodia   Author: latchset   File: sqlite.py    License: GNU General Public License v3.0 6 votes vote down vote up
def get(self, key):
        self.logger.debug("Fetching key %s", key)
        query = "SELECT value from %s WHERE key=?" % self.table
        try:
            conn = sqlite3.connect(self.dburi)
            c = conn.cursor()
            r = c.execute(query, (key,))
            value = r.fetchall()
        except sqlite3.Error:
            self.logger.exception("Error fetching key %s", key)
            raise CSStoreError('Error occurred while trying to get key')
        self.logger.debug("Fetched key %s got result: %r", key, value)
        if len(value) > 0:
            return value[0][0]
        else:
            return None 
Example 13
Project: custodia   Author: latchset   File: sqlite.py    License: GNU General Public License v3.0 6 votes vote down vote up
def set(self, key, value, replace=False):
        self.logger.debug("Setting key %s to value %s (replace=%s)",
                          key, value, replace)
        if key.endswith('/'):
            raise ValueError('Invalid Key name, cannot end in "/"')
        if replace:
            query = "INSERT OR REPLACE into %s VALUES (?, ?)"
        else:
            query = "INSERT into %s VALUES (?, ?)"
        setdata = query % (self.table,)
        try:
            conn = sqlite3.connect(self.dburi)
            with conn:
                c = conn.cursor()
                self._create(c)
                c.execute(setdata, (key, value))
        except sqlite3.IntegrityError as err:
            raise CSStoreExists(str(err))
        except sqlite3.Error:
            self.logger.exception("Error storing key %s", key)
            raise CSStoreError('Error occurred while trying to store key') 
Example 14
Project: custodia   Author: latchset   File: sqlite.py    License: GNU General Public License v3.0 6 votes vote down vote up
def span(self, key):
        name = key.rstrip('/')
        self.logger.debug("Creating container %s", name)
        query = "INSERT into %s VALUES (?, '')"
        setdata = query % (self.table,)
        try:
            conn = sqlite3.connect(self.dburi)
            with conn:
                c = conn.cursor()
                self._create(c)
                c.execute(setdata, (name,))
        except sqlite3.IntegrityError as err:
            raise CSStoreExists(str(err))
        except sqlite3.Error:
            self.logger.exception("Error creating key %s", name)
            raise CSStoreError('Error occurred while trying to span container') 
Example 15
Project: slack-bot   Author: EvilInsultGenerator   File: eig_plugin.py    License: The Unlicense 6 votes vote down vote up
def sqlite_get_user_lang(user):
    BASE_DIR = os.path.dirname(os.path.abspath(__file__))
    db_path = os.path.join(BASE_DIR, "eig_bot.db")
    conn = sqlite3.connect(db_path)
    sql = "SELECT lang FROM users WHERE user='"+user+"';";
    c = conn.cursor()
    c.execute(sql)
    data=c.fetchone()
    if not data:
        sql = "INSERT INTO users(user, lang) VALUES ('"+user+"', 'en');"
        c.execute(sql)
        conn.commit()
        conn.close()
        return 'en'
    else :
        lang = data[0]
        conn.close()
        return lang 
Example 16
Project: gphotos-sync   Author: gilesknap   File: LocalData.py    License: MIT License 6 votes vote down vote up
def check_schema_version(self):
        query = "SELECT  Version FROM  Globals WHERE Id IS 1"
        self.cur.execute(query)
        version = float(self.cur.fetchone()[0])
        if version > self.VERSION:
            raise ValueError("Database version is newer than gphotos-sync")
        elif version < self.VERSION:
            log.warning(
                "Database schema out of date. Flushing index ...\n"
                "A backup of the previous DB has been created"
            )
            self.con.commit()
            self.con.close()
            self.backup_sql_file()
            self.con = lite.connect(str(self.db_file))
            self.con.row_factory = lite.Row
            self.cur = self.con.cursor()
            self.cur2 = self.con.cursor()
            self.clean_db() 
Example 17
Project: BASS   Author: Cisco-Talos   File: bindiffdb.py    License: GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, filename):
        self.filename = filename
        self.similar_functions = []
        self.filename = filename
        self.connect() 
Example 18
Project: BASS   Author: Cisco-Talos   File: bindiffdb.py    License: GNU General Public License v2.0 5 votes vote down vote up
def connect(self):
        self.dbconn = sqlite3.connect(self.filename) 
Example 19
Project: Starx_Pixiv_Collector   Author: SuzukiHonoka   File: start.py    License: MIT License 5 votes vote down vote up
def proxy_and_sni_switch():
    global proxy_enable, s, sni_bypass, server_ip, dl_server_ip, post_url
    tag = 'Proxy_And_Sni_Switch'
    if proxy_enable:
        print_with_tag(tag, 'Going to connect to your socks5 server..')
        try:
            # socks.set_default_proxy(socks.SOCKS5, proxy_host, int(proxy_port))
            # socket.socket = socks.socksocket
            # socket.timeout = 500
            proxies = {
                "http": "socks5://" + proxy_host + ":" + proxy_port,
                'https': "socks5://" + proxy_host + ":" + proxy_port
            }
            s.proxies = proxies
        except Exception as e:
            print_with_tag(tag, 'An error occurred when setting up the socks5 proxy for request.')
            print_with_tag(tag, e)
            exit()
        else:
            print_with_tag(tag, 'Proxy connection seems successfully created!!')
    else:
        print_with_tag(tag, 'Not using the proxy..')
        if sni_bypass:
            tag = 'Experiment Function: SNI Bypass'
            print_with_tag(tag, 'Starting..')
            ##########################################################
            print_with_tag(tag, 'Using DoH server to get the correct DNS record.')
            server_ip = get_dns_data_from_doh_server('www.pixiv.net', 'A')
            print_with_tag(tag, ['Server IP =>', server_ip])
            dl_server_ip = get_dns_data_from_doh_server('i.pximg.net', 'A')
            print_with_tag(tag, ['DL Server IP =>', dl_server_ip])
            post_url = post_url.replace('accounts.pixiv.net', server_ip)
            print_with_tag(tag, 'Setting up SSLadapter..')
            # common
            s.mount('https://', host_header_ssl.HostHeaderSSLAdapter())
            # download
            print_with_tag(tag, 'SNI Bypass done.') 
Example 20
Project: Starx_Pixiv_Collector   Author: SuzukiHonoka   File: start.py    License: MIT License 5 votes vote down vote up
def update_database(illustID, illustTitle, illustType, userId, userName, tags, urls):
    tag = 'Update_Database'
    # Connect database
    conn = sqlite3.connect('illust_data.db')
    c = conn.cursor()
    # Create table
    if len(c.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='ILLUST_DATA'").fetchall()) == 0:
        print_with_tag(tag, 'Creating table..')
        c.execute('''CREATE TABLE ILLUST_DATA
                     (ID INT PRIMARY KEY NOT NULL, TITLE TEXT NOT NULL, TYPE INT NOT NULL, USER_ID INT NOT NULL,USER_NAME TEXT NOT NULL,TAGS TEXT NOT NULL,IMG_SRC TEXT NOT NULL)''')
        print_with_tag(tag, 'Done.')
    if len(c.execute("SELECT ID FROM ILLUST_DATA WHERE ID = ?", (str(illustID),)).fetchall()) == 0:
        print_with_tag(tag, ['Ready to insert data for ID:', str(illustID)])
        # Insert a row of data
        sql = "INSERT INTO ILLUST_DATA(ID,TITLE,TYPE,USER_ID,USER_NAME,TAGS,IMG_SRC)VALUES(?,?,?,?,?,?,?)"
        c.execute(sql, (str(illustID), str(illustTitle), str(illustType),
                        str(userId), str(userName), str(tags), str(urls)))
        print_with_tag(tag, 'Insert done.')
        # Save (commit) the changes
        conn.commit()
        print_with_tag(tag, 'Committed.')
    else:
        print_with_tag(tag, ['ID exist:', str(illustID)])
    # We can also close the connection if we are done with it.
    # Just be sure any changes have been committed or they will be lost.
    conn.close() 
Example 21
Project: news-corpus-builder   Author: skillachie   File: news_corpus_generator.py    License: MIT License 5 votes vote down vote up
def _set_up_db(self,db):

        if os.path.exists(db):
            print 'Database exists, assume schema does, too.'
        else:
            print 'Need to create schema'
            print 'Creating schema...'
            conn = sqlite3.connect(db)
            cur = conn.cursor()
            cur.execute("create table articles (Id INTEGER PRIMARY KEY,category, title,body)")
            cur.execute("CREATE UNIQUE INDEX uni_article on articles (category,title)")
            conn.close() 
Example 22
Project: news-corpus-builder   Author: skillachie   File: news_corpus_generator.py    License: MIT License 5 votes vote down vote up
def _save_to_db(self,clean_article):
        
        conn = sqlite3.connect(self.db)
        with conn:
            cur = conn.cursor()   
            try:
                cur.execute("INSERT INTO articles (Id,category,title,body)\
                    VALUES(?, ?, ?,?)",(None,clean_article['category'],clean_article['title'],clean_article['body']))
            except sqlite3.IntegrityError:
                self.stats['not_insert_db'] += 1
                print 'Record already inserted with title %s ' %(clean_article['title'].encode("utf-8")) 
Example 23
Project: CAMISIM   Author: CAMI-challenge   File: taxonomy_ncbi.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self, databaseFile, allowedRanks=TAXONOMIC_RANKS, considerNoRank=False):
        """
            @param databaseFile: usually file named "ncbitax_sqlite.db"
            @param allowedRanks: taxonomic ranks that will be considered (where 'root' is the root of the taxonomy)
            @param considerNoRank: consider ranks 'no rank' if true
        """
        self._allowedRanks = set(allowedRanks)
        if considerNoRank:
            self._allowedRanks.add('no rank')
        try:
            self.conn = sqlite3.connect(os.path.normpath(databaseFile))
            self.cursor = self.conn.cursor()
        except Exception:
            sys.stderr.write(str('TaxonomyNcbi: Failed to create connection to database: ' + databaseFile))
            raise 
Example 24
Project: CAMISIM   Author: CAMI-challenge   File: taxonomy.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self, databaseFile, taxonomicRanks):#taxonomicRanks is a list of taxonomic ranks
        self.taxonomicRanks = taxonomicRanks
        self.taxonomicRanksSet = set(taxonomicRanks)
        self.ncbidToPathToRoot = dict([]) #buffer: ncbid -> path to root
        self.newOtuCounter = 0
        try:
            self.conn = sqlite3.connect(os.path.normpath(databaseFile))
            self.cursor = self.conn.cursor()
        except Exception:
            print "Failed to create connection to a database:", databaseFile
            raise 
Example 25
Project: git2net   Author: gotec   File: extraction.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def _process_repo_parallel(git_repo_dir, sqlite_db_file, commits, extraction_settings):
    """ Processes all commits in a given git repository in a parallel manner.

    Args:
        git_repo_dir: path to the git repository that is mined
        sqlite_db_file: path (including database name) where the sqlite database will be created
        commits: list of commits that are already in the database
        extraction_settings: settings for the extraction

    Returns:
        sqlite database will be written at specified location
    """

    args = [{'git_repo_dir': git_repo_dir, 'commit_hash': commit.hash, 'extraction_settings': extraction_settings}
            for commit in commits]

    # suggestion by marco-c (github.com/ishepard/pydriller/issues/110)
    def _init(git_repo_dir, git_init_lock_):
        global git_init_lock
        git_init_lock = git_init_lock_

    con = sqlite3.connect(sqlite_db_file)
    with multiprocessing.Pool(extraction_settings['no_of_processes'],
                              initializer=_init, initargs=(git_repo_dir,git_init_lock)) as p:
        with tqdm(total=len(args), desc='Parallel ({0} processes)' \
                  .format(extraction_settings['no_of_processes'])) as pbar:
            for result in p.imap_unordered(_process_commit, args, chunksize=extraction_settings['chunksize']):
                if not result['edits'].empty:
                    result['edits'].to_sql('edits', con, if_exists='append', index=False)
                if not result['commit'].empty:
                    result['commit'].to_sql('commits', con, if_exists='append', index=False)
                pbar.update(1) 
Example 26
Project: macops   Author: google   File: ApplicationUsage.py    License: Apache License 2.0 5 votes vote down vote up
def _Connect(self, database_name=None):
    """Connect to database.

    Args:
      database_name: str, default APPLICATION_USAGE_DB
    Returns:
      sqlite3.Connection instance
    """
    if database_name is None:
      database_name = APPLICATION_USAGE_DB

    conn = sqlite3.connect(database_name)

    return conn 
Example 27
Project: b1tifi   Author: mh4x0f   File: main.py    License: MIT License 5 votes vote down vote up
def load_database(self, path):
        self.con    = sqlite3.connect(path)
        self.db     = self.con.cursor()
        self.db.execute(C.createTables) 
Example 28
Project: ANGRYsearch   Author: DoTheEvo   File: angrysearch_update_database.py    License: GNU General Public License v2.0 5 votes vote down vote up
def fts5_pragma_check():
    with sqlite3.connect(':memory:') as conn:
        cur = conn.cursor()
        cur.execute('pragma compile_options;')
        available_pragmas = cur.fetchall()

    if ('ENABLE_FTS5', ) in available_pragmas:
        return True
    else:
        return False 
Example 29
Project: ANGRYsearch   Author: DoTheEvo   File: angrysearch.py    License: GNU General Public License v2.0 5 votes vote down vote up
def make_sys_tray(self):
        if Qw.QSystemTrayIcon.isSystemTrayAvailable():
            menu = Qw.QMenu()
            menu.addAction('v1.0.1')
            menu.addSeparator()
            exitAction = menu.addAction('Quit')
            exitAction.triggered.connect(self.close)

            self.tray_icon = Qw.QSystemTrayIcon()
            self.tray_icon.setIcon(self.icon)
            self.tray_icon.setContextMenu(menu)
            self.tray_icon.show()
            self.tray_icon.setToolTip('ANGRYsearch')
            self.tray_icon.activated.connect(self.sys_tray_clicking) 
Example 30
Project: ANGRYsearch   Author: DoTheEvo   File: angrysearch.py    License: GNU General Public License v2.0 5 votes vote down vote up
def wait_for_finishing_typing(self, input_text):
        if not self.setting_params['typing_delay']:
            self.new_query_new_thread(input_text)
            return
        self.last_keyboard_input = input_text
        self.waiting_threads.append(ThreadDelayDBQuery(input_text))
        self.waiting_threads[-1].delay_signal.connect(
            self.waiting_done, Qc.Qt.QueuedConnection)
        self.waiting_threads[-1].start()