qwerty
qwerty

Reputation: 5256

Simple way to save HTML table to SQL file?

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.

Upvotes: 0

Views: 9524

Answers (3)

Goran Jovic
Goran Jovic

Reputation: 9518

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

Upvotes: 0

Nick Pyett
Nick Pyett

Reputation: 3408

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.

Upvotes: 2

nickf
nickf

Reputation: 546433

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"));

Upvotes: 1

Related Questions