【问题标题】:pywhois parsed results into mysqlpywhois 将结果解析到 mysql 中
【发布时间】:2014-07-01 06:00:16
【问题描述】:

抱歉,如果您认为是转发,代码非常简单,我怀疑这里也是一个微不足道的错误,但无法继续前进:

import whois
import MySQLdb
db = MySQLdb.connect(host="localhost", user="root",  passwd="pass", db="whois")
cur = db.cursor()
wi = whois.whois("google.com")
cur.execute("""INSERT INTO wrec (dname, wfull, dns) VALUES (%s, %s, %s)""") , (wi.domain_name, wi.text, wi.name_servers)

结束于:

_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s, %s, %s)' at line 1")

如前所述,怀疑是微不足道的错误。有什么建议?提前非常感谢

【问题讨论】:

  • 这是一个字符串插值问题。 %s 不会被变量替换。

标签: python mysql-python pywhois


【解决方案1】:

您将获取的 Whois 变量放在了执行函数之外!

变化:

cur.execute("""INSERT INTO wrec (dname, wfull, dns) VALUES (%s, %s, %s)""") , (wi.domain_name, wi.text, wi.name_servers)

收件人:

cur.execute("""INSERT INTO wrec (dname, wfull, dns) VALUES (%s, %s, %s)""", (wi.domain_name, wi.text, wi.name_servers))

编辑:

别忘了补充:

db.commit()

在脚本的末尾。

【讨论】:

  • 非常感谢,这行得通,但是我遇到了另一个 mysql 错误。再次感谢
  • 只是为了补全代码,下一个错误是关于格式化的。我发现在插入 mysql 之前,我需要使用 str() 转换变量。那么它看起来像这个 import whois import MySQLdb db = MySQLdb.connect(host="localhost", user="root", passwd="pass", db="whois") cur = db.cursor() wi = whois。 whois("google.com") domname = str(wi.domain_name) fullwhois = str(wi.text) dnsserv = str(wi.name_servers) cur.execute("""INSERT INTO wrec (dname, wfull, dns) 值(%s, %s, %s)""", (domname, fullwhois, dnsserv))
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-28
  • 2013-05-22
  • 1970-01-01
  • 2017-11-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多