我们从Python开源项目中,提取了以下45个代码示例,用于说明如何使用sqlite3.OptimizedUnicode()。
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
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")
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")
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")
def CheckOptimizedUnicode(self): 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.assertTrue(type(a_row[0]) == str, "type of non-ASCII row must be str") self.assertTrue(type(d_row[0]) == str, "type of ASCII-only row must be str")
def __init__(self, filepath=None): if filepath==None: filepath = os.path.join(config.database_path, "SKIRT-runs.db") self._con = sqlite3.connect(filepath) self._con.row_factory = sqlite3.Row self._con.text_factory = sqlite3.OptimizedUnicode ## This function closes the connection to the database. Any uncommited changes are lost. # You can no longer use the database after calling this function.
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")
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")
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) ############################################