使用DBConnection与CPLEX建立CSV连接

时间:2017-11-12 17:58:46

标签: csv input cplex opl

我想将CPLEX连接到CSV格式的文件,我的数据应该写入和读取。

我认为我正确地将CSV文件连接到“provacsv”文件夹中,如下所示:

DBConnection db("odbc","DRIVER={Microsoft Access Text Driver (*.txt, *.csv)}; DBQ=C:\\Users\\Giovanni\\opl\\provacsv");

然而,我无法读取数据,即使像2列值一样简单。我使用以下语法:

c from DBRead(db,"SELECT c FROM cb.csv");
b from DBRead(db,"SELECT b FROM cb.csv");
A from DBRead(db,"SELECT * FROM A.csv");    // A is a matrix of values (like 2X2 matrix)

我还想用我的文件输出:

x to DBWrite(db,"WRITE x TO x.csv");

2 个答案:

答案 0 :(得分:0)

检查example from IBM Support这基本上归结为使用ODBC驱动程序。使用Microsoft Access。将此代码放在*.dat文件中:

DBConnection db("odbc","DRIVER={Microsoft Access Text Driver (*.txt, *.csv)}; DBQ=.\\data");
Gasolines,Gas from DBRead(db,"SELECT name,name,demand,price,octane,lead FROM GasData.csv");
Oils,Oil from DBRead(db,"SELECT name,name,capacity,price,octane,lead FROM OilData.csv");
MaxProduction = 14000;
ProdCost = 4;

然后在子文件夹.\data中,您必须放置*.csv个文件。我尝试使用兼容的Access驱动程序,它抛出了未定义元素的错误。但至少这是在官方IBM示例中这样做的官方方式。

答案 1 :(得分:-2)

读取csv

你应该使用IloOplInputFile:

假设您有此export.cvs文件

Nicolas;2;
Alexander;3;

然后你可以写

tuple t
{
string firstname;
int number;
}

{t} s={};

execute
{
var f=new IloOplInputFile("export.csv");
while (!f.eof)
{
var str=f.readline();
//writeln(str);
var ar=str.split(";");
if (ar.length==3) s.add(ar[0],Opl.intValue(ar[1]));
}
f.close();
}

execute
{
writeln(s);
}

将读取csv文件并计算元组集合:

{<"Nicolas" 2> <"Alexander" 3>}

和写作

tuple t
{
string firstname;
int number;
}

{t} s={<"Nicolas",2>,<"Alexander",3>};

execute
{
var f=new IloOplOutputFile("export.csv");
for(var i in s)
{
f.writeln(i.firstname,";",i.number,";");
}
f.close();
}

写一个文件export.csv

Nicolas;2;
Alexander;3;

也可以在

阅读

https://www.ibm.com/developerworks/community/forums/html/topic?id=538340b3-3e1a-4951-bed3-48cac63e7c29&ps=25

https://www.ibm.com/developerworks/community/forums/html/threadTopic?id=3fd44d41-210b-4b81-a005-819530d6377b&ps=25

问候