从AppleScript for sqlite3

时间:2016-10-07 13:25:25

标签: sqlite applescript

我想使用AppleScript从Mac Mail中的电子邮件中收集一些数据,然后使用“do shell script”将此数据写入sqlite3数据库。问题是,我想要编写的内容之一是来自消息体的数据,它可以包含所有特殊字符,这些特殊字符可能会阻塞将数据传递给sqlite3的过程。

转发此数据的最佳方法是什么?或者,有没有比在AppleScript中使用“do shell script”更好的方法呢?

1 个答案:

答案 0 :(得分:0)

如果没有示例有问题的数据我假设引用是唯一的问题,因为完全二进制安全需要更多代码。

on sqlite_quote(subject)
    set OLD_TID to AppleScript's text item delimiters
    try
        set AppleScript's text item delimiters to "\\'"
        set subject to subject's quoted form's text items
        set AppleScript's text item delimiters to ""
        set subject to subject as string

        set AppleScript's text item delimiters to OLD_TID
    on error error_message number error_number from error_source partial result error_result to error_class
        set AppleScript's text item delimiters to OLD_TID

        error error_message number error_number from error_source partial result error_result to error_class
    end try

    subject
end sqlite_quote
on sqlite_quotes(subjects)
    set subjects to {} & subjects
    repeat with subject in subjects
        set subject's contents to sqlite_quote(subject)
    end repeat
    subjects
end sqlite_quotes
on sqlite_fields_quote(sources)
    my list2string(",", sqlite_quotes(sources))
end sqlite_fields_quote

on sqlite_stmnt_create_table_ifnexists(table, columns)
    "create table if not exists " & sqlite_quote(table) & "(" & sqlite_fields_quote(columns) & ")"
end sqlite_stmnt_create_table_ifnexists
on sqlite_stmnt_drop_table_ifexists(table)
    "drop table if exists " & sqlite_quote(table)
end sqlite_stmnt_drop_table_ifexists
on sqlite_stmnt_insert_into(table, values)
    "insert into " & sqlite_quote(table) & " values(" & sqlite_fields_quote(values) & ")"
end sqlite_stmnt_insert_into


on list2string(delimiter, source) -- general utility
    try
        set {|applescript's text item delimiters|, AppleScript's text item delimiters} to {AppleScript's text item delimiters, delimiter}
        set |result| to source as string
        set AppleScript's text item delimiters to |applescript's text item delimiters|
    on error error_message number error_number from error_source partial result error_result to error_class
        set AppleScript's text item delimiters to |applescript's text item delimiters|
        error error_message number error_number from error_source partial result error_result to error_class
    end try
    |result|
end list2string


property executable : "sqlite3"
property db_path : "/tmp/test.sqlite"

on run argv
    set {table, values} to {"table", {"example with CRLF: " & («data TEXT0D0A» as text) & " using applescript's TIDs on this string's quoted form"}} -- example

    set {stmnts, test_ins, test_drp, debug} to {{}, true, false, false}

    if test_drp then set stmnts's end to sqlite_stmnt_drop_table_ifexists(table)
    if test_ins then set stmnts's end to sqlite_stmnt_create_table_ifnexists(table, "text")

    set stmnts's end to sqlite_stmnt_insert_into(table, values)

    if test_ins then set stmnts's end to "select * from " & sqlite_quote(table)

    set |script| to executable & space & db_path's quoted form & space & my list2string(";", stmnts)'s quoted form

    if debug then set |script| to "echo >&2 " & |script|'s quoted form & ";" & |script| & "|xxd"

    do shell script |script|
end run