How to connect to a in-memory sqlite database using the Database Toolbox
9 ビュー (過去 30 日間)
古いコメントを表示
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!
0 件のコメント
回答 (1 件)
Kojiro Saito
2024 年 2 月 26 日
sqlite command only allows file name as input (like data.db), so you need to use JDBC interface instead.
%% 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)
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Database Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!