View Full Version : Help converting txtdb to mysql
alvazulu
09-08-2002, 04:37 PM
I have been trying using Phpmyadmin, but no success.
The textdb is CSV .
Can anybody help?
Can you show the format of this "txtdb" please? a few rows or something.. :)
You can use phpMyAdmin to do it if the .txt, .csv file is basically a hunk of SQL like this:
CREATE TABLE `yourTableName` (
`field1` int(2) NOT NULL auto_increment,
`field2` varchar(25) NOT NULL default '',
'field3` varchar(30) NOT NULL default '',
);
INSERT INTO yourTableName ('field1', 'field2', 'field3') Values ('value1', 'value2', 'value3'); \\ one of these for every row in the database
But if it's just data like:
value1,value2,value3
value4,value5,value6
..
Then as far as i know there is no way to import this using phpMyAdmin. One way that i would use if the .csv file is like above (just the data & no sql) would be to get mySQL-front (http://www.webattack.com/get/mysqlfront.shtml) and import the .csv file into that, thus creating a database, then once you can export this database as an sql file, copy/load what's in that sql file into phpmyadmin on you web host and yippee..
mat,
You could write a scriptlet that meanders through the file and outputs to the page in a format that can be cut'n'pasted to mysql.
txt.file
arg1,arg2,arg3,arg4,arg5
brg1,brg2,brg3,brg4,brg5
etc etc
<?php
$dbtxtfile = '../txtfiles/mytextfile.txt';
$fp = fopen($dbtxtfile,"r");
while($mdb = fgetcsv ($fp, 1000, ",")) {
// the line above reads each line of the file and attributes each comma (the "," bit) seperated argument to $mdb[numeric];
echo '"INSERT INTO `tablename` (fieldname1,fieldname2,fieldname3,fieldname4,fieldname5) VALUES(\ \\''.$mdb[0].'\ \\',\ \\''.$mdb[1].'\ \\',\ \\''.$mdb[2].'\ \\',\ \\''.$mdb[3].'\ \\',\ \\''.$mdb[4].'\ \\')";<br>
';
}
fclose($fp);
?>
That should (it was flytyped so might need some cleaning to parse properly) output to the page a formatted version of the file ready for updating a database. You could just add the values straight into the database rather than outputting as text to the page if you really wanted.
note There are backslashes in the VALUES( bit ) to enable echoing out single quotes \ ''.$mdb[0].'\ ' sort of thing - the forum doesn't allow them with php syntax highlighting
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.