我应该使用复杂的多维数组还是创建一个对象?

发布于 2024-12-15 01:55:30 字数 956 浏览 0 评论 0原文

我正在编写一个脚本将 csv 导入 MySQL。 csv 将具有可变数量的字段、不同的数据类型和字段大小。

我当前的计划是遍历 csv 并收集其统计信息以通知 MySQL CREATE TABLE 查询。

我的想法是以这种格式创建一个数组:

$csv_table_data = array(
  ['columns'] => array(
    [0] => array(
      ['min_size'] = int,
      ['max_size'] = int,
      ['average'] = int
      ['type'] = string
    ),
    [1] => array(
      ['min_size'] = int,
      ['max_size'] = int,
      ['average'] = int
      ['type'] = string
    ),
    [2] => array(
      ['min_size'] = int,
      ['max_size'] = int,
      ['average'] = int
      ['type'] = string
    ),
  ),
  ['some_other_data'] = array()
);

因此,通过访问 $csv_table_data['columns'][$i] 我可以访问每列的属性并使用它来创建 MySQL 表。除了 $csv_table_data['columns'] 之外,我还有其他数据,例如总行数、字段数,并且可能会添加更多数据。

我可以在一次传递中收集所有这些数据,创建适当的表,然后在第二次传递中填充它。

我没有使用太多面向对象的编程,但是对于所有这些数据,我是否应该考虑创建具有这些不同属性的对象,而不是创建这个复杂的数组?

在可读性、可维护性、执行速度以及您想到的任何其他考虑因素方面,您如何看待它?

谢谢

I'm writing a script to import csv's into MySQL. The csv's will have variable numbers of fields, different data types and field sizes.

My current plan is to do a pass through the csv and collect statistical information on it to inform the MySQL CREATE TABLE query.

My conception was to create an array in this format:

$csv_table_data = array(
  ['columns'] => array(
    [0] => array(
      ['min_size'] = int,
      ['max_size'] = int,
      ['average'] = int
      ['type'] = string
    ),
    [1] => array(
      ['min_size'] = int,
      ['max_size'] = int,
      ['average'] = int
      ['type'] = string
    ),
    [2] => array(
      ['min_size'] = int,
      ['max_size'] = int,
      ['average'] = int
      ['type'] = string
    ),
  ),
  ['some_other_data'] = array()
);

So, by accessing $csv_table_data['columns'][$i] I can access each column's attributes and use that to create the MySQL table. Besides $csv_table_data['columns'] I also have other data like total_rows, number_of_fields, and may add more.

I can gather all this data in one pass, create the appropriate table, and then populate it in a second pass.

I haven't used much object-oriented programming, but with all this data should I consider creating an object with these various properties, rather than creating this complex array?

What do you think about it in terms of readability, maintainability, speed of execution, and any other considerations that occur to you?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天暗了我发光 2024-12-22 01:55:30

我认为你应该使用类,而不仅仅是一个大对象。也许你把它分成 2 或 3 个班级。它比仅数组要干净得多。

像这样的东西

class Table{

private $data = array();
private $otherData = 'what_ever';

public function getData(){
 return $this->data;
}

public function addData(Row $row){
 $this->data[] = $row;
}

//Add getter and setter


}

class Row{

private $min_size;
private $max_size;
private $avg_size;
private $type;

public function setMinSize($minSize){
$this->min_size = $minSize;
}

public function getMinSize(){
 return $this->min_size;
}


//Add more getter and setter

}

I think you should use Classes, not only one big Object. Maybe you split it up to 2 or 3 Classes. It's much more cleaner as only arrays.

Something like this

class Table{

private $data = array();
private $otherData = 'what_ever';

public function getData(){
 return $this->data;
}

public function addData(Row $row){
 $this->data[] = $row;
}

//Add getter and setter


}

class Row{

private $min_size;
private $max_size;
private $avg_size;
private $type;

public function setMinSize($minSize){
$this->min_size = $minSize;
}

public function getMinSize(){
 return $this->min_size;
}


//Add more getter and setter

}
橪书 2024-12-22 01:55:30

如果要插入的文件数量有限,可以使用 MySql 的内部函数。
要导入数据文件,首先将其上传到您的主目录,以便该文件现在位于本地系统上的 /importfile.csv。然后在 mysql 提示符下键入以下 SQL:
加载数据本地内文件'/importfile.csv'
INTO TABLE 测试表
以“,”结尾的字段
以 '\n' 结尾的行
(字段1、字段2、字段3);

上面的 SQL 语句告诉 MySQL 服务器在 LOCAL 文件系统上查找 INFILE,将文件的每一行作为单独的行读取,将任何逗号字符视为列分隔符,并将其作为列 field1 放入 MySQL test_table 中、 field2 和 field3 分别。上述许多 SQL 子句都是可选的,您应该阅读 MySQL 文档以了解如何正确使用此语句。

另一方面,如果您有很多文件,或者您希望允许用户上传 csv,然后您又将它们添加到数据库中,我建议使用“数组”版本,因为它似乎更容易遍历。

干杯,
丹尼斯

if you have a limited number of files you want to insert you can use MySql's internal functions.
To import the datafile, first upload it to your home directory, so that the file is now located at /importfile.csv on our local system. Then you type the following SQL at the mysql prompt:
LOAD DATA LOCAL INFILE '/importfile.csv'
INTO TABLE test_table
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
(field1, filed2, field3);

The above SQL statement tells the MySQL server to find your INFILE on the LOCAL filesystem, to read each line of the file as a separate row, to treat any comma character as a column delimiter, and to put it into your MySQL test_table as columns field1, field2, and field3 respectively. Many of the above SQL clauses are optional and you should read the MySQL documentation on the proper use of this statement.

On the other hand if you have to many files or you want to allow users to upload csv which you then in turn add them to the DB, I recommend using the 'array' version as it seems simpler to traverse.

Cheers,
Denis

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文