开发者

Adding Excel Spreadsheet to SQL Database

开发者 https://www.devze.com 2023-01-20 08:05 出处:网络
How can I import an excel file into my 开发者_运维问答SQL database? I have two options, MSSQL or MySQL.

How can I import an excel file into my 开发者_运维问答SQL database? I have two options, MSSQL or MySQL.

Thank you


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 could export the excel file as a CSV and then use mysqlimport : http://dev.mysql.com/doc/refman/5.0/en/mysqlimport.html


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消