- Timestamp:
- Mar 22, 2011, 12:05:48 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/db/csv_to_db.php
r8968 r8986 2 2 3 3 // edit with personal settings 4 $location = ''; 5 $username = ''; 6 $password = ''; 7 $database = ''; 8 $table = ''; 9 $file = '.csv'; 4 $location = ' '; 5 $username = ' '; 6 $password = ' '; 7 $database = ' '; 8 9 $filelist = array(); 10 $filelist = scandir("csv"); 11 sort($filelist); 12 unset($filelist[0]); 13 unset($filelist[1]); 14 unset($filelist[2]); 10 15 11 16 … … 16 21 or die("Error connecting to database: " . mysql_error()); 17 22 18 19 // create table20 mysql_query("21 CREATE TABLE IF NOT EXISTS ".$table." (22 latitude double(9,7) NOT NULL,23 longitude double(9,7) NOT NULL,24 ssid varchar(45) NOT NULL,25 mac varchar(45) NOT NULL,26 encryption tinyint(4) NOT NULL,27 ID int(11) NOT NULL AUTO_INCREMENT,28 PRIMARY KEY (ID)29 )30 ");31 23 32 24 33 // .csv file to open 34 if (($handle = fopen($file, "r")) !== FALSE) { 35 while (($data = fgetcsv($handle, 1000, "\t")) !== FALSE) { 36 $lat=str_replace("N ", "", $data[0]); 37 $lon=str_replace("E ", "", $data[1]); 38 $enc = substr($data[8], -2, 1); 39 echo "\"".$lat.", ".$lon."\", \"".$data[2]."\", \"".$data[4]."\", \"".$enc."\"<br />\n"; 40 $query=mysql_query("INSERT INTO ".$table." (latitude, longitude, ssid, mac, encryption) VALUES ('$lat','$lon','$data[2]','$data[4]','$enc')"); 41 if (!$query) { 42 die('Invalid query: ' . mysql_error()); 25 foreach($filelist as $file) { 26 27 $table = substr($file, 0, -4); 28 29 // create table 30 mysql_query(" 31 CREATE TABLE IF NOT EXISTS ".$table." ( 32 latitude double(9,7) NOT NULL, 33 longitude double(9,7) NOT NULL, 34 ssid varchar(45) NOT NULL, 35 mac varchar(45) NOT NULL, 36 encryption tinyint(4) NOT NULL, 37 ID int(11) NOT NULL AUTO_INCREMENT, 38 PRIMARY KEY (ID) 39 ) 40 "); 41 42 43 // .csv file to open 44 if (($handle = fopen("csv/".$file, "r")) !== FALSE) { 45 while (($data = fgetcsv($handle, 1000, "\t")) !== FALSE) { 46 $lat=str_replace("N ", "", $data[0]); 47 $lon=str_replace("E ", "", $data[1]); 48 $enc = substr($data[8], -2, 1); 49 echo "\"".$lat.", ".$lon."\", \"".$data[2]."\", \"".$data[4]."\", \"".$enc."\"<br />\n"; 50 $query=mysql_query("INSERT INTO ".$table." (latitude, longitude, ssid, mac, encryption) VALUES ('$lat','$lon','$data[2]','$data[4]','$enc')"); 51 if (!$query) { 52 die('Invalid query: ' . mysql_error()); 53 } 43 54 } 55 fclose($handle); 56 rename("csv/".$file, "csv/backup/".$file); 44 57 } 45 fclose($handle); 58 46 59 } 47 60
Note:
See TracChangeset
for help on using the changeset viewer.