How can I import an excel file into my SQL database? I have two options, MSSQL or MySQL.
Thank you
How can I import an excel file into my SQL database? I have two options, MSSQL or MySQL.
Thank you
You could export the excel file as a CSV and then use mysqlimport : http://dev.mysql.com/doc/refman/5.0/en/mysqlimport.html
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()
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?