处理的MySQL结果在bash [英] Processing MySQL result in bash

查看:113
本文介绍了处理的MySQL结果在bash的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前具有几千行,它发送各种查询MySQL来产生穆宁适用的输出已经是bash脚本。

I'm currently having a already a bash script with a few thousand lines which sends various queries MySQL to generate applicable output for munin.

截至目前为止的结果只是这不是一个数字的问题,但我现在面临的一个挑战的形式有更复杂的查询工作:

Up until now the results were simply numbers which weren't a problem, but now I'm facing a challenge to work with a more complex query in the form of:

$ echo "SELECT id, name FROM type ORDER BY sort" | mysql test
id      name
2       Name1
1       Name2
3       Name3

从该结果我需要存储的ID和名称(以及它们各自的关联)和基于所述标识需要执行进一步查询,例如 SELECT COUNT(*)FROM somedata其中type = 2 后来输出的结果与相关配对名称从第一个结果列。

From this result I need to store the id and name (and their respective association) and based on the IDs need to perform further queries, e.g. SELECT COUNT(*) FROM somedata WHERE type = 2 and later output that result paired with the associated name column from the first result.

我会很容易地知道该怎么做,在PHP / Ruby的,但我想腾出叉特别是因为它是定期轮询另一个进程,但我完全失去了在那里使用bash开始。

I'd know easily how to do it in PHP/Ruby , but I'd like to spare to fork another process especially since it's polled regularly, but I'm complete lost where to start with bash.

也许使用bash是错误的做法无论如何,我应该只是掏钱?

Maybe using bash is the wrong approach anyway and I should just fork out?

我用 GNU bash的版本3.2.39(1) - 释放(的i486-PC-Linux的GNU)

推荐答案

您打算掏钱,因为你把它,到mysql命令行客户端程序进不去。所以,无论哪种方式,你将不得不进程创建开销。随着你使用一个新的MySQL调用为每个查询你也将招致连接和认证到mysqld服务器多次成本的方法。这是昂贵的,但如果这个程序不能按比例增长为代价可能并不重要。

You're going to "fork out," as you put it, to the mysql command line client program anyhow. So either way you're going to have process-creation overhead. With your approach of using a new invocation of mysql for each query you're also going to incur the cost of connecting to and authenticating to the mysqld server multiple times. That's expensive, but the expense may not matter if this app doesn't scale up.

使之成为抵御SQL注入是另一回事。如果您提示输入她的名字,用户和她回答,萨莉;删除表的类型;她的笑,你就完了。

Making it secure against sql injection is another matter. If you prompt a user for her name and she answers "sally;drop table type;" she's laughing and you're screwed.

您可能是明智的使用是在那些对你的一些逻辑的资料库访问重要的方面更前pressive的语言。红宝石,PHP,PERL都是不错的选择。 PERL情况进行调整和设计,以shell脚本控制下没好气运行。

You might be wise to use a language that's more expressive in the areas that are important for data-base access for some of your logic. Ruby, PHP, PERL are all good choices. PERL happens to be tuned and designed to run snappily under shell script control.

这篇关于处理的MySQL结果在bash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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