MATLAB Answers

0

How do I pull data from an SQLite daatbase into Matlab workspace

Asked by Charles on 4 Sep 2017
Latest activity Commented on by Kojiro Saito on 5 Sep 2017
Hi I have somehow managed to put together some python code to retrieve data in an SQL lite database, I know which to pull that same data from the SQLite data base into the Matlab workspace. Do I merely connect to the database using 'conn' and then use 'fetch'? Is there another means by which I can pull the data into a cell array structure?

  0 Comments

Sign in to comment.

1 Answer

回答者: Kojiro Saito
2017 年 9 月 5 日

The simple way is using sqlite and fetch.
conn = sqlite('sqlite_db.db');
sqlquery = 'SELECT * FROM someTable';
extracted_data = fetch(conn, sqlquery);
close(conn);
For detail, please see this link and this link.

  2 Comments

Charles
2017 年 9 月 5 日
Ok. Thank you I will try this. If I also want to see the fields i.e table structure in my db, how do I do this?
Kojiro Saito
2017 年 9 月 5 日
To see the fields of the table, you need to change the query. For example, if you want to see the fields of table "inventoryTable",
conn = sqlite('sqlite_db.db');
sqlquery = 'SELECT sql FROM sqlite_master WHERE tbl_name = "inventoryTable" AND type = "table"';
extracted_data = fetch(conn, sqlquery)
These commands will show the fields.
Or alternatively, the following will list the all tables of sqlite_db.db file.
conn = sqlite('sqlite_db.db');
sqlquery = 'SELECT * FROM sqlite_master WHERE type="table"';
extracted_data = fetch(conn, sqlquery)

Sign in to comment.