使用php从sqlite3向csv添加标头

时间:2014-01-18 10:12:25

标签: php

我是新手。我正在尝试向sqlite3数据库中的csv提取数据添加标题,但没有运气。任何帮助,将不胜感激。

<?php
$db = new sqlite3('I:\webbs.db');

$results = $db->query('select Id   ,CompanyId  ,DateTime  ,Serial  ,DeviceId  ,AgentAId  ,GpsAddress  ,Targa  ,CommonRoadDescription  ,RoadCivicNumber  ,VehicleBrandDescription  

,VehicleModelDescription  ,VerbaliVehicleTypeDescription  ,CommonColorVehicleDescription  ,VerbaliRuleOneCode  ,VerbaliRuleOneDescription  ,VerbaliRuleOnePoints  ,VerbaliClosedNoteDescription  

,Points  ,VerbaliMissedNotificationDescription  ,MissedNotificationNote  ,StatementNote  from VerbaliData');


$fp = fopen('explorer.csv', 'w');



while ($row = $results->fetchArray(SQLITE3_BOTH)) {
    fputcsv($fp, $row);
}
fclose($fp);

?>

1 个答案:

答案 0 :(得分:0)

为什么不做这样的事情?:

$headers = array('CompanyId', 'DateTime', 'Serial', ...);
results = $db->query('SELECT ' . implode(',', $headers) . ' FROM table');

$fp = fopen('explorer.csv', 'w');

// write headers
fputcsv($fp, $headers);

// write data. I would use SQLITE3_NUM. This makes more sense
while ($row = $results->fetchArray(SQLITE3_NUM)) {
    fputcsv($fp, $row);
}
fclose($fp);