How to connect to a in-memory sqlite database using the Database Toolbox

조회 수: 9 (최근 30일)
Jack
Jack 2024년 2월 23일
답변: Kojiro Saito 2024년 2월 26일
According to the sqlite docs (https://www.sqlite.org/inmemorydb.html), it is possible to create an in-memory database using the :memory: file name. I attempted to create the database like :
conn = sqlite(":memory:", "create");
This did not work and returned the following error:
Error using sqlite
Database file :memory: was not created.
Error in index (line 2)
conn = sqlite(":memory:", "create");
Has anyone had any luck with in-memory databases or have any guidance?
Thank you!

답변 (1개)

Kojiro Saito
Kojiro Saito 2024년 2월 26일
sqlite command only allows file name as input (like data.db), so you need to use JDBC interface instead.
You can use jdbc:sqlite::memory in the URL of databaseConnectionOptions.
%% Setup with JDBC driver
opts = databaseConnectionOptions("jdbc", "Other");
opts = setoptions(opts, ...
'DataSourceName',"SQLite-jdbc-memory", ...
'JDBCDriverLocation',"C:\Path\to\sqlite-jdbc-3.45.1.0.jar", ...
'Driver', "org.sqlite.JDBC", "URL", "jdbc:sqlite::memory:");
username = "";
password = "";
saveAsDataSource(opts)
%% Connect to SQLite in Memory with JDBC
conn = database("SQLite-jdbc-memory", username, password);
%% Do something
% Write table to SQLite
LastName = ["Sanchez";"Johnson";"Zhang";"Diaz";"Brown"];
Age = [38;43;38;40;49];
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
t = table(LastName,Age,Height,Weight);
sqlwrite(conn, "mytable", t)
% Read from SQLite
t2 = sqlread(conn, "mytable");
%% Close connections
close(conn)

카테고리

Help CenterFile Exchange에서 Database Toolbox에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by