本文整理匯總了Python中db.DB.query_from_file方法的典型用法代碼示例。如果您正苦於以下問題:Python DB.query_from_file方法的具體用法?Python DB.query_from_file怎麽用?Python DB.query_from_file使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類db.DB
的用法示例。
在下文中一共展示了DB.query_from_file方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: SQLDB
# 需要導入模塊: from db import DB [as 別名]
# 或者: from db.DB import query_from_file [as 別名]
class SQLDB(Magics):
_db = None
@line_magic('db_connect')
def conn(self, parameter_s):
"""Conenct to database in ipython shell.
Examples::
%db_connect
%db_connect postgresql://user:[email protected]:port/database
"""
uri = urlparse(parameter_s)
if not uri.scheme:
params = {
'dbtype': 'sqlite',
'filename': os.path.join(os.path.expanduser('~'), 'db.sqlite')
}
elif uri.scheme == 'sqlite':
params = {
'dbtype': 'sqlite',
'filename': uri.path
}
else:
params = {
'dbtype': get_or_none(uri.scheme),
'username': get_or_none(uri.username),
'password': get_or_none(uri.password),
'hostname': get_or_none(uri.hostname),
'port': get_or_none(uri.port),
'dbname': get_or_none(uri.path[1:])
}
self._db = DB(**params)
return self._db
@line_magic('db')
def db(self, parameter_s):
return self._db
@line_magic('save_credentials')
@check_db
def save_credentials(self, parameter_s):
if not parameter_s:
print('[ERROR]Please Specify credentials name')
else:
self._db.save_credentials(profile=parameter_s)
print('Save credentials {} successful!'.format(parameter_s[0]))
@line_magic('use_credentials')
@check_db
def use_credentials(self, credential):
self._db = DB(profile=credential)
return self._db
@line_magic('table')
@check_db
def table(self, parameter_s):
p = parameter_s.split()
l = len(p)
if l == 1:
if not p[0]:
return self._db.tables
else:
return attrgetter(p[0])(self._db.tables)
else:
data = self._db.tables
for c in p:
if c in ['head', 'sample', 'unique', 'count', 'all', 'query']:
data = attrgetter(c)(data)()
else:
data = attrgetter(c)(data)
return data
@line_magic('find_column')
@check_db
def find_column(self, parameter_s):
p = parameter_s.split()
if not parameter_s:
print('[ERROR]Please Specify column wildcard')
else:
return self._db.find_column(*p)
@line_magic('find_table')
@check_db
def find_table(self, parameter_s):
if not parameter_s:
print('[ERROR]Please Specify table wildcard')
else:
return self._db.find_table(parameter_s)
@line_magic('query')
@check_db
def query(self, parameter_s):
if not parameter_s:
print('[ERROR]Please Specify sql query')
else:
return self._db.query(parameter_s)
@line_magic('query_from_file')
#.........這裏部分代碼省略.........
示例2: PandaPostgreSQLTest
# 需要導入模塊: from db import DB [as 別名]
# 或者: from db.DB import query_from_file [as 別名]
class PandaPostgreSQLTest(unittest.TestCase):
def setUp(self):
self.db = DB(username='glamp', dbname='Chinook', dbtype='postgres')
def test_query_rowsum(self):
df = self.db.query('select * from "Artist";')
self.assertEqual(len(df), 244)
def test_query_groupby(self):
q = 'select "AlbumId", sum(1) from "Track" group by 1'
df = self.db.query(q)
self.assertEqual(len(df), 297)
def test_query_from_file_rowsum(self):
with open("db/tests/testscript.sql", "w") as f:
f.write('select * from "Artist";')
df = self.db.query_from_file("db/tests/testscript.sql")
self.assertEqual(len(df), 244)
def test_add_profile(self):
profiles = list_profiles()
self.db.save_credentials(profile="test_profile")
self.assertEqual(len(profiles)+1, len(list_profiles()))
remove_profile("test_profile")
def test_remove_profile(self):
profiles = list_profiles()
self.db.save_credentials(profile="test_profile")
self.assertEqual(len(profiles)+1, len(list_profiles()))
remove_profile("test_profile")
def test_list_profiles(self):
self.db.save_credentials(profile="test_profile")
self.assertTrue(len(list_profiles()) > 0)
remove_profile("test_profile")
def test_table_head(self):
self.assertEqual(len(self.db.tables.Artist.head()), 6)
def test_table_all(self):
self.assertEqual(len(self.db.tables.Artist.all()), 244)
def test_table_select(self):
df = self.db.tables.Artist.select("ArtistId", "Name")
self.assertEqual(df.shape, (244, 2))
def test_table_sample(self):
df = self.db.tables.Artist.sample(n=10)
self.assertEqual(len(df), 10)
def test_table_uniqe(self):
df = self.db.tables.Track.unique("GenreId", "MediaTypeId")
self.assertEqual(len(df), 33)
def test_column_head(self):
col = self.db.tables.Track.TrackId.head()
self.assertEqual(len(col), 6)
def test_column_all(self):
col = self.db.tables.Track.TrackId.all()
self.assertEqual(len(col), 2883)
def test_column_sample(self):
col = self.db.tables.Track.TrackId.sample(n=10)
self.assertEqual(len(col), 10)
def test_column_unique(self):
col = self.db.tables.Customer.Country.unique()
self.assertEqual(len(col), 11)
def test_table_count_rows(self):
count = self.db.tables.Invoice.count
self.assertEqual(count, 251)