2012-04-06 62 views
0

而不是創建,然後在sqlite3中刪除一個表,我想創建一個臨時表,當您結束會話時會破壞它。是否可以合併下面的命令,以便臨時表不被分號破壞?結合多個shell命令在sqlite3中創建臨時表

set databaseName to "test.db" 
set tableName to "tempTable" 
do shell script ("mkdir -p ~/Documents/Databases/ ; sqlite3 ~/Documents/Databases/" & databaseName & " \"create table if not exists " & tableName & "(First, Last); \"") 
do shell script ("sqlite3 ~/Documents/Databases/" & databaseName & " \"insert into " & tableName & " (First, Last) values('John', 'Doe'); \"") 

我該如何實現這個版本呢?

set databaseName to "test.db" 
set tableName to "tempTable" 
do shell script ("mkdir -p ~/Documents/Databases/ ; sqlite3 ~/Documents/Databases/" & databaseName & " \"create temp table " & tableName & "(First, Last); \"") 
do shell script ("sqlite3 ~/Documents/Databases/" & databaseName & " \"insert into " & tableName & " (First, Last) values('John', 'Doe'); \"") 

回答

1

SQLite接受以分號分隔的語句本身。

do shell script ("sqlite3 ~/Documents/Databases/" & databaseName & " \" & ¬ 
    "create temp table " & tableName & " (First, Last); " & ¬ 
    "insert into " & tableName & " (First, Last) values('John', 'Doe'); " & ¬ 
    " -- other stuff here --" & ¬ 
    "select * from " & tableName & ";\"") 

在某些時候它變得更容易寫出來作爲一個單獨的腳本,或者可能是這裏的文件,而不是建立一個單一的巨大do shell script,雖然。

+0

完美,謝謝。 – adayzdone 2012-04-06 17:23:31