我正在使用pymysql拉取数据库表结果,如下所述,一个表包含非fqdn的列表,另一个表包含非fqdn的列表。从本质上讲,我希望验证在完整的fqdn表中是否存在这些部分-如果存在,则执行一次DB插入。任何帮助都将不胜感激。如下所示:
#!/usr/bin/python
'''Produce list of machines that are postivie matches for rhn and vmware member\
ship'''
import os
import pymysql
import glob
conn = pymysql.connect(host='10.0.0.101', port=3306, user='root', passwd='', db\
='hosts')
cur = conn.cursor()
cur.execute("SELECT hostname from rhnreg")
rhnreg = []
for r in cur.fetchall():
rhnreg.append(r)
cur.close
cur = conn.cursor()
cur.execute("SELECT hostname from metalreg")
mreg = []
for r in cur.fetchall():
mreg.append(r)
cur.close
conn.close发布于 2014-10-18 09:00:09
默认情况下,MySQL支持字符串模式匹配和查询,因此您应该使用它(有关这些的更多信息,请参阅http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html和http://dev.mysql.com/doc/refman/5.0/en/pattern-matching.html )
这将导致如下所示的结果
SELECT hostname FROM fullyqualifiedhostnames where hostname LIKE "%non-qualified-hostname%";除此之外,请确保您实际调用了close函数(在上面的示例中,您只“引用”了实际的函数,而不是调用它们。Python对调用有严格的括号(例如cur.close()和conn.close())
最有可能的是,如果你想在python中完成这个任务,你可以使用正则表达式(https://docs.python.org/2/library/re.html)。另一种可能的选择是只使用fnmatch内置库,它将为您提供与UNIX类似的文件通配符,这可能足以满足您的用例(https://docs.python.org/2/library/fnmatch.html)
https://stackoverflow.com/questions/26435304
复制相似问题