用Julia 0.51操作sqlite資料庫

lt發表於2017-04-01

按照 http://blog.csdn.net/wowotuo/article/details/41909955 的提示操作,結果報錯。

julia> using SQLite;
ERROR: ArgumentError: Module SQLite not found in current path.
Run `Pkg.add("SQLite")` to install the SQLite package.
 in require at .\loading.jl:365

julia> Pkg.add("SQLite")
INFO: Initializing package repository C:\Users\qiwang\.julia\v0.5
INFO: Cloning METADATA from https://github.com/JuliaLang/METADATA.jl
ERROR: GitError(Code:ERROR, Class:OS, failed to send request: 無法解析伺服器的名稱或地址

連網以後,下載後設資料和軟體。現在的軟體太依賴網際網路了,沒網路就沒辦法正常工作

julia> Pkg.add("SQLite")
INFO: Initializing package repository C:\Users\qiwang\.julia\v0.5
INFO: Cloning METADATA from https://github.com/JuliaLang/METADATA.jl
INFO: Cloning cache of BinDeps from https://github.com/JuliaLang/BinDeps.jl.git
INFO: Cloning cache of BufferedStreams from https://github.com/BioJulia/BufferedStreams.jl.git
INFO: Cloning cache of CategoricalArrays from https://github.com/JuliaData/CategoricalArrays.jl.git
...

INFO: Installing BinDeps v0.4.7
INFO: Installing BufferedStreams v0.3.2
INFO: Installing CategoricalArrays v0.1.3
INFO: Installing Compat v0.21.0
INFO: Installing DataArrays v0.3.12
INFO: Installing DataFrames v0.9.0
INFO: Installing DataStreams v0.1.3
INFO: Installing DataStructures v0.5.3
INFO: Installing FileIO v0.3.1
INFO: Installing GZip v0.3.0
INFO: Installing LegacyStrings v0.2.1
INFO: Installing LibExpat v0.2.5
INFO: Installing Libz v0.2.4

INFO: Packages to install: libwinpthread1, libgcc_s_sjlj1, libsqlite3-0
INFO: Downloading: libwinpthread1
INFO: Extracting: libwinpthread1
INFO: Downloading: libgcc_s_sjlj1
INFO: Extracting: libgcc_s_sjlj1
INFO: Downloading: libsqlite3-0
INFO: Extracting: libsqlite3-0
INFO: Complete
INFO: Package database updated

大約10分鐘,安裝完畢後,在
C:\Users\aa.julia目錄下生成了許多目錄和檔案:

大小:103 MB (108,602,457 位元組)    
佔用空間:222 MB (233,005,056 位元組)    
包含31,129 個檔案,19,581 個資料夾    

繼續在Julia環境下輸入,又報錯:

julia> using SQLite;
INFO: Precompiling module SQLite.

julia> db=SQLiteDB("d:\\sqlite\\testsqlite.db");
ERROR: UndefVarError: SQLiteDB not defined

輸入?進入幫助系統。

help?> SQLite
search: SQLite

  No documentation found.

  Displaying the README.md for the module instead.

裡面有示例程式碼如下:

julia> using SQLite

julia> db = SQLite.DB("Chinook_Sqlite.sqlite")

julia> # using SQLite's in-built syntax

julia> SQLite.query(db, "SELECT FirstName, LastName FROM Employee WHERE LastName REGEXP 'e(?=a)'")
1x2 ResultSet
| Row | "FirstName" | "LastName" |
|-----|-------------|------------|
| 1   | "Jane"      | "Peacock"  |

原來是julia的sqlite語法變化了。
在我自己的機器上練習。

先用sqlite軟體建立表並儲存到檔案。

sqlite> create table test(a int,b varchar(10));
sqlite> insert into test values(1,'a');
sqlite> select * from test;
1|a
sqlite> .save testsqlite.db

切換到Julia環境,查詢並插入。


julia> db=SQLite.DB("d:\\sqlite\\testsqlite.db");

julia> SQLite.query(db, "SELECT * from test")
1××2 DataFrames.DataFrame
││ Row ││ a ││ b   ││
├├──────────┼┼──────┼┼──────────┤┤
││ 1   ││ 1 ││ "a" ││


julia> SQLite.query(db, "insert into test values(2,'b')");

julia> SQLite.query(db, "SELECT * from test")
2××2 DataFrames.DataFrame
││ Row ││ a ││ b   ││
├├──────────┼┼──────┼┼──────────┤┤
││ 1   ││ 1 ││ "a" ││
││ 2   ││ 2 ││ "b" ││

再回到sqlite軟體,可以看到這個資料庫檔案中的資料已經變化。

sqlite> select * from test;
1|a
sqlite> .open testsqlite.db
sqlite> select * from test;
1|a
2|b

Julia的語法穩定性看來是個大問題,歷史程式碼都不能重用。

相關文章