本文整理匯總了Python中sqlite3.OptimizedUnicode方法的典型用法代碼示例。如果您正苦於以下問題:Python sqlite3.OptimizedUnicode方法的具體用法?Python sqlite3.OptimizedUnicode怎麽用?Python sqlite3.OptimizedUnicode使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類sqlite3
的用法示例。
在下文中一共展示了sqlite3.OptimizedUnicode方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: sql_query
# 需要導入模塊: import sqlite3 [as 別名]
# 或者: from sqlite3 import OptimizedUnicode [as 別名]
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
示例2: CheckOptimizedUnicode
# 需要導入模塊: import sqlite3 [as 別名]
# 或者: from sqlite3 import OptimizedUnicode [as 別名]
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")
示例3: CheckOptimizedUnicodeAsString
# 需要導入模塊: import sqlite3 [as 別名]
# 或者: from sqlite3 import OptimizedUnicode [as 別名]
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")
示例4: CheckOptimizedUnicodeAsUnicode
# 需要導入模塊: import sqlite3 [as 別名]
# 或者: from sqlite3 import OptimizedUnicode [as 別名]
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�")
示例5: CheckNonUtf8_TextFactoryOptimizedUnicode
# 需要導入模塊: import sqlite3 [as 別名]
# 或者: from sqlite3 import OptimizedUnicode [as 別名]
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
示例6: CheckOptimizedUnicode
# 需要導入模塊: import sqlite3 [as 別名]
# 或者: from sqlite3 import OptimizedUnicode [as 別名]
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")
示例7: CheckOptimizedUnicode
# 需要導入模塊: import sqlite3 [as 別名]
# 或者: from sqlite3 import OptimizedUnicode [as 別名]
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")
示例8: in_demo
# 需要導入模塊: import sqlite3 [as 別名]
# 或者: from sqlite3 import OptimizedUnicode [as 別名]
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)
############################################
示例9: in_demo
# 需要導入模塊: import sqlite3 [as 別名]
# 或者: from sqlite3 import OptimizedUnicode [as 別名]
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)
############################################
示例10: assemble_actions_db
# 需要導入模塊: import sqlite3 [as 別名]
# 或者: from sqlite3 import OptimizedUnicode [as 別名]
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()