Unable to connect to SQLite database

조회 수: 29 (최근 30일)
Annette Hynes
Annette Hynes 2019년 3월 21일
답변: Annette Hynes 2019년 3월 26일
I am unable to use sqlite to connect to databases that have worked previously for me in Matlab and currently work in R. The command:
conn = sqlite(dbfile);
yields the error:
"Error using sqlite
Received exception upon attempting an operation. Exception: [Unable to open the database. (unable to open database file)]. Details:
[connect(~/*.db)]."
The command
isfile(dbfile)
confirms that the file exists.
I tried using the example tutorial SQLiteWorkflow.m, but received an error:
"Error using sqlite
SQLite file exists.
Error in SQLiteWorkflow (line 18)
conn = sqlite('tutorial.db', 'create');"
Any ideas on how to get sqlite working for me again?
  댓글 수: 5
Walter Roberson
Walter Roberson 2019년 3월 21일
My question about dbfile was asking about what the character vector stored in the variable dbfile is -- the path to the database you are trying to connect to.
Annette Hynes
Annette Hynes 2019년 3월 22일
In another development, I used the answer from a post about importing a table from .db and it worked perfectly with my databases.
How can the following code work:
system('sqlite3 -header -csv my_dbfile.db "select * from sfl;" > results.csv');
when I am unable to make a connection using:
conn = sqlite('my_dbfile.db');
?

댓글을 달려면 로그인하십시오.

채택된 답변

Annette Hynes
Annette Hynes 2019년 3월 26일
I am still unable to use 'sqlite' to connect to my .db databases (OSX High Sierra, R2018b). The one change I made in my Matlab setup between when I could connect and now is that I changed the MATLAB_JAVA environment to avoid chronic crashes. I still crash, but less frequently now.
My current workaround is to use 'system' to access my SQLite databases via a shell, export them to a .csv file, and import them into the Matlab workspace.
dbfile = fullfile([file_path, '/'], [this_cruise, '.db']);
sqlquery = 'SELECT * FROM sfl';
outfile = [file_path, '/', this_cruise, '_PAR.csv'];
cmd = ['sqlite3 -header -csv ', dbfile, ' "', sqlquery, ';" > ', outfile];
system(cmd);
T = readtable(outfile);

추가 답변 (1개)

Annette Hynes
Annette Hynes 2019년 3월 21일
pwd
'/Users/annettehynes/Documents/MATLAB/Examples/database/WorkWithDataMATLABInterfaceToSQLiteExample'
ls
WorkWithDataMATLABinterfaceToSQLiteExample.mlx
My .db file is stored locally on my machine so the path is simply a local path. On another note, why am I unable to copy and paste into these question boxes?
  댓글 수: 2
Walter Roberson
Walter Roberson 2019년 3월 21일
Unfortunately with the new editor, these boxes to enter in comments etc. do not show up as text boxes for the purposes of browsers, so browsers do not enable menu items for copying and pasting.
You appear to be using Mac. If you are not up to date on your Firefox, you might possibly be able to use command-V to paste into these boxes. However that will probably fail if you are up to date on Firefox. If you use Safari then command-V will work (at least for now...)
Annette Hynes
Annette Hynes 2019년 3월 21일
Thanks for the tip!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by