我可以使用八度访问sqlite3吗? [英] Can I access sqlite3 using octave?
问题描述
是否可以从八度读取和写入sqlite3?
Is there a way to read and write to sqlite3 from octave?
我正在考虑使用R中的RODBC或python中的sqlite3包的方法,但要使用八度.
I'm thinking something along the lines of RODBC in R or the sqlite3 package in python, but for octave.
我查看了八度伪造 http://octave.sourceforge.net/packages.php
但是只能找到仅支持postgresql的数据库"包.
But could only find the 'database' package, which only supports postgresql.
详细信息:
- 操作系统:Ubuntu 12.04
- 八度:3.6.2
- sqlite:3.7.9
推荐答案
我意识到这是一个老问题,但是这里的大多数答案似乎都没有抓住重点,重点在于是否存在提供正式接口的定制八度音程包,而不是首先是否可以从八度内执行sqlite3查询.
I realise this is an old question, but most answers here seem to miss the point, focusing on whether there exists a bespoke octave package providing a formal interface, rather than whether it is possible to perform sqlite3 queries from within octave at all in the first place.
因此,我想为任何试图通过八度访问 sqlite3的人提供一个实用的答案;这样做实在是微不足道的,我自己也这样做过很多次.
Therefore I thought I'd provide a practical answer for anyone simply trying to access sqlite3 via octave; it is in fact trivial to do so, I have done so myself many times.
只需对 sqlite3
命令进行适当的系统调用(显然这意味着您的系统上安装了sqlite3客户端).我发现最方便的方法是使用
Simply do an appropriate system call to the sqlite3
command (obviously this implies you have an sqlite3 client installed on your system). I find the most convenient way to do so is to use the
sqlite3数据库.sqlite<FileContainingQuery> OutputToFile
sqlite3 database.sqlite < FileContainingQuery > OutputToFile
调用sqlite3的语法.
syntax for calling sqlite3.
任何修改输出的sqlite3命令都可以与查询一起传递,以获得所需格式的输出.
Any sqlite3 commands modifying output can be passed together with the query to obtain the output in the desired format.
例如这是一个玩具示例,它以表格形式绘制频率图表,该表格以csv格式返回适当的得分和计数(从输出中删除标题和运行时统计信息).
E.g. here's a toy example plotting a frequency chart from a table which returns appropriate scores and counts in csv format (with headers and runtime stats stripped from the output).
pkg load io % required for csv2cell (used to collect results)
% Define database and Query
Database = '/absolute/path/to/database.sqlite';
Query = strcat(
% Options to sqlite3 modifying output format:
".timer off \n", % Prevents runtime stats printed at end of query
".headers off \n", % If you just want the output without headers
".mode csv \n", % Export as csv; use csv2cell to collect results
% actual query
"SELECT Scores, Counts \n",
"FROM Data; \n" % (Don't forget the semicolon!)
);
% Create temporary files to hold query and results
QueryFile = tempname() ; QueryFId = fopen( QueryFile, 'w' );
fprintf( QueryFId, Query ); fclose( QueryFId);
ResultsFile = tempname();
% Run query
Cmd = sprintf( 'sqlite3 "%s" < "%s" > "%s"', Database, QueryFile, ResultsFile );
[Status, Output] = system( Cmd );
% Confirm query succeeded and if so collect Results
% in a cell array and clean up temp files.
if Status != 0, delete( QueryFile, ResultsFile ); error("Query Failed");
else, Results = csv2cell( ResultsFile ); delete( QueryFile, ResultsFile );
end
% Process Results
Results = cell2mat( Results );
Scores = Results(:, 1); Counts = Results(:, 2);
BarChart = bar( Scores, Counts, 0.7 ); % ... etc
Et,voilà
这篇关于我可以使用八度访问sqlite3吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!