主要内容

使用MATLAB®接口导入数据到SQLite

这个例子展示了如何在MATLAB®和MATLAB®接口之间移动数据到SQLite。假设您有想要导入到MATLAB®中的产品数据。您可以将这些数据快速加载到SQLite数据库文件中。不需要安装数据库或驱动程序。关于MATLAB®到SQLite的接口的详细信息,请参见与MATLAB接口工作到SQLite.要获得更多功能,请使用JDBC驱动程序连接到SQLite数据库文件。有关详细信息,请参见配置驱动和数据源

要访问此示例的代码,请输入编辑SQLiteWorkflow.m

创建SQLite连接

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

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

在SQLite数据库文件中创建表

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

createInventoryTable = [创建表inventoryTable...'(productNumber NUMERIC, Quantity NUMERIC, ')...'Price NUMERIC, inventoryDate VARCHAR)'];exec(conn,createInventoryTable)'创建表供应商'...'(SupplierNumber NUMERIC, SupplierName varchar(50), ')...City varchar(20), Country varchar(20)...“FaxNumber varchar(20))”];exec(conn, createsproviders) createSalesVolume = [/ cn]创建表salesVolume...'(StockNumber NUMERIC, January NUMERIC, ')...'二月数值型,三月数值型,四月数值型,'...'May NUMERIC, June NUMERIC, July NUMERIC, '...'August NUMERIC, September NUMERIC, October NUMERIC, '...'November NUMERIC, December NUMERIC)'];exec(conn,createSalesVolume) createProductTable = ['create table productTable '...'(productNumber NUMERIC, stockNumber NUMERIC, ')...' supplernumber NUMERIC, unitCost NUMERIC, '...“productDescription varchar(20))”];exec(康涅狄格州,createProductTable)清晰createInventoryTablecreateSupplierscreateSalesVolume...createProductTable

tutorial.db包含四个空表。

加载数据到SQLite数据库文件

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

负载(“sqliteworkflowdata.mat”)插入(康涅狄格州,“inventoryTable”...“productNumber”“数量”“价格”“inventoryDate”}, CinvTable)插入(康涅狄格州,“供应商”...“SupplierNumber”“SupplierName”“城市”“国家”“FaxNumber”},...Csuppliers)插入(康涅狄格州,“salesVolume”...“StockNumber”“1月”“2”“3”“4”“可能”“6月”...“7”“八月”“9”“十月”11月的12月的},...CsalesVol)插入(康涅狄格州,“productTable”...“productNumber”“stockNumber”“supplierNumber”“unitCost”...“productDescription”}, CprodTable)清晰CinvTableCsuppliersCsalesVolCprodTable

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

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

创建到的只读SQLite连接tutorial.db

康涅狄格州= sqlite (“tutorial.db”“只读”);

导入数据到MATLAB®

导入产品数据到MATLAB®工作空间使用获取.变量inventoryTable_datasuppliers_datasalesVolume_data,productTable_data包含表中的数据inventoryTable供应商salesVolume,productTable

inventoryTable_data =获取(康涅狄格州,'SELECT * FROM inventoryTable');suppliers_data =获取(康涅狄格州,'SELECT * FROM suppliers');salesVolume_data =获取(康涅狄格州,'SELECT * FROM salesVolume');productTable_data =获取(康涅狄格州,'SELECT * FROM productTable');

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

: inventoryTable_data (1:3)
ans =3×4单元阵列{[1]}{[1700]}{[14.5000]}{'9/23/2014 9:38…'}{[2]}{[1200]}{[9.3000]}{'7/8/2014 10:50…'}{[3]}{[356]}{[17.2000]}{“5/14/2014 7:14)…”}
: suppliers_data (1:3)
ans =3×5单元阵列列1至4{[1001]}{'奇妙产品'}{'纽约'}{'美国'}{[1002下载188bet金宝搏]}{'了不起的玩具'}{'伦敦'}{'联合王国'}{[1003]}{'奇奇怪怪的小部件'}{'阿德莱德'}{'澳大利亚'}
: salesVolume_data (1:3)
ans =3×13单元阵列列1到6{[125970]}{[1400]}{[1100]}{[981]}{[882]}{[794]}{[212569]}{[2400]}{[1721]}{[1414]}{[1191]}{[983]}{[389123]}{[1800]}{[1200]}{[890]}{[670]}{[550]}列7到12 {[752]}{[654]}{[773]}{[809]}{[980]}{[3045]}{[825]}{[731]}{[653]}{[723]}{[790]}{[1400]}{[450]}{[400]}{[410]}{[402]}{[450]}{[1200]}列13 {[19000]}{[5000]}{[16000]}
: productTable_data (1:3)
ans =3×5单元阵列{[9]}{[125970]}{[1003]}{[13]}{“维多利亚娃娃”}{[8]}{[212569]}{[1001]}{[5]}{‘火车’}{[7]}{[389123]}{[1007]}{[16]}{引擎工具包的}

关闭SQLite连接

关上(康涅狄格州)

清除MATLAB®工作空间变量。

清晰的康涅狄格州

另请参阅

||||

相关的话题

外部网站