473,698 Members | 2,182 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
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 32202
ronverdonk
4,258 Recognized Expert Specialist
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<cou nt($data); $i++) {
$sql .= $data[$i].' VARCHAR(50), ';
}
$sql .= ')';
echo $sql;
fclose($handle) ;
?> [/php]Ronald
Feb 18 '08 #2
Gerald Ekosso
1 New Member
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_G erald.csv", "r");
// Read first (headers) record only)
$data = fgetcsv($handle , 1000, ",");
$sql= 'CREATE TABLE table_name (';
for($i=0;$i<cou nt($data); $i++) {
$sql .= $data[$i].' VARCHAR(50), ';
}
//The line below gets rid of the comma
$sql = substr($sql,0,s trlen($sql)-2);
$sql .= ')';
echo $sql;
fclose($handle) ;
?>
Sep 14 '10 #3
dkdenni7
1 New Member
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
5266
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
1607
by: changli_to | last post by:
Is there a build-in file or package?
1
1673
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
1750
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 (usergoups) table like as follows' insert into UserGroups(Name,modusr,moddt) values ('sam',131,'7/23/2006 10:02:13 PM')
4
3431
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 homepage. Also, when the user clicks on the description hyperlink they should see a dropdown with other hyperlinks such as hockey, baseball, etc. Here is what I have so far. I am new to asp and mysql, I would really appreciate the help. % ...
2
6602
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 select * from tablename) but when i am trying redirect output to file in Linux it shows error? can you tell me suggestion for this issue. code i used : <code> #!bin/sh password=xyzbac mysql -u root -p$password -h 192.168.1.8 << y use sys;
1
1343
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 follow-up visit i will have to keep a complete record for each patient at every visit like: visit-date, purpose, recommendations ...). So i want to have access to auotmatically create a follow up table for each patient by clicking on a button and be able to...
1
2856
by: lipton | last post by:
how can i compare a mysql table feild value with selected redio button value with php
0
8601
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9156
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9021
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7716
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4365
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4614
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3043
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1998
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.