带有 ODBC SQL 查询的 Julia ReadOnlyMemoryError

Julia ReadOnlyMemoryError with ODBC SQL query

我正在编写一个查询 SQL 数据库的函数,但我遇到了一个让我难过的 ReadOnlyMemoryError()。问题是,当我 运行 我的代码作为一个简单的脚本时,一切都按预期运行。但是当我尝试将完全相同的代码包装在一个函数中时,我得到了 ReadOnlyMemoryError()。

这是我的代码的脚本版本:

using ODBC
using DBInterface
using Dates
using DataFrames

server = "server string "
username = "username "
password = " password"
db = " db name"

start_date=Nothing
end_date=Nothing

if start_date == Nothing || typeof(start_date) != "Date"
    start_date = Dates.today() - Dates.Day(30)
end

if end_date == Nothing || typeof(end_date) != "Date"
    end_date = Dates.today()
end

query = """ SQL SELECT statement """

connect_string = "DRIVER={ODBC Driver 17 for SQL Server};SERVER=" * server *
                ";DATABASE=" * db *
                ";UID=" * username *
                ";PWD=" * password
conn = ODBC.Connection(connect_string)

df = DBInterface.execute(conn, query) |> DataFrame

这按预期工作,结果是一个包含大约 50 万行的数据帧 df。但是,当我尝试使用相同的代码制作可重用函数时,出现错误:

using ODBC
using DBInterface
using Dates
using DataFrames

function get_cf_data(start_date=Nothing, end_date=Nothing)
    server = " server string "
    username = " user name"
    password = " password"
    db = " db "

    if start_date == Nothing || typeof(start_date) != "Date"
        start_date = Dates.today() - Dates.Day(30)
    end

    if end_date == Nothing || typeof(end_date) != "Date"
        end_date = Dates.today()
    end

    query = """  SQL SELECT statement """

    connect_string = "DRIVER={ODBC Driver 17 for SQL Server};SERVER=" * server *
                    ";DATABASE=" * db *
                    ";UID=" * username *
                    ";PWD=" * password
    conn = ODBC.Connection(connect_string)

    df = DBInterface.execute(conn, query) |> DataFrame

    return df
end

在这种情况下,当我尝试从 REPL get_cf_data() 调用时,出现错误:ReadOnlyMemoryError()。我对 Julia 有点陌生,所以任何见解都将不胜感激。谢谢!

如评论所述,大多数编程语言在集成 ODBC 连接等 API 时的最佳做法是在使用后关闭并释放其资源。

此外,考虑 parameterization(任何传递文字值的语言 运行 SQL 的最佳实践),您可以在其中设置准备好的 SQL 语句并绑定值随后的 execute 调用。

function get_cf_data(start_date=Nothing, end_date=Nothing)
    server = " server string "
    username = " user name"
    password = " password"
    db = " db "

    if isnothing(start_date) || typeof(start_date) != "Date"
        start_date = Dates.today() - Dates.Day(30)
    end

    if isnothing(end_date) || typeof(end_date) != "Date"
        end_date = Dates.today()
    end

    # PREPARED STATEMENT WITH QMARK PLACEHOLDERS
    sql = """SELECT Col1, Col2, Col3, ...
             FROM myTable
             WHERE myDate BETWEEN ? AND ?
          """

    connect_string = "DRIVER={ODBC Driver 17 for SQL Server};SERVER=" * server *
                    ";DATABASE=" * db *
                    ";UID=" * username *
                    ";PWD=" * password
    conn = ODBC.Connection(connect_string)

    # PREPARE STATEMENT AND BIND PARAMS
    stmt = DBInterface.prepare(conn, sql)
    df = DBInterface.execute(stmt, (start_date, end_date)) |> DataFrame

    DBInterface.close(stmt)                   # CLOSE STATEMENT
    DBInterface.close(conn)                   # CLOSE CONNECTION
    stmt = Nothing; conn = Nothing            # UNINTIALIZE OBJECTS

    return df
end