sql - 带有 ODBC SQL 查询的 Julia ReadOnlyMemoryError

标签 sql function odbc julia

我正在编写一个查询 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 调用中绑定(bind)值。

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

关于sql - 带有 ODBC SQL 查询的 Julia ReadOnlyMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64755530/

相关文章:

r - 使用 "..."R 拆分函数的数组参数

javascript - $().live 不是函数 - JavaScript/jQuery

c++ - int * & 有实际意义吗?

java - 创建与 .accdb 访问文件的连接

sql-server - 通过 ODBC 的 Microsoft SQL Server 统计

mysql - 对枚举进行排序实际上是如何工作的?

php - 如何在 php 和 mysql 中计算带有 unix 时间戳的记录月份组。

sql-server - 测试 SQL Server 连接

mysql - 获取优惠的具体日期

mysql - 两个时间戳之间的差异