jeudi 25 février 2016

How to connect to SQLite from MATLAB?

I have been trying to connect to SQLite database file from MATLAB. I already have the database created by the name of my-db.sqlite on my local directory. I also have downloaded the sqlite-jdbc-x.x.x.jar file and have given its path to javaaddpath('path to jdbc'), then gave all other required information. The code below will help you understand.

javaaddpath('C:\Users\cse100975\AppData\Roaming\MathWorks\MATLAB\javaDirectory\sqlite-jdbc-3.8.11.2.jar');
dbpath = 'C:\Users\Neelabh Pant\Documents\Dr. Elmasri\Datasets\my_db.db';
user = '';
password = '';
driver = 'org.sqlite.JDBC';
protocol = 'jdbc';
subprotocol = 'sqlite';
resource = dbpath;
url = strjoin({protocol, subprotocol, resource}, ':');
conn = database(dbpath, user, password, driver, url);
query = 'SELECT COUNT(DISTINCT usr_id) from usr';
cursor = exec(conn, query);
cursor = fetch(cursor);
result = cursor.Data

So, the result gives me 0 as the output, though, I know that's not correct because I can cross check it from the original SQLite database. I did everything the way MATLAB's SQLite documentation asks to do but still it doesn't get connected and gives me the incorrect result. PLEAS HELP!

Aucun commentaire:

Enregistrer un commentaire