2016-02-26 23 views
0

我一直在試圖從MATLAB連接到SQLite數據庫文件。我的本地目錄中已經有由my-db.sqlite的名稱創建的數據庫。我也下載了sqlite-jdbc-x.x.x.jar文件,並給出了路徑javaaddpath('path to jdbc'),然後給出了所有其他所需的信息。下面的代碼將幫助你理解。如何從MATLAB連接到SQLite?

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 

因此,result給我0作爲輸出,不過,我知道這不是正確的,因爲我可以跨越從原來的SQLite數據庫檢查。我做了所有的事情MATLAB's SQLite documentation要求做,但它仍然沒有連接,並給我不正確的結果。 請幫助!

回答

-1

你使用的是什麼版本的Matlab? MATLAB提供了一種簡單的方法來處理SQLite數據庫中提供:https://www.mathworks.com/help/database/ug/sqlite.html

例子:

conn = sqlite('MyDb.db'); 
sqlquery = 'select productdescription from productTable'; 
rowlimit = 5; 

results = fetch(conn,sqlquery,rowlimit);