首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >防止Oracle SQL Developer在导出时截断CLOB

防止Oracle SQL Developer在导出时截断CLOB
EN

Stack Overflow用户
提问于 2015-07-26 10:12:32
回答 2查看 4.5K关注 0票数 8

我想将包含大型CLOB的查询结果导出到CSV文件中。然而,一旦在CSV字段中导出,CLOB将在大约4K个字符之后被截断(即它们将过早地以“…”结束)。如何防止Oracle SQL Developer在导出时截断CLOB?

EN

回答 2

Stack Overflow用户

发布于 2017-09-13 09:23:32

您可以绕过Oracle SQL Developer进行导出,例如,您可以使用Python脚本来处理导出,这样CLOB就不会被截断:

代码语言:javascript
运行
复制
from __future__ import print_function
from __future__ import division

import time
import cx_Oracle

def get_cursor():
    '''
    Get a cursor to the database
    '''
    # http://stackoverflow.com/questions/24149138/cx-oracle-doesnt-connect-when-using-sid-instead-of-service-name-on-connection-s
    # http://www.oracle.com/technetwork/articles/dsl/prez-python-queries-101587.html
    ip = '' # E.g. '127.0.0.1'
    port = '' # e.g. '3306'
    sid = ''
    dsnStr = cx_Oracle.makedsn(ip, port, sid)
    username = '' # E.g. 'FRANCK'
    password = '' # E.g. '123456'
    db = cx_Oracle.connect(user=username, password=password, dsn=dsnStr)    
    cursor = db.cursor()
    return cursor

def read_sql(filename):
    '''
    Read an SQL file and return it as a string
    '''
    file = open(filename, 'r')
    return ' '.join(file.readlines()).replace(';', '')

def execute_sql_file(filename, cursor, verbose = False, display_query = False):
    '''
    Execute an SQL file and return the results
    '''
    sql = read_sql(filename)
    if display_query: print(sql)
    start = time.time()
    if verbose: print('SQL query started... ', end='')
    cursor.execute(sql)
    if verbose: 
        end = time.time()
        print('SQL query done. (took {0} seconds)'.format(end - start))
    return cursor


def main():
    '''
    This is the main function
    '''
    # Demo:
    cursor = oracle_db.get_cursor()
    sql_filename = 'your_query.sql' # Write your query there
    cursor = oracle_db.execute_sql_file(sql_filename, cursor, True)    
    result_filename = 'result.csv'   # Will export your query result there
    result_file = open(result_filename, 'w')
    delimiter = ','    
    for row in cursor:
        for count, column in enumerate(row):
            if count > 0: result_file.write(delimiter)
            result_file.write(str(column))
        result_file.write('\n')
    result_file.close()


if __name__ == "__main__":
    main()
    #cProfile.run('main()') # if you want to do some profiling
票数 2
EN

Stack Overflow用户

发布于 2016-10-14 08:38:45

我使用的是SQL Developer版本4.1.3.20,也有同样的问题。对我来说唯一有效的事情就是选择XML作为导出格式。这样,我就可以导出大约135,000个字符的JSON字符串,而不会被截断。

然而,第二个问题是在导出后立即尝试导入数据,SQL Developer表示由于错误"null“而无法打开文件。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31632983

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档