将 HTML 表保存到 SQL 文件的简单方法?

发布于 2024-10-07 03:02:25 字数 296 浏览 6 评论 0原文

我有一大堆数据需要插入到 mysql 数据库中。所有表格在结构上看起来都相同。是否有任何简单的方法可以将所有表行导出到 SQL 文件中,准备导入到具有相同结构的数据库表中?

听起来像是可以用 Javascript 来实现的东西,但我还不知道足够的 javascript 来做到这一点,而且我也没有太多时间来做到这一点。几天之内我需要它。

我也把它放在一个excel文件中,如果这样更容易的话。我一直在搜索,但大多数结果都是将 SQL 导出到 html(相反),我并不真正需要,其他结果也没有真正给我任何东西。

任何建议将不胜感激。

I have a big list of data I need to insert into a mysql database. All tables look the same structurally. Is there any simple ways of exporting all table rows into an SQL file ready to import to a database table with the same structure?

Sounds like something that could be achieved with Javascript, but i don't know enough javascript to do it yet, and i also don't have very much time to do it. I need it within a few days.

I also have it in an excel file if it's easier that way. I've been searching but most results are for exporting SQL to html (the other way around), which i don't really need, the other results didn't really give me anything either.

Any suggestions would be greatly appreciated.

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

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

发布评论

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

评论(3

高冷爸爸 2024-10-14 03:02:25

如果您有权访问 phpMyAdmin,则可以将 .csv 文件直接导入到 MySQL 表中(您可以在“另存为”菜单中将 Excel 文件保存为 .csv 文件)。

请参阅此处

http:// vegdave.wordpress.com/2007/05/19/import-a-csv-file-to-mysql-via-phpmyadmin/

您的表应该与 csv 文件的结构完全相同,即csv 文件的顺序。

If you have access to phpMyAdmin, you can import a .csv file directly into your MySQL table (you can save an excel file to a .csv file in the Save As menu).

See here

http://vegdave.wordpress.com/2007/05/19/import-a-csv-file-to-mysql-via-phpmyadmin/

Your table should be exacly the same structure as your csv file, i.e. all columns in the order of your csv file.

︶ ̄淡然 2024-10-14 03:02:25

您可以使用 jQuery 轻松完成此操作:

var output = [];
var temp;
var cellValues;
$('tr').each(function () {
    temp = 'INSERT INTO `mytable` VALUES (";
    cellValues = [];
    $(this).children().each(function () {
        cellValues.push('"' + $(this).text() + '"'); // <-- you may need to do escaping here
    });
    temp += cellValues.join(", ") + ");";
    output.push(temp);
});

console.log(output.join("\n"));

You could do this pretty easily with jQuery:

var output = [];
var temp;
var cellValues;
$('tr').each(function () {
    temp = 'INSERT INTO `mytable` VALUES (";
    cellValues = [];
    $(this).children().each(function () {
        cellValues.push('"' + $(this).text() + '"'); // <-- you may need to do escaping here
    });
    temp += cellValues.join(", ") + ");";
    output.push(temp);
});

console.log(output.join("\n"));
满意归宿 2024-10-14 03:02:25

如果您使用的是 Unix,则可以使用 awk 将带有 HTML 表的文件转换为 CSV。大多数数据库服务器似乎支持从 CSV 导入

编辑:

即使您不使用 Unix 或不关心 awk,您也可以在您最喜欢的文本编辑器中打开表并查找/替换

1. "<tr>" ""
2. "</tr> ""
3. "<td>" ""
4. "</td> ";"

或类似的东西

If you are using Unix, you can use awk to convert the file with HTML table to CSV. Most database servers seem to support import from CSV

EDIT:

Even if you don't use Unix or care about awk you can open the table in your favorite text editor and Find/Replace

1. "<tr>" ""
2. "</tr> ""
3. "<td>" ""
4. "</td> ";"

Or something like that

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