朱莉婭,SQLite3
你好!我有一個 SQLite3 資料庫,想從中獲取資訊到一個字串,而不是DataFrame。
# Connection to database
db = SQLite.DB(raw"pictures.sqlite")
# Creation of table
SQLite.execute(db, "DROP TABLE IF EXISTS Files")
SQLite.execute(db, "CREATE TABLE IF NOT EXISTS Files
(ID INTEGER PRIMARY KEY AUTOINCREMENT,
FullName TEXT,
creation_date TEXT,
change_date TEXT,
size INTEGER,
UNIQUE (ID))")
#Another code
#This is a way to add rows to database (it works)
DBInterface.execute(db,
"INSERT INTO Files (FullName, creation_date, change_date, size)
VALUES
('$fileName', '$cdate', '$mdate', '$fileSize')
")
#Then i am trying to get one row into a string
strq = SQLite.DBInterface.execute(db, "SELECT * FROM Files WHERE ID=3")
#I can't transfornm these to string
我正在寫關于Julia的文章,需要幫助!!
uj5u.com熱心網友回復:
TLDR:
CSV.write(stdout, strq);
解釋
SQLite.jl 的 API 假定DBInterface.execute用于查詢資料庫。此方法產生一個型別為 的物件SQLite.Query。
讓我們看看我們可以用它做什么:
julia> methodswith(SQLite.Query, supertypes=true)
[1] eltype(q::SQLite.Query) in SQLite at ~/.julia/packages/SQLite/wpQeE/src/tables.jl:51
[2] isempty(q::SQLite.Query) in SQLite at ~/.julia/packages/SQLite/wpQeE/src/tables.jl:15
[3] iterate(q::SQLite.Query) in SQLite at ~/.julia/packages/SQLite/wpQeE/src/tables.jl:94
[4] iterate(q::SQLite.Query, rownumber) in SQLite at ~/.julia/packages/SQLite/wpQeE/src/tables.jl:100
SQLite.Query您可以看到一件重要的事情 -只要它與Tables.jl介面匹配,您就可以做任何您想做的事情。所以你沒有義務使用DataFrames,但沒有“純字串”實作,這樣的事情也沒有任何意義。
但是,表格有很多介面。您需要的是CSV.jl完全為您提供 SQL 表的“字串表示”之類的東西。
用你的代碼試試這個(我在我的表中插入了一行來查看結果):
julia> CSV.write(stdout, strq);
ID,FullName,creation_date,change_date,size
1,somefilename,somecdate,somemfate,0
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/485341.html
