Python中的MySQL LOAD DATA LOCAL INFILE示例? [英] MySQL LOAD DATA LOCAL INFILE example in python?

查看:245
本文介绍了Python中的MySQL LOAD DATA LOCAL INFILE示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找语法定义,示例,示例代码,Wiki等. 从python执行LOAD DATA LOCAL INFILE命令.

I am looking for a syntax definition, example, sample code, wiki, etc. for executing a LOAD DATA LOCAL INFILE command from python.

如果可以的话,我相信我也可以使用mysqlimport,所以欢迎任何反馈(和代码片段),这是更好的方法. Google搜索对当前信息的影响不大

I believe I can use mysqlimport as well if that is available, so any feedback (and code snippet) on which is the better route, is welcome. A Google search is not turning up much in the way of current info

在两种情况下,目标都是相同的:使用已知的命名约定自动加载数百个文件&日期结构,放入单个MySQL表中.

The goal in either case is the same: Automate loading hundreds of files with a known naming convention & date structure, into a single MySQL table.

大卫

推荐答案

好吧,使用python的MySQLdb,我使用它:

Well, using python's MySQLdb, I use this:

connection = MySQLdb.Connect(host='**', user='**', passwd='**', db='**')
cursor = connection.cursor()
query = "LOAD DATA INFILE '/path/to/my/file' INTO TABLE sometable FIELDS TERMINATED BY ';' ENCLOSED BY '\"' ESCAPED BY '\\\\'"
cursor.execute( query )
connection.commit()

根据需要替换主机/用户/密码/数据库.这基于MySQL文档此处,确切LOAD DATA INFILE语句将取决于您的特定要求等(请注意,FIELDS TERMINATED BY,ENCLOSED BY和ESCAPED BY语句将特定于您尝试读取的文件类型).

replacing the host/user/passwd/db as appropriate for your needs. This is based on the MySQL docs here, The exact LOAD DATA INFILE statement would depend on your specific requirements etc (note the FIELDS TERMINATED BY, ENCLOSED BY, and ESCAPED BY statements will be specific to the type of file you are trying to read in).

这篇关于Python中的MySQL LOAD DATA LOCAL INFILE示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆