本文整理汇总了Python中pandas.io.sql.DatabaseError方法的典型用法代码示例。如果您正苦于以下问题:Python sql.DatabaseError方法的具体用法?Python sql.DatabaseError怎么用?Python sql.DatabaseError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pandas.io.sql
的用法示例。
在下文中一共展示了sql.DatabaseError方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create_table
# 需要导入模块: from pandas.io import sql [as 别名]
# 或者: from pandas.io.sql import DatabaseError [as 别名]
def create_table(connect_str_or_path, use_sqlite, command):
if use_sqlite:
sqlite_path = connect_str_or_path
conn = sqlite3.connect(sqlite_path)
cur = conn.cursor()
cur.execute(command)
conn.commit()
cur.close()
else:
conn = None
try:
db_connect_str = connect_str_or_path
conn = psycopg2.connect(db_connect_str)
cur = conn.cursor()
cur.execute(command)
conn.commit()
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
if "already exists" not in str(error):
print(error)
finally:
if conn is not None:
conn.close()
示例2: load_sqlite_table
# 需要导入模块: from pandas.io import sql [as 别名]
# 或者: from pandas.io.sql import DatabaseError [as 别名]
def load_sqlite_table(database_path, table_name):
"""Returns (table, connection). table is a pandas DataFrame."""
conn = sqlite3.connect(database_path)
try:
df = pd.read_sql("SELECT * FROM %s" % table_name, conn)
# print("\nLoading %s table from SQLite3 database." % table_name)
except DatabaseError as e:
if 'no such table' in e.args[0]:
print("\nNo such table: %s" % table_name)
print("Create the table before loading it. " +
"Consider using the create_sqlite_table function")
raise DatabaseError
else:
print(e)
raise Exception("Failed to create %s table. Unknown error." %
table_name)
return df, conn
示例3: test_read_sql_delegate
# 需要导入模块: from pandas.io import sql [as 别名]
# 或者: from pandas.io.sql import DatabaseError [as 别名]
def test_read_sql_delegate(self):
iris_frame1 = sql.read_sql_query("SELECT * FROM iris", self.conn)
iris_frame2 = sql.read_sql("SELECT * FROM iris", self.conn)
tm.assert_frame_equal(iris_frame1, iris_frame2)
pytest.raises(sql.DatabaseError, sql.read_sql, 'iris', self.conn)