MySQL LOAD DATA LOCAL INFILE python 中的示例?

发布于 2024-07-30 08:41:06 字数 238 浏览 6 评论 0原文

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

我相信如果可用的话我也可以使用 mysqlimport,因此欢迎任何有关更好路线的反馈(和代码片段)。 谷歌搜索并没有以当前信息的方式出现太多这

两种情况的目标都是相同的:自动加载数百个具有已知命名约定和名称的文件。 日期结构,放入单个 MySQL 表中。

大卫

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

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

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.

David

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千里故人稀 2024-08-06 08:41:06

好吧,使用 python 的 MySQLdb,我使用这个:

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()

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

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()

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).

儭儭莪哋寶赑 2024-08-06 08:41:06

您还可以通过在查询后添加以下行来获取导入结果:

results = connection.info()

You can also get the results for the import by adding the following lines after your query:

results = connection.info()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文