在 SQLite3.exe 中执行命令
#include <SQLite.au3>
_SQLite_SQLiteExe($sDatabaseFile, $sInput, ByRef $sOutput [, $sSQLiteExeFilename = "SQLite3.exe" [, $fDebug = False]])
$sDatabaseFile | 数据库文件名 |
$sInput | SQLite3.exe 命令 |
$sOutput | SQLite3.exe 原输出 |
$sSQLiteExeFilename | [可选参数] SQLite3.exe 路径 |
$fDebug | [可选参数] Write SQLite3.exe exitcode through the callback function specified in _SQLite_Startup() (default is don't print) |
成功: | 返回 $SQLITE_OK |
失败: | 返回值可能违反 $SQLITE_* 常量 |
@error: | 1 - 不能创建新的数据库 |
2 - SQLite3.exe 未找到 | |
3 - SQL 错误 / 未完成 SQL | |
4 - 无法打开输入文件 |
#include <SQLite.au3>
#include <SQLite.dll.au3>
#include <File.au3>
;文件名
Local $sTsvFile = FileGetShortName(_TempFile(@ScriptDir, "~", ".tsv"))
Local $sDbFile = FileGetShortName(_TempFile(@ScriptDir, "~", ".db"))
;创建 Tsv 文件
FileWriteLine($sTsvFile, "a" & @TAB & "b" & @TAB & "c")
FileWriteLine($sTsvFile, "a1" & @TAB & "b1" & @TAB & "c1")
FileWriteLine($sTsvFile, "a2" & @TAB & "b2" & @TAB & "c2")
;导入(使用 SQLite3.exe)
Local $sIn, $sOut, $i, $sCreate = "CREATE TABLE TblImport (";
For $i = 1 To _StringCountOccurance(FileReadLine($sTsvFile, 1), @TAB) + 1
$sCreate &= "Column_" & $i & ","
Next
$sCreate = StringTrimRight($sCreate, 1) & ");"
$sIn = $sCreate & @CRLF ; 创建表
$sIn &= ".separator \t" & @CRLF ; 把 @TAB 作为分隔符
$sIn &= ".import '" & $sTsvFile & "' TblImport" & @CRLF
_SQLite_SQLiteExe($sDbFile, $sIn, $sOut, -1, True)
If @error = 0 Then
;显示表 (使用 SQLite3.dll)
Local $iRows, $iColumns, $aRes
_SQLite_Startup()
ConsoleWrite("_SQLite_LibVersion=" & _SQLite_LibVersion() & @CRLF)
_SQLite_Open($sDbFile)
_SQLite_GetTable2d(-1, "SELECT ROWID,* FROM TblImport;", $aRes, $iRows, $iColumns)
_SQLite_Display2DResult($aRes) ; 输出到控制台
_SQLite_Close()
_SQLite_Shutdown()
Else
If @error = 2 Then
ConsoleWrite("ERROR: Sqlite3.exe file not found" & @CRLF)
Else
ConsoleWrite("ERROR: @error=" & @error & " when calling _SQLite_SQLiteExe" & @CRLF)
EndIf
EndIf
;移除临时文件
FileDelete($sTsvFile)
FileDelete($sDbFile)
;~ Output:
;~ rowid Column_1 Column_2 Column_3
;~ 1 a b c
;~ 2 a1 b1 c1
;~ 3 a2 b2 c2
Func _StringCountOccurance($sSearchString, $sSubString, $fCaseSense = 0) ; 返回在 $sSearchString 中 $sSubString 的数目
Local $iOccCnt = 1
Do
If StringInStr($sSearchString, $sSubString, $fCaseSense, $iOccCnt) > 0 Then
$iOccCnt += 1
Else
ExitLoop
EndIf
Until 0
Return $iOccCnt - 1
EndFunc ;==>_StringCountOccurance