Python sqlite3.OptimizedUnicode() Examples

The following are 30 code examples of sqlite3.OptimizedUnicode(). 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 also want to check out all available functions/classes of the module sqlite3 , or try the search function .
Example #1
Source File: chat80.py    From luscan-devel with GNU General Public License v2.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
    """
    try:
        import sqlite3
        path = find(dbname)
        connection =  sqlite3.connect(path)
        # return ASCII strings if possible
        connection.text_factory = sqlite3.OptimizedUnicode
        cur = connection.cursor()
        return cur.execute(query)
    except ImportError:
        import warnings
        warnings.warn("To run this function, first install pysqlite, or else use Python 2.5 or later.")
        raise
    except ValueError:
        import warnings
        warnings.warn("Make sure the database file %s is installed and uncompressed." % dbname)
        raise 
Example #2
Source File: factory.py    From oss-ftp with MIT License 5 votes vote down vote up
def CheckOptimizedUnicodeAsUnicode(self):
        # Non-ASCII -> unicode argument
        self.con.text_factory = sqlite.OptimizedUnicode
        self.con.execute("delete from test")
        self.con.execute("insert into test (value) values (?)", (u'�\0�',))
        row = self.con.execute("select value from test").fetchone()
        self.assertIs(type(row[0]), unicode)
        self.assertEqual(row[0], u"�\x00�") 
Example #3
Source File: factory.py    From android_universal with MIT License 5 votes vote down vote up
def CheckOptimizedUnicode(self):
        # In py3k, str objects are always returned when text_factory
        # is OptimizedUnicode
        self.con.text_factory = sqlite.OptimizedUnicode
        austria = "�sterreich"
        germany = "Deutchland"
        a_row = self.con.execute("select ?", (austria,)).fetchone()
        d_row = self.con.execute("select ?", (germany,)).fetchone()
        self.assertEqual(type(a_row[0]), str, "type of non-ASCII row must be str")
        self.assertEqual(type(d_row[0]), str, "type of ASCII-only row must be str") 
Example #4
Source File: factory.py    From odoo13-x64 with GNU General Public License v3.0 5 votes vote down vote up
def CheckOptimizedUnicode(self):
        # In py3k, str objects are always returned when text_factory
        # is OptimizedUnicode
        self.con.text_factory = sqlite.OptimizedUnicode
        austria = "�sterreich"
        germany = "Deutchland"
        a_row = self.con.execute("select ?", (austria,)).fetchone()
        d_row = self.con.execute("select ?", (germany,)).fetchone()
        self.assertEqual(type(a_row[0]), str, "type of non-ASCII row must be str")
        self.assertEqual(type(d_row[0]), str, "type of ASCII-only row must be str") 
Example #5
Source File: factory.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def CheckOptimizedUnicode(self):
        # In py3k, str objects are always returned when text_factory
        # is OptimizedUnicode
        self.con.text_factory = sqlite.OptimizedUnicode
        austria = "�sterreich"
        germany = "Deutchland"
        a_row = self.con.execute("select ?", (austria,)).fetchone()
        d_row = self.con.execute("select ?", (germany,)).fetchone()
        self.assertEqual(type(a_row[0]), str, "type of non-ASCII row must be str")
        self.assertEqual(type(d_row[0]), str, "type of ASCII-only row must be str") 
Example #6
Source File: types.py    From datafari with Apache License 2.0 5 votes vote down vote up
def CheckNonUtf8_TextFactoryOptimizedUnicode(self):
        orig_text_factory = self.con.text_factory
        try:
            try:
                self.con.text_factory = sqlite.OptimizedUnicode
                self.cur.execute("select ?", (chr(150),))
                self.fail("should have raised a ProgrammingError")
            except sqlite.ProgrammingError:
                pass
        finally:
            self.con.text_factory = orig_text_factory 
Example #7
Source File: factory.py    From datafari with Apache License 2.0 5 votes vote down vote up
def CheckOptimizedUnicodeAsUnicode(self):
        # Non-ASCII -> unicode argument
        self.con.text_factory = sqlite.OptimizedUnicode
        self.con.execute("delete from test")
        self.con.execute("insert into test (value) values (?)", (u'�\0�',))
        row = self.con.execute("select value from test").fetchone()
        self.assertIs(type(row[0]), unicode)
        self.assertEqual(row[0], u"�\x00�") 
Example #8
Source File: factory.py    From datafari with Apache License 2.0 5 votes vote down vote up
def CheckOptimizedUnicodeAsString(self):
        # ASCII -> str argument
        self.con.text_factory = sqlite.OptimizedUnicode
        row = self.con.execute("select value from test").fetchone()
        self.assertIs(type(row[0]), str)
        self.assertEqual(row[0], "a\x00b") 
Example #9
Source File: factory.py    From datafari with Apache License 2.0 5 votes vote down vote up
def CheckOptimizedUnicode(self):
        self.con.text_factory = sqlite.OptimizedUnicode
        austria = unicode("�sterreich", "latin1")
        germany = unicode("Deutchland")
        a_row = self.con.execute("select ?", (austria,)).fetchone()
        d_row = self.con.execute("select ?", (germany,)).fetchone()
        self.assertEqual(type(a_row[0]), unicode, "type of non-ASCII row must be unicode")
        self.assertEqual(type(d_row[0]), str, "type of ASCII-only row must be str") 
Example #10
Source File: factory.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def CheckOptimizedUnicode(self):
        # In py3k, str objects are always returned when text_factory
        # is OptimizedUnicode
        self.con.text_factory = sqlite.OptimizedUnicode
        austria = "�sterreich"
        germany = "Deutchland"
        a_row = self.con.execute("select ?", (austria,)).fetchone()
        d_row = self.con.execute("select ?", (germany,)).fetchone()
        self.assertEqual(type(a_row[0]), str, "type of non-ASCII row must be str")
        self.assertEqual(type(d_row[0]), str, "type of ASCII-only row must be str") 
Example #11
Source File: factory.py    From Imogen with MIT License 5 votes vote down vote up
def CheckOptimizedUnicode(self):
        # In py3k, str objects are always returned when text_factory
        # is OptimizedUnicode
        self.con.text_factory = sqlite.OptimizedUnicode
        austria = "�sterreich"
        germany = "Deutchland"
        a_row = self.con.execute("select ?", (austria,)).fetchone()
        d_row = self.con.execute("select ?", (germany,)).fetchone()
        self.assertEqual(type(a_row[0]), str, "type of non-ASCII row must be str")
        self.assertEqual(type(d_row[0]), str, "type of ASCII-only row must be str") 
Example #12
Source File: factory.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def CheckOptimizedUnicode(self):
        # In py3k, str objects are always returned when text_factory
        # is OptimizedUnicode
        self.con.text_factory = sqlite.OptimizedUnicode
        austria = "�sterreich"
        germany = "Deutchland"
        a_row = self.con.execute("select ?", (austria,)).fetchone()
        d_row = self.con.execute("select ?", (germany,)).fetchone()
        self.assertEqual(type(a_row[0]), str, "type of non-ASCII row must be str")
        self.assertEqual(type(d_row[0]), str, "type of ASCII-only row must be str") 
Example #13
Source File: types.py    From oss-ftp with MIT License 5 votes vote down vote up
def CheckNonUtf8_TextFactoryOptimizedUnicode(self):
        orig_text_factory = self.con.text_factory
        try:
            try:
                self.con.text_factory = sqlite.OptimizedUnicode
                self.cur.execute("select ?", (chr(150),))
                self.fail("should have raised a ProgrammingError")
            except sqlite.ProgrammingError:
                pass
        finally:
            self.con.text_factory = orig_text_factory 
Example #14
Source File: factory.py    From oss-ftp with MIT License 5 votes vote down vote up
def CheckOptimizedUnicodeAsString(self):
        # ASCII -> str argument
        self.con.text_factory = sqlite.OptimizedUnicode
        row = self.con.execute("select value from test").fetchone()
        self.assertIs(type(row[0]), str)
        self.assertEqual(row[0], "a\x00b") 
Example #15
Source File: factory.py    From oss-ftp with MIT License 5 votes vote down vote up
def CheckOptimizedUnicode(self):
        self.con.text_factory = sqlite.OptimizedUnicode
        austria = unicode("�sterreich", "latin1")
        germany = unicode("Deutchland")
        a_row = self.con.execute("select ?", (austria,)).fetchone()
        d_row = self.con.execute("select ?", (germany,)).fetchone()
        self.assertEqual(type(a_row[0]), unicode, "type of non-ASCII row must be unicode")
        self.assertEqual(type(d_row[0]), str, "type of ASCII-only row must be str") 
Example #16
Source File: types.py    From BinderFilter with MIT License 5 votes vote down vote up
def CheckNonUtf8_TextFactoryOptimizedUnicode(self):
        orig_text_factory = self.con.text_factory
        try:
            try:
                self.con.text_factory = sqlite.OptimizedUnicode
                self.cur.execute("select ?", (chr(150),))
                self.fail("should have raised a ProgrammingError")
            except sqlite.ProgrammingError:
                pass
        finally:
            self.con.text_factory = orig_text_factory 
Example #17
Source File: factory.py    From BinderFilter with MIT License 5 votes vote down vote up
def CheckOptimizedUnicodeAsUnicode(self):
        # Non-ASCII -> unicode argument
        self.con.text_factory = sqlite.OptimizedUnicode
        self.con.execute("delete from test")
        self.con.execute("insert into test (value) values (?)", (u'�\0�',))
        row = self.con.execute("select value from test").fetchone()
        self.assertIs(type(row[0]), unicode)
        self.assertEqual(row[0], u"�\x00�") 
Example #18
Source File: factory.py    From BinderFilter with MIT License 5 votes vote down vote up
def CheckOptimizedUnicodeAsString(self):
        # ASCII -> str argument
        self.con.text_factory = sqlite.OptimizedUnicode
        row = self.con.execute("select value from test").fetchone()
        self.assertIs(type(row[0]), str)
        self.assertEqual(row[0], "a\x00b") 
Example #19
Source File: factory.py    From BinderFilter with MIT License 5 votes vote down vote up
def CheckOptimizedUnicode(self):
        self.con.text_factory = sqlite.OptimizedUnicode
        austria = unicode("�sterreich", "latin1")
        germany = unicode("Deutchland")
        a_row = self.con.execute("select ?", (austria,)).fetchone()
        d_row = self.con.execute("select ?", (germany,)).fetchone()
        self.assertTrue(type(a_row[0]) == unicode, "type of non-ASCII row must be unicode")
        self.assertTrue(type(d_row[0]) == str, "type of ASCII-only row must be str") 
Example #20
Source File: types.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def CheckNonUtf8_TextFactoryOptimizedUnicode(self):
        orig_text_factory = self.con.text_factory
        try:
            try:
                self.con.text_factory = sqlite.OptimizedUnicode
                self.cur.execute("select ?", (chr(150),))
                self.fail("should have raised a ProgrammingError")
            except sqlite.ProgrammingError:
                pass
        finally:
            self.con.text_factory = orig_text_factory 
Example #21
Source File: factory.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def CheckOptimizedUnicodeAsUnicode(self):
        # Non-ASCII -> unicode argument
        self.con.text_factory = sqlite.OptimizedUnicode
        self.con.execute("delete from test")
        self.con.execute("insert into test (value) values (?)", (u'�\0�',))
        row = self.con.execute("select value from test").fetchone()
        self.assertIs(type(row[0]), unicode)
        self.assertEqual(row[0], u"�\x00�") 
Example #22
Source File: factory.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def CheckOptimizedUnicodeAsString(self):
        # ASCII -> str argument
        self.con.text_factory = sqlite.OptimizedUnicode
        row = self.con.execute("select value from test").fetchone()
        self.assertIs(type(row[0]), str)
        self.assertEqual(row[0], "a\x00b") 
Example #23
Source File: factory.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def CheckOptimizedUnicode(self):
        self.con.text_factory = sqlite.OptimizedUnicode
        austria = unicode("�sterreich", "latin1")
        germany = unicode("Deutchland")
        a_row = self.con.execute("select ?", (austria,)).fetchone()
        d_row = self.con.execute("select ?", (germany,)).fetchone()
        self.assertEqual(type(a_row[0]), unicode, "type of non-ASCII row must be unicode")
        self.assertEqual(type(d_row[0]), str, "type of ASCII-only row must be str") 
Example #24
Source File: types.py    From vsphere-storage-for-docker with Apache License 2.0 5 votes vote down vote up
def CheckNonUtf8_TextFactoryOptimizedUnicode(self):
        orig_text_factory = self.con.text_factory
        try:
            try:
                self.con.text_factory = sqlite.OptimizedUnicode
                self.cur.execute("select ?", (chr(150),))
                self.fail("should have raised a ProgrammingError")
            except sqlite.ProgrammingError:
                pass
        finally:
            self.con.text_factory = orig_text_factory 
Example #25
Source File: factory.py    From vsphere-storage-for-docker with Apache License 2.0 5 votes vote down vote up
def CheckOptimizedUnicodeAsUnicode(self):
        # Non-ASCII -> unicode argument
        self.con.text_factory = sqlite.OptimizedUnicode
        self.con.execute("delete from test")
        self.con.execute("insert into test (value) values (?)", (u'�\0�',))
        row = self.con.execute("select value from test").fetchone()
        self.assertIs(type(row[0]), unicode)
        self.assertEqual(row[0], u"�\x00�") 
Example #26
Source File: factory.py    From vsphere-storage-for-docker with Apache License 2.0 5 votes vote down vote up
def CheckOptimizedUnicodeAsString(self):
        # ASCII -> str argument
        self.con.text_factory = sqlite.OptimizedUnicode
        row = self.con.execute("select value from test").fetchone()
        self.assertIs(type(row[0]), str)
        self.assertEqual(row[0], "a\x00b") 
Example #27
Source File: factory.py    From vsphere-storage-for-docker with Apache License 2.0 5 votes vote down vote up
def CheckOptimizedUnicode(self):
        self.con.text_factory = sqlite.OptimizedUnicode
        austria = unicode("�sterreich", "latin1")
        germany = unicode("Deutchland")
        a_row = self.con.execute("select ?", (austria,)).fetchone()
        d_row = self.con.execute("select ?", (germany,)).fetchone()
        self.assertEqual(type(a_row[0]), unicode, "type of non-ASCII row must be unicode")
        self.assertEqual(type(d_row[0]), str, "type of ASCII-only row must be str") 
Example #28
Source File: relextract.py    From razzy-spinner with GNU General Public License v3.0 4 votes vote down vote up
def in_demo(trace=0, sql=True):
    """
    Select pairs of organizations and locations whose mentions occur with an
    intervening occurrence of the preposition "in".

    If the sql parameter is set to True, then the entity pairs are loaded into
    an in-memory database, and subsequently pulled out using an SQL "SELECT"
    query.
    """
    from nltk.corpus import ieer
    if sql:
        try:
            import sqlite3
            connection =  sqlite3.connect(":memory:")
            connection.text_factory = sqlite3.OptimizedUnicode
            cur = connection.cursor()
            cur.execute("""create table Locations
            (OrgName text, LocationName text, DocID text)""")
        except ImportError:
            import warnings
            warnings.warn("Cannot import sqlite; sql flag will be ignored.")


    IN = re.compile(r'.*\bin\b(?!\b.+ing)')

    print()
    print("IEER: in(ORG, LOC) -- just the clauses:")
    print("=" * 45)

    for file in ieer.fileids():
        for doc in ieer.parsed_docs(file):
            if trace:
                print(doc.docno)
                print("=" * 15)
            for rel in extract_rels('ORG', 'LOC', doc, corpus='ieer', pattern=IN):
                print(clause(rel, relsym='IN'))
                if sql:
                    try:
                        rtuple = (rel['subjtext'], rel['objtext'], doc.docno)
                        cur.execute("""insert into Locations
                                    values (?, ?, ?)""", rtuple)
                        connection.commit()
                    except NameError:
                        pass

    if sql:
        try:
            cur.execute("""select OrgName from Locations
                        where LocationName = 'Atlanta'""")
            print()
            print("Extract data from SQL table: ORGs in Atlanta")
            print("-" * 15)
            for row in cur:
                print(row)
        except NameError:
            pass


############################################
# Example of has_role(PER, LOC)
############################################ 
Example #29
Source File: relextract.py    From luscan-devel with GNU General Public License v2.0 4 votes vote down vote up
def in_demo(trace=0, sql=True):
    """
    Select pairs of organizations and locations whose mentions occur with an
    intervening occurrence of the preposition "in".

    If the sql parameter is set to True, then the entity pairs are loaded into
    an in-memory database, and subsequently pulled out using an SQL "SELECT"
    query.
    """
    from nltk.corpus import ieer
    if sql:
        try:
            import sqlite3
            connection =  sqlite3.connect(":memory:")
            connection.text_factory = sqlite3.OptimizedUnicode
            cur = connection.cursor()
            cur.execute("""create table Locations
            (OrgName text, LocationName text, DocID text)""")
        except ImportError:
            import warnings
            warnings.warn("Cannot import sqlite; sql flag will be ignored.")


    IN = re.compile(r'.*\bin\b(?!\b.+ing)')

    print
    print "IEER: in(ORG, LOC) -- just the clauses:"
    print "=" * 45

    for file in ieer.fileids():
        for doc in ieer.parsed_docs(file):
            if trace:
                print doc.docno
                print "=" * 15
            for rel in extract_rels('ORG', 'LOC', doc, corpus='ieer', pattern=IN):
                print show_clause(rel, relsym='IN')
                if sql:
                    try:
                        rtuple = (rel['subjtext'], rel['objtext'], doc.docno)
                        cur.execute("""insert into Locations
                                    values (?, ?, ?)""", rtuple)
                        connection.commit()
                    except NameError:
                        pass

    if sql:
        try:
            cur.execute("""select OrgName from Locations
                        where LocationName = 'Atlanta'""")
            print
            print "Extract data from SQL table: ORGs in Atlanta"
            print "-" * 15
            for row in cur:
                print row
        except NameError:
            pass


############################################
# Example of has_role(PER, LOC)
############################################ 
Example #30
Source File: actions_db.py    From Melissa-Core with MIT License 4 votes vote down vote up
def assemble_actions_db():
    global con, cur, modules
    try:
        if profile.data['actions_db_file'] != ':memory:' \
                and os.path.exists(profile.data['actions_db_file']):
            os.remove(profile.data['actions_db_file'])

        con = sqlite3.connect(
            profile.data['actions_db_file'],
            check_same_thread=False)
        con.text_factory = sqlite3.OptimizedUnicode
        cur = con.cursor()

    except sqlite3.Error as e:
        print "Error %s:" % e.args[0]
        sys.exit(1)

    if create_actions_db(con, cur):
        print 'Successfully Created ' + profile.data['actions_db_file']

    package = importlib.import_module(profile.data['modules'])
    for finder, name, _ in pkgutil.walk_packages(package.__path__):
        print 'Loading module ' + name
        try:
            loader = finder.find_module(name)
            mod = loader.load_module(name)

        except:
            print "Skipped module '%s' due to an error." % name

        else:
            priority = mod.PRIORITY if hasattr(mod, 'PRIORITY') else 0

            if hasattr(mod, 'WORDS'):
                insert_words(con, cur, name, mod.WORDS, priority)
                modules[name] = mod

            else:
                print 'WARNING: Module will not be used.'
                print '    WORDS not found for module ' + name

    # con.close()