473,378 Members | 1,110 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,378 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?
Aug 25 '17 #1
1 3536
Luuk
1,047 Expert 1GB
Ok , better late than never ;)

You can use a tool like gawk.exe for that.
(search Google for it, or find it here)

When you have a csv file like this ('test.csv'):
Expand|Select|Wrap|Line Numbers
  1. a,b,c
  2. 1,2,3
  3. 4,5,6
And an awkfile ('csvtosql.awk') like this:
Expand|Select|Wrap|Line Numbers
  1. FNR==1 {
  2.         s = "CREATE TABLE " FILENAME "(";
  3.         for(i=1; i<=NF; i++) {
  4.                 s = s $i " VARCHAR(50),";
  5.         }
  6.         s = substr(s,0,length(s)-1);
  7.         s = s ");"
  8.         print s;
  9. }
It's possible to do this:
Expand|Select|Wrap|Line Numbers
  1. gawk.exe -F "," -f csvtosql.awk test.csv
  2. CREATE TABLE test.csv(a VARCHAR(50),b VARCHAR(50),c VARCHAR(50));
Here the
-F "," specifies the field delimiter
-f csv2sql.awk references the awk-script
test.csv references the input file.
Oct 28 '17 #2

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?
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...
3
by: chabrenas | last post by:
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...
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: rajatagl | last post by:
Hi, I want to export one of mysql table data to CSV file using PHP. how can it be done plz tell me.
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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...

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.