主要内容

使用MATLAB接口导入数据到SQLite

这个例子展示了如何在MATLAB®和MATLAB接口之间移动数据到SQLite。假设您有想要导入到MATLAB中的产品数据。您可以将这些数据快速加载到SQLite数据库文件中。无需安装数据库和驱动程序。有关SQLite的MATLAB接口的详细信息,请参见用MATLAB接口实现SQLite数据库中的数据交互

创建SQLite连接

创建一个SQLite连接康涅狄格州到一个新的SQLite数据库文件tutorial.db.指定当前工作文件夹中的文件名。

Dbfile = fullfile(pwd,“tutorial.db”);Conn = sqlite(dbfile,“创造”);

在SQLite数据库文件中创建表

创建表inventoryTable供应商salesVolume,productTable使用执行.清除MATLAB工作空间变量。

createInventoryTable = strcat(创建目录目录..."(产品编号数值,数量数值,"...“价格数字,库存日期VARCHAR)”);execute(conn,createInventoryTable) createsupitors = strcat(“创建表格供应商”..."(供应商编号NUMERIC,供应商名称VARCHAR(50), "...“VARCHAR市(20),VARCHAR乡(20),”...“FaxNumber VARCHAR(20))”);execute(conn, createsupproviders) createSalesVolume = strcat(创建表salesVolume...“(股票号码数字,一月数字,”...“二月数字,三月数字,四月数字,”...“五月数字,六月数字,七月数字,”...“八月数字,九月数字,十月数字,”...“十一月数字,十二月数字)”);execute(conn,createSalesVolume) createProductTable = strcat(创建表productTable..."(productNumber数值,stockNumber数值,"..."supplierNumber NUMERIC, unitCost NUMERIC, "...“productDescription VARCHAR(20))”);execute(康涅狄格州,createProductTable)清晰createInventoryTablecreateSupplierscreateSalesVolume...createProductTable

tutorial.db包含四个空表。

将数据加载到SQLite数据库文件

加载名为的mat文件sqliteworkflowdata.mat.的变量CinvTableCsuppliersCsalesVol,CprodTable包含用于导出的数据。中的表中导出数据tutorial.db使用sqlwrite.清除MATLAB工作空间变量。

负载(“sqliteworkflowdata.mat”tablename = .“inventoryTable”;变量名= [“productNumber”“数量”“价格”“inventoryDate”];数据= cell2table(CinvTable);%将数据从单元格数组转换为表data.Properties.VariableNames = varnames;%设置变量名Sqlwrite (conn,tablename,data) tablename =“供应商”;变量名= [“SupplierNumber”“SupplierName”“城市”“国家”“FaxNumber”];data = cell2table(cproviders);%将数据从单元格数组转换为表data.Properties.VariableNames = varnames;%设置变量名Sqlwrite (conn,tablename,data) tablename =“salesVolume”;变量名= [“StockNumber”“1月”“2”“3”“4”“可能”“6月”...“7”“八月”“9”“十月”“11月”“12月”];data = cell2table(CsalesVol);%将数据从单元格数组转换为表data.Properties.VariableNames = varnames;%设置变量名Sqlwrite (conn,tablename,data) tablename =“productTable”;变量名= [“productNumber”“stockNumber”“supplierNumber”“unitCost”...“productDescription”];data = cell2table(CprodTable);%将数据从单元格数组转换为表data.Properties.VariableNames = varnames;%设置变量名sqlwrite(康涅狄格州、表、数据)清晰CinvTableCsuppliersCsalesVolCprodTable

关闭SQLite连接。清除MATLAB工作空间变量。

关上(康涅狄格州)清晰康涅狄格州

创建一个只读的SQLite连接tutorial.db

Conn = sqlite(“tutorial.db”“readonly”);

在MATLAB中导入数据

将产品数据导入到MATLAB工作区中获取.变量inventoryTable_datasuppliers_datasalesVolume_data,productTable_data包含来自表的数据inventoryTable供应商salesVolume,productTable

=获取(conn,SELECT * FROM inventoryTable);Suppliers_data = fetch(conn,"SELECT * FROM供应商");salesVolume_data = fetch(conn,"SELECT * FROM salesVolume");productTable_data =获取(conn,“SELECT * FROM productTable”);

显示每个表中的前三行数据。

头(inventoryTable_data, 3)
productNumber数量价格inventoryDate  _____________ ________ _____ ______________________ 1 1700 14.5”9/23/2014 9:38:34是“1200 9.3“7/8/2014 10:50:45点”3 356 17.2“5/14/2014 7:14:28点”
头(suppliers_data, 3)
FaxNumber SupplierNumber SupplierName城市国家  ______________ _________________ __________ ________________ _______________ 1001年“神奇产品”“纽约”“美国”“2下载188bet金宝搏12 435 1617”1002“很棒的玩具”“伦敦”“联合王国”“44 456 9345”1003“古怪小部件”“阿德莱德”“澳大利亚”“618 8490 2211”
头(salesVolume_data, 3)
股票编号一月二月三月四月五月六月七月八月九月十月十一月十二月___________ _______ ________ __________ ___________ ______ _________ _______ ________ ________ 125970 1400 1100 981 882 794 752 654 773 809 980 3045 19000 212569 2400 1721 1414 1191 983 825 731 653 723 790 1400 5000 389123 1800 1200 890 670 550 450 400 410 402 450 1200 16000
头(productTable_data, 3)
productNumber stockNumber supplierNumber unitCost productDescription  _____________ ___________ ______________ ________ __________________ 9 125970 1003 13“维多利亚娃娃”8 212569 1001 5“火车”7 389123 1007 16“引擎工具包”

关闭SQLite连接

关上(康涅狄格州)

清除MATLAB工作空间变量。

清晰的康涅狄格州

另请参阅

对象

功能

相关的话题

外部网站