主要内容

导入数据使用MATLAB接口SQLite

这个例子展示了如何在MATLAB®之间移动数据和MATLAB接口SQLite。假设你有你想要的产品数据导入MATLAB。你可以很快这个数据加载到一个SQLite数据库文件。你不需要安装数据库或司机。SQLite MATLAB接口的详细信息,请参阅使用MATLAB与SQLite数据库的数据交互接口SQLite

创建SQLite连接

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

dbfile = fullfile (pwd,“tutorial.db”);康涅狄格州= sqlite (dbfile,“创造”);

SQLite数据库中创建表文件

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

createInventoryTable = strcat (“创建表inventoryTable”,”(productNumber数字、数量的数字,“,“价格数字,inventoryDate VARCHAR)”);执行(康涅狄格州,createInventoryTable) createSuppliers = strcat (“创建表供应商”,”(SupplierNumber数字,SupplierName VARCHAR (50),“,“城市VARCHAR(20)、国家VARCHAR (20),“,“FaxNumber VARCHAR (20))”);执行(康涅狄格州,createSuppliers) createSalesVolume = strcat (“创建表salesVolume”,”(StockNumber数字,数字1月”,”2月数字,数字3月、4月数字,”,“可能数字,数字6月,7月的数字。”,“8月数字,数字9月,10月数字。”,“数字11月,12月的数字)”);执行(康涅狄格州,createSalesVolume) createProductTable = strcat (“创建表productTable”,”(productNumber数字,stockNumber数字。”,“supplierNumber数字,unitCost数字。”,“productDescription VARCHAR (20))”);execute(康涅狄格州,createProductTable)清晰createInventoryTablecreateSupplierscreateSalesVolumecreateProductTable

tutorial.db包含四个空表。

数据加载到SQLite数据库文件

加载MAT-file命名sqliteworkflowdata.mat。的变量CinvTable,Csuppliers,CsalesVol,CprodTable包含数据用于出口。导出数据到表中tutorial.db使用sqlwrite。清晰的MATLAB工作空间变量。

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

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

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

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

康涅狄格州= sqlite (“tutorial.db”,“readonly”);

数据导入MATLAB

将产品数据导入MATLAB工作区使用获取。变量inventoryTable_data,suppliers_data,salesVolume_data,productTable_data包含的数据表inventoryTable,供应商,salesVolume,productTable

inventoryTable_data =获取(康涅狄格州,“从inventoryTable SELECT *”);suppliers_data =获取(康涅狄格州,“从供应商选择*”);salesVolume_data =获取(康涅狄格州,“从salesVolume SELECT *”);productTable_data =获取(康涅狄格州,“从productTable SELECT *”);

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

头(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)
_________________ __________ SupplierNumber SupplierName城市国家FaxNumber * * * ___________售予1001“神奇产品”,“纽约”“美国”“212 435 1617”1002“很棒的玩具”“伦敦”“联合王下载188bet金宝搏国”“44 456 9345”1003“古怪小部件”“阿德莱德”“澳大利亚”“618 8490 2211”
头(salesVolume_data, 3)
StockNumber 1月2月3月4月5月6月7月8月9月10月11月12月___________累积________ _____ _____ _____ ___ _____ _____解释说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工作空间变量。

清晰的康涅狄格州

另请参阅

对象

功能

相关的话题

外部网站