views:

66

answers:

3

How can I import an excel file into my SQL database? I have two options, MSSQL or MySQL.

Thank you

+2  A: 

You could export the excel file as a CSV and then use mysqlimport : http://dev.mysql.com/doc/refman/5.0/en/mysqlimport.html

madhurtanwani
Reading on the page, there is this direct command for importing a CSV file : mysqlimport --fields-optionally-enclosed-by=""" --fields-terminated-by=, --lines-terminated-by="\n" --user=YOUR_USERNAME --password YOUR_DATABASE YOUR_TABLE.csv
madhurtanwani
BTW, I've never used this, but MSSQL supports BULK INSERTS for imports : http://msdn.microsoft.com/en-us/library/ms188365.aspx
madhurtanwani
perfect, thanks lots!!!!!! i chose the second one, for MSSQL :)
lucifer
Which mssql server are you using? DTS provides a very simple interface to excel spreadsheet imports. I imagine ssis out of 2005 and later would have the same functionality, but I've never had the opportunity to try it
M.E.
+1  A: 

In Python it would be something like:

import MySQLdb, xlrd

def xl_to_mysql():
    book = xlrd.open_workbook('yourdata.xls') 
    to_db = []
    for sheet in book.sheets(): 
        for rowx in xrange(sheet.nrows): 
            to_db.append(tuple(sheet.cell(rowx, colx).value 
                               for colx in xrange(sheet.ncols)))

    conn = MySQLdb.connect(host="yourhost",user="username",
                           passwd="yourpassword",db="yourdb")
    curs = conn.cursor()
    # however many placeholders `%s` you need in the query below 
    curs.executemany("INSERT INTO yourtable VALUES (%s,%s,%s);", to_db)
    conn.commit()

    curs.close()
    conn.close()

if __name__ == '__main__':
   xl_to_mysql()
Adam Bernier
Thanks :) I'll keep this one in mind for when I start learning python after my current project :D
lucifer
Cheers, mate. Good on ya for challenging the old noggin :-)
Adam Bernier
+1  A: 

You can import the file as any other file.

If the question is about data from Excel then in SQL Server I would have linked Excel as a linked server, see here or here, or used OPENROWSET. There are other options like exporting/importing as XML, etc.

All options are pretty well covered on internet. What us the concrete context and/or problem?

vgv8