前面介绍过使用MySQL数据库如何在matlab中使用,但是安装配置MySQL不是很方便,在matlab中加载对应Java相关文件也不容易,这里介绍一个方便使用的数据库sqlite,复制文件夹,加入路径即可使用。
% 避免旧数据,类似clear clc close
try
if exist('demo.db','file')
delete('demo.db');
end
catch
error( 'Unable to delete database' );
end
% 创建数据库文件
sqlite3('demo.db',...
['CREATE TABLE test ',...
'(some_text TEXT, some_int INT, some_real REAL);']);
% 准备插入的数据
data=struct;
data(1).text = 'world';
data(1).int = 1337;
data(1).dbl = 2.71828;
data(2).text = 'demobar';
data(2).int = -4131;
data(2).dbl = 9.0;
data(3).text = 'demo-bar';
data(3).int = 404;
data(3).dbl = 2*pi;
% 插入多组数据
sqlite3('demo.db',...
['INSERT INTO test (some_text, some_int, some_real) ',...
'VALUES (?, ?, ?);'], data)
% 插入一组数据
sqlite3('demo.db',...
['INSERT INTO test (some_text, some_int, some_real) ',...
'VALUES ("hello", 42, 3.14159);'])
% 查询
x = sqlite3('demo.db',...
'SELECT some_text, some_int, some_real as real FROM test;');
y=sqlite3('demo.db','SELECT * FROM test;');
% 还是一样的全部数据被查询出来
% 修改
sqlite3('demo.db',...
'UPDATE test SET some_int = "2333" WHERE some_text = "world";');
% 删除
sqlite3('demo.db',...
'DELETE FROM test WHERE some_text = "hello";');
y=sqlite3('demo.db','SELECT * FROM test;');
% 模糊查询
y=sqlite3('demo.db','SELECT * FROM test WHERE some_text LIKE "demo%";');
这个模糊查询是要说的重点,demo%是查找demo开头的,%demo是查找demo结尾的,%demo%是查找包含demo的