octave-db

octave-db contains bindings that allow SQL queries and other database operations.
Download

octave-db Ranking & Summary

Advertisement

  • Rating:
  • License:
  • BSD License
  • Price:
  • FREE
  • Publisher Name:
  • Xavier Delacour
  • Publisher web site:

octave-db Tags


octave-db Description

octave-db contains bindings that allow SQL queries and other database operations. octave-db contains bindings that allow SQL queries and other database operations on postgres, mysql, sqlite, and standard ODBC databases from within Octave.A simplified interface that is uniform across all database types is provided: you call sql(db,query) to execute a query on a given database db. If the query returns anything (such as SELECT), this is converted to a cell array. You can use default_db(db) to have the framework remember a database so that you can issue queries with just sql(query). There are four types of database objects, postgres_db, mysql_db, sqlite3_db, and odbc_db. You can construct these by simply calling them with connection parameters (depends on database type).In addition, the entire C client API for each database is exposed (libpq, libmysqlclient, etc).Some examples:octave:1> sqlite3octave:2> db=sqlite3_db(":memory:");octave:3> sql(db,"create table some_table (id int4,val int4);");octave:4> sql(db,"insert into some_table (id,val) values (1,10);");octave:5> sql(db,"insert into some_table (id,val) values (2,9);");octave:6> sql(db,"insert into some_table (id,val) values (3,8);");octave:7> sql(db,"insert into some_table (id,val) values (4,7);");octave:8> a=cell2mat(sql(db,"select * from some_table;"))a = 1 10 2 9 3 8 4 7octave:1> postgresoctave:2> default_db(postgres_db("host=localhost dbname=testdb user=testuser password='secret' "));octave:3> try, sql("drop table some_table;"); catch end_try_catchoctave:4> sql("create table some_table (id int4,str varchar);");octave:5> sql("insert into some_table (id,str) values (1,'a');");octave:6> sql("insert into some_table (id,str) values (2,'b');");octave:7> sql("insert into some_table (id,str) values (3,'c');");octave:8> sql("insert into some_table (id,str) values (4,'d');");octave:9> a=sql("select * from some_table;")a ={ = 1 = 2 = 3 = 4 = a = b = c = d}octave:10> assert(a{1,1}==1);octave:11> assert(strcmp(a{1,2},"a"));octave:1> mysqloctave:2> db=mysql_init();octave:3> res=mysql_real_connect(db,"localhost","root","secret","testdb");octave:4> if (swig_this(res)!=swig_this(db))> error("connect to db failed");> endifoctave:5> mysql_get_client_info()ans = 5.0.45octave:6> mysql_get_client_version()ans = 50045octave:7> mysql_get_host_info(db)ans = Localhost via UNIX socketoctave:8> mysql_get_proto_info(db)ans = 10octave:9> mysql_get_server_info(db)ans = 5.0.45-Debian_1ubuntu3.1-logoctave:10> if (mysql_query(db,"select 2,4,8;"))> error("query failed: %i %s",mysql_errno(db),mysql_error(db));> endifoctave:11> res=mysql_store_result(db);octave:12> f1=mysql_fetch_field_direct(res,0);octave:13> f2=mysql_fetch_field_direct(res,1);octave:14> nc=int32(mysql_field_count(db))nc = 3octave:15> nr=int32(mysql_num_rows(res))nr = 1octave:16> c=cell(nr,nc);octave:17> for i=0:nr-1,> r=mysql_fetch_row(res);> for j=0:nc-1,> c{i+1,j+1}=r(j);> endfor> endforoctave:18> cc ={ = 2 = 4 = 8}There is limited type conversion support built into the simplified API; at least integer and floating point types are converted to their counterparts in Octave. Everything else is returned as a string. If there are errors, these are translated to Octave errors.For each database type, there are a number of tests that exercise both the high-level/unified API as well as the low-level C API of each database. These tests can serve as examples/documentation, since SWIG/Octave doesn't yet provide a way to insert documentation into wrapper code.


octave-db Related Software