是否可以将SQL数据库转换为CSV文件,该文件可以作为Java脚本中的链接读取?

问题描述 投票:0回答:1

IV设法在php中编写了一个函数,此刻将我的数据库转换为从链接下载的csv文件。

$host      = "localhost";
$user      = "root";
$pass      = "";
$db_name   = "the-earth-museum";
$exp_table = "explorer_stories";

$mysqli = new mysqli($host, $user, $pass, $db_name);
$mysqli->set_charset("utf8");
if (!$mysqli)
    die("ERROR: Could not connect. " . mysqli_connect_error());

// Create and open new csv file
$csv  = $exp_table . '.csv';
$file = fopen($csv, 'w');

// Get the table
if (!$mysqli_result = mysqli_query($mysqli, "SELECT * FROM {$exp_table}"))
    printf("Error: %s\n", $mysqli->error);

// Get column names 
while ($column = mysqli_fetch_field($mysqli_result)) {
    $column_names[] = $column->name;
}

// Write column names in csv file
if (!fputcsv($file, $column_names))
    die('Can\'t write column names in csv file');

// Get table rows
while ($row = mysqli_fetch_row($mysqli_result)) {
    // Write table rows in csv files
    if (!fputcsv($file, $row))
        die('Can\'t write rows in csv file');
}

fclose($file);

echo "<a href= \"$csv\"Download\n >Download</a>";

但是,除了要下载文件外,我需要在URL位置读取链接。因此,在加载页面时,它会从链接中提取数据,而不是先下载文件。

var url =
  "data/story-data.csv";

  var template = {
  content: "<h1>{title}</h1><a href='{image}'><img src='img/Biak_Thumbnail.jpg' style='padding-right: 20px;padding-left: 20px;'></a><br><p>Discover <a href='{info}'>the objects</a>, follow <a href='{story}'>their story</a>, explore connections with people and <a href='{senseop}'>{place}</a><br><br>(Plus an additional <a href='{info}'>resource</a> you might find interesting)"
  };

有可能吗?还是更适合在javascript中编写相同的函数,然后没有交叉?

javascript php mysql arcgis-js-api
1个回答
0
投票

您可以通过sql查询将其转换,然后直接读取该文件。

SELECT *
FROM table_na,e
INTO OUTFILE "/path/to/file.csv"
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'

然后读取该文件。

如果有更多数据并且需要花费时间,请确保动态生成文件,可能会出现同步问题。多个用户可以同时执行同一操作。

© www.soinside.com 2019 - 2024. All rights reserved.