从automator运行python脚本

时间:2014-10-06 13:20:58

标签: python sqlite shell automator

我正在尝试从Automator创建一个“.app”来运行一个简单的python脚本。当我在Automator中执行脚本时,会出现更多或更少的错误“检查您的操作属性并再次执行”。

在历史上它说“Traceback(最近的呼叫最后:)”。关键是这个脚本在终端会话中运行良好。

我的循环“While”似乎至少是一个错误,因为我可以在这个阶段执行脚本,因为我可以执行脚本直到这个阶段。(管理sqlite数据库有问题吗?)但我无法理解,因为终端没有问题。什么遗失了?

我的python脚本:

#!/usr/bin/python

import sqlite3
import os.path

file_name = "newDB.data"
choice = ""

if os.path.isfile(file_name):
    choice = raw_input("Erase DB? press [y] or [n]:\n")

if choice == "y":
    print "erase"

    while True: 
        try:
            os.remove(file_name)
            break

    except OSError as e: # name the Exception `e`
        print "Failed with:", e.strerror # look what it says
        print "Error code:", e.code 

if choice == "n":
    print "Bye!"
    exit() 


# start sqlite connection
conn = sqlite3.connect("newDB.data")
c = conn.cursor()


# attach
c.execute("ATTACH database 'store1.data' AS db1")
c.execute("ATTACH database 'store2.data' AS db2")


# rename tables
while True:
    try:
        c.execute("ALTER TABLE db1.ZPATIENT RENAME TO table1")
        print "table 1 renamed"
        break

    except:
        c.execute("ALTER TABLE db1.table1 RENAME TO ZPATIENT")
        print "except 1"


while True:
    try:
        c.execute("ALTER TABLE db2.ZPATIENT RENAME TO table2")
        print "table 2 renamed"
        break

    except:
        c.execute("ALTER TABLE db2.table2 RENAME TO ZPATIENT")
        print "except 2"


# some information commands (START):
c.execute("SELECT * from table1")
print(c.fetchall())
c.execute("SELECT * from table2")
print(c.fetchall())
# some information commands (END)


#c.execute("create table ZPATIENT as select * from table1 union select * from table2") ---> first union action but some entries duplicated (one column changed?)

# remove some duplicated entries...
c.execute("create table ZPATIENT as select * from (select * from table1 union select * from table2) final group by ZDATECREATED")
c.execute("CREATE TABLE Z_PRIMARYKEY (Z_ENT int, Z_NAME text, Z_SUPER int, Z_MAX int)")
c.execute("CREATE TABLE Z_METADATA (Z_VERSION int, Z_UUID text, Z_PLIST BLOB)")

c.execute("SELECT count(*) FROM ZPATIENT")
result=c.fetchone()
number_of_rows=result[0]
print number_of_rows
start = 0
end = number_of_rows + 1

c.execute('SELECT * FROM ZPATIENT') 
newresult=c.fetchall()

for row in newresult:

    start += 1
    end -= 1
    print start
    print end

    # some information commands (START):
    list_of_tuple = list(row)
    list_of_tuple[0] = start
    list_of_tuple[2] = end
    row = tuple(list_of_tuple)
    print row
    # some information commands (END)

    c.execute("UPDATE ZPATIENT SET Z_PK = ? WHERE rowid = ?", (start, start))
    c.execute("UPDATE ZPATIENT SET Z_OPT = ? WHERE rowid = ?", (end, start))


c.execute("INSERT INTO Z_PRIMARYKEY (Z_ENT, Z_NAME, Z_SUPER, Z_MAX) VALUES (0, 'Patient', 0, ?)", (start,))

# close
conn.commit()
conn.close()

为了工作我在同一个文件夹中有两个名为store1.data和store2.data的sqlite数据库......

如果有人有解决方案......我不知道是否可以通过一次点击更轻松地执行此操作?

1 个答案:

答案 0 :(得分:1)

一个简单的解决方案可能是避免使用automator,只需创建一个bash脚本来调用python脚本。你可以通过双击它来执行bash脚本,如果这是你想要的。

#! /bin/bash

python scriptname.py

就是你所需要的一切。

相关问题