473,383 Members | 1,880 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

create a mySQL table from the field headings line of a CSV file

What I'm looking for is a lazy man's way to define a table structure, using the table headings row in a CSV file. I'd be happy to start with all fields being VARCHAR(50) or some such default, and then making appropriate changes via PHPmyAdmin later.

The reason for being this lazy is the number of fields that I need to define - the CSV file is downloaded from a hotel booking affiliate, and contains every conceivable detail about the hotels. If they were also using mySQL, I could have asked if they'd provide a dump instead of a CSV file...

Anyone have any bright ideas?
Dec 9 '07 #1
3 32151
ronverdonk
4,258 Expert 4TB
As follows (maybe a bit late):
[php]<?php
$handle = fopen("test.csv", "r");
// Read first (headers) record only)
$data = fgetcsv($handle, 1000, ",");
$sql= 'CREATE TABLE table_name (';
for($i=0;$i<count($data); $i++) {
$sql .= $data[$i].' VARCHAR(50), ';
}
$sql .= ')';
echo $sql;
fclose($handle);
?> [/php]Ronald
Feb 18 '08 #2
There is a small problem when you try to run the resulting query in MYSQL. That is because there is an extra comma "," resulting from the generated SQL. I got around it by adding one line more. Code below:

<?php
$handle = fopen("Master_Gerald.csv", "r");
// Read first (headers) record only)
$data = fgetcsv($handle, 1000, ",");
$sql= 'CREATE TABLE table_name (';
for($i=0;$i<count($data); $i++) {
$sql .= $data[$i].' VARCHAR(50), ';
}
//The line below gets rid of the comma
$sql = substr($sql,0,strlen($sql)-2);
$sql .= ')';
echo $sql;
fclose($handle);
?>
Sep 14 '10 #3
hi in this by default varchar , how i change data according to data ....?????
Mar 1 '17 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Fredo | last post by:
Hello, I have this .txt file : Roger|tow25$rank259 Isabelle|tow36$rank24 Pascal|tow12$rank29 Sergeď|tow45$rank5 Michel|tow1245$rank45478 Frédéric|tow1$rank125425
3
by: changli_to | last post by:
Is there a build-in file or package?
1
by: E Arredondo | last post by:
Is there a way to create a MYSQL table or database using a text file that countains the Field name and the lenght ? that is : fields.txt ----- start ---------- name,10 address,15
3
by: prabhukumarasamy | last post by:
Now i m working in a existing project. I have to do some updations. In that project database table contain a table(usergroups) field name as 'Name'. When i am trying to insert a new record in...
4
by: mramsay | last post by:
Hi, I'm having a real problem creating a dynamic hyperlink for my website. I want to pull the field name from mysql table. Field name is description. I would like this to be a hyperlink on my...
2
by: xtremebass | last post by:
Hi Bytes, in Linux , is it possible to redirect Mysql table output to a file in Linux. i tried it, shows error , but output has displayed in linux prompt when no redirection of file has given(say...
1
by: Kollypap | last post by:
Im working on a project that involves PATIENT ENROLMENT, having patient's ID as key. there is possibility of a patient to have several follow up visits after been enrolled (meanwhile for each...
1
by: lipton | last post by:
how can i compare a mysql table feild value with selected redio button value with php
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.