主要内容

使用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, 和产品表使用执行.清除MATLAB®工作空间变量。

createInventoryTable = [创建表inventoryTable...'(产品数值数字,数量数字,'...'价格数字,库存DATE 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,CONTESESALESVOLUME)CREATEPRODUCTTABLE = ['创建表ProductTable'...'(productNumber NUMERIC, stockNumber NUMERIC, ')...' supplernumber NUMERIC, unitCost NUMERIC, '...'ProductDescription Varchar(20))'];exec(康涅狄格州,createProductTable)清晰createInventoryTablecreateSuppliers.createSalesVolume...createProductTable.

tutorial.db包含四个空表。

加载数据到SQLite数据库文件

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

负载('sqliteworkflowdata.mat')插入(康涅狄格州,'InventoryTable'...“productNumber”“数量”“价格”“inventoryDate”},cinvtable)插入(Conn,“供应商”...'Instannumber'“SupplierName”“城市”“国家”'传真号'},...Csuppliers)插入(康涅狄格州,“salesVolume”...“StockNumber”“1月”“2”“3”“4”“可能”“6月”...'七月'“八月”'九月'“十月”'十一月'12月的},...csalesvol)插入(conn,“productTable”...“productNumber”“stockNumber”'Instannumber''单位成本'...“productDescription”},cprodtable)清除cinvtable.CsuppliersCsalesVolCprodTable

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

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

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

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

导入数据到MATLAB®

导入产品数据到MATLAB®工作空间使用拿来.变量InventoryTable_Data.供应商_DATA.salesVolume_data, 和producttable_data.包含表中的数据inventoryTable供应商salesVolume, 和产品表

inventoryTable_data =获取(康涅狄格州,'SELECT * FROM inventoryTable');suppliers_data =获取(康涅狄格州,'SELECT * FROM suppliers');salesVolume_data =获取(康涅狄格州,'选择*来自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]} {'nover'} {'new york'} {s manbetx 845'grounds'} {'terrific toys'} {'伦敦'} {'英国'} {'英国'} {[1003]} {'澳大利亚'} {'澳大利亚'} {'澳大利亚'} {'212 435 16345'} {'618 8490 2211'}
: salesVolume_data (1:3)
ans =3×13个单元阵列列1到6 {[125970]} {[1400]} {[1100]} {[2125]} {[1721] {[2125]} {[2125]} {[{[1191]} {[983]} {[1800] {[1200]} {[890]} {[890] {[550]} {[550]} {[550]} {[550]} {[{[65]} {[723] {[723] {[723] {[723] {[723] {[723] {[{[410] {[450] {[19000] {[450] {[19000] {[19000] {{[16000]}
: productTable_data (1:3)
ans =3×5单元阵列{[9]}{[125970]}{[1003]}{[13]}{“维多利亚娃娃”}{[8]}{[212569]}{[1001]}{[5]}{‘火车’}{[7]}{[389123]}{[1007]}{[16]}{引擎工具包的}

关闭sqlite连接

关上(康涅狄格州)

清除MATLAB®工作空间变量。

清除康涅狄格州

另请参阅

||||

相关话题

外部网站