views:

81

answers:

3

How do we populate the data into mySql windows 5.1. version?I have .csv files. I'm not allowed to use the load data command. Please help me out.

A: 

What are the tools you have? Any kind of scripting language? Any management interface, like MySQL Admin?

There are a lot of ways to load data into database. But we need to know what tools you have at hand.

EDIT

One of your options is to make a .sql batch file. The quickest to load approach would be using prepared statements. Check below how.

PREPARE massInsert 'INSERT INTO mytable (field1, field2, field3...) VALUES (?,?,?,?...)';
EXECUTE massInsert (value1, value2...);
many lines
...
Dave
use the comments feature to ask questions, not the answers feature
Tahbaza
I have to make a populate.sql file that has all tha data. And then I insert that data into sql command line by some commands given in the manual. But how do i make a populate.sql file ? Do i need to use some macros?
shilps
Shilps, check my edit
Dave
+1  A: 

Are they trying to get you to learn a scripting language?
If so, how about Python...

I just tested that the following code does what you require.
Please note: You'll need to download the 3rd-party MySQLdb module.

import csv
import MySQLdb

def populate_mysql_db():
    conn = MySQLdb.connect(host="localhost",
        user="myusername",passwd="mypassword",db="mydb")
    c = conn.cursor()
    c.execute('''
        CREATE TABLE IF NOT EXISTS mytable 
        (mycol1 varchar(100), mycol2 integer);
        ''')
    csv_reader = csv.reader(open('mycsvfile.csv', 'rb'), delimiter=',')
    for line in csv_reader:
        c.execute('''INSERT INTO mytable (mycol1, mycol2) 
            VALUES (%s, %s)''', (line[0],line[1]))
    c.close()
    conn.close()

if __name__ == "__main__":
    populate_mysql_db()
Adam Bernier
well, I dont need to use any scripting language. I have the data and I just need to make a file populatedb.sql which is a text file.The TA said to me " I'm sure you can think of some clever trick so that you don't have to actually type all of the statements manually into the populate.sql file you need to submit.In fact, once you have made the necessary populate.sql file, you can pass it to the command line .So you don't need to type everything manually into the sql command line either.
shilps
@shilps, since it's tagged "homework" and you're working with MySQL, I think we can assume that at least _some_ programming is involved? What's the class you're taking? What are your learning goals? What does your textbook say?
Alec
The text book doesnt say anything regarding this thing. It has nothing to do with my subject topics. I just need to make a file that contains all the data which is present in .csv files. I cannot put it manually because it is going to take along time. So i need some better way to solve it.
shilps
@shilps: there are many ways to accomplish your task. Pick one and get it done. Maybe you'll learn a couple of things along the way.
Adam Bernier
@shilps: "Some clever trick so that you don't have to actually type all of the statements manually" either means a program, a script, or magic.
can u plz tell me " many ways" you are talking about..
shilps
@shilps: Do you know any programming languages? Java? C? C++? PHP? Perl? Python? Visual Basic?
I know c, c++ only
shilps
A: 

Try this:

grant file on *.* to youruser@localhost identified by 'yourpassword';

Gilmar Pupo
That's not nice.