2015-10-20 63 views
1
hostname = raw_input("Enter hostname : ") 

connection = MySQLdb.connect (host = "localhost", user = "hawkhost_databas", passwd = "}MFkdDI6]#QX", db = "hawkhost_database") 

cursor = connection.cursor() 

cursor.execute ("select User, Hostname, Password, Port from dbinfo where hostname=%s", (hostname)) 

回答

-1

线

cursor.execute ("select User, Hostname, Password, Port from dbinfo where hostname=%s", (hostname)) 

过程中被转化的所有参数是错误的,因为它是传递两个参数来cursor.execute和字符串没有被正确取代。

cursor.execute ("select User, Hostname, Password, Port from dbinfo where hostname=%s" % (hostname)) 

尝试或

cursor.execute ("select User, Hostname, Password, Port from dbinfo where hostname={}".format(hostname)) 

要解释的东西好一点,让我们打印:

hostname='example.com' 
print("select User, Hostname, Password, Port from dbinfo where hostname=%s", (hostname)) 

产生

select User, Hostname, Password, Port from dbinfo where hostname=%s example.com 

print("select User, Hostname, Password, Port from dbinfo where hostname=%s" % (hostname)) 
# print("select User, Hostname, Password, Port from dbinfo where hostname={}".format(hostname)) 

产生

select User, Hostname, Password, Port from dbinfo where hostname=example.com 

你要知道,在查询中,这将是一个好主意,放置在hostname报价,例如'example.com'

+0

文件 “./db_copy.py” ,第21行,在 cursor.execute(“选择用户,主机名,密码,端口从dbinfo其中用户=%s”%(uid)) 文件“/usr/lib/python2.6/site-packages/MySQLdb /cursors.py“,第205行,执行 self.errorhandler(self,exc,value) 文件”/usr/lib/python2.6/site-packages/MySQLdb/connections.py“,第36行,在defaulterrorhandler raise errorclass,errorvalue _mysql_exceptions.ProgrammingError:(1064,“你的SQL语法错误;检查与您的MySQL服务器版本对应的手册,以在第1行“.115.134”附近使用正确的语法) –

+0

文件“./db_copy.py”,第27行,在 data = cursor.fetchall() 文件“/usr/lib/python2.6/site-packages/MySQLdb/cursors.py”,第382行,在fetchall中 self._check_executed() 文件“/usr/lib/python2.6/site-packages/MySQLdb /cursors.py“,第105行,在_check_executed self.errorhandler(self,ProgrammingError,”execute()first“) 文件”/usr/lib/python2.6/site-packages/MySQLdb/connections.py“,第36行,在defaulterrorhandler 引发errorclass,错误值 _mysql_exceptions.ProgrammingError:执行()第一个 –

+0

所以你有一个错误的SQL合成器X。你最终使用的是什么字符串? '从dbinfo中选择用户,主机名,密码,端口,其中hostname ='example.com';'应该工作。我忽略在MySQL中是否使用大写/小写(即它是“hostname”还是“Hostname”?) – Pynchia

-1

cursor.execute( “选择用户,主机名,密码,端口从DBINFO其中主机名= '%s' 的” %d)

这解决了该问题