當前位置: 首頁>>代碼示例>>Python>>正文


Python sql.DatabaseError方法代碼示例

本文整理匯總了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() 
開發者ID:MattKleinsmith,項目名稱:pbt,代碼行數:25,代碼來源:utils.py

示例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 
開發者ID:MattKleinsmith,項目名稱:pbt,代碼行數:19,代碼來源:utils.py

示例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) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:8,代碼來源:test_sql.py


注:本文中的pandas.io.sql.DatabaseError方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。