Hoe importeer ik een flat-file database in MySQL?

U kan een conversieprogramma gebruiken dat gebaseerd is op het volgende voorbeeld in PHP. Vergeet niet om gebruikersnamen, databases, wachtwoorden, veldnamen en delimiter aan te passen aan uw specifieke situatie vooraleer u het script draait.

#!/usr/bin/php

<? /* Beginning of Script */ /*--------------------------------*/ /* Set You Database Variables Here*/ /*--------------------------------*/ $database="your_database0"; $dbhost="localhost"; $user="your_user_name"; $password="your_password"; echo "<h3>Converting Flat File DBase to MySQL</h3>";

/*---------------------*/ /* Connect to Database */ /*---------------------*/ mysql_connect($dbhost,$user,$password) or die( "Unable to select database"); mysql_select_db($database);

/*----------------------------------------------------------*/ /* Setup Query to Create Table with same fields as */ /* Flat Dbase */ /*----------------------------------------------------------*/

$query = "CREATE TABLE test ( name varchar(20), email varchar(30), phone_number varchar(20), ID INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (ID));"; /*-----------------------------*/ /* Create Table with fields */ /*-----------------------------*/ $result= MYSQL_QUERY($query);

/*---------------------------------------*/ /* Open flat file with "r" for read only */ /*---------------------------------------*/ $fp = fopen("your_flatfile.txt", "r");

/*-------------------------------------------*/ /* Convert flat file to MySQL Database table */ /*-------------------------------------------*/ while (!feof($fp)){ /* While not the end of the flat file */

$line = fgets($fp,1800); /* Get the next line */

print "$line <br>";

$line_list = explode("|",$line); /* Assuming the fields are separated */ /* by a "|", make an array of all of */ /* the fields in $line */

/*--------------------------------*/ /* Assign array list to variables */ /*--------------------------------*/ $name = $line_list[0]; /* String */ $email = $line_list[1]; /* String */ $phone = $line_list[2]; /* String */ $ID = $line_list[3]; /* Number */

print "$name|$email|$phone|$ID <br>";

/*---------------------*/ /* Add Record to Table */ /*---------------------*/ $query = "INSERT INTO test4 VALUES('$name','$email','$phone', $ID);"; /* Notice in the VALUES that strings */ /* need the single quotes around the */ /* variable and the numbers do not. */

/*---------------*/ /* Run the query */ /*---------------*/ $result= MYSQL_QUERY($query); } /*-----------------------------*/ /* Close the flat file and the */ /* MySQL Database connection */ /*-----------------------------*/ $closefp = fclose($fp); MYSQL_CLOSE();

echo "Done!";

?>


Sluit venster      Suggesties

©2024. Mag niet worden overgenomen zonder toestemming van One2Web.