2012-03-12 155 views
0
import MySQLdb 
x=raw_input('user> ') 
y=raw_input('passwd> ') 
db=MySQLdb.Connect(host="localhost", user="%s", passwd="%s" % x,y) 
cursor=db.cursor() 
cursor.execute('GRANT ALL ON squidctrl.* TO [email protected] IDENTIFIED BY "connect";') 
cursor.execute('GRANT ALL ON squidlog.* TO [email protected] IDENTIFIED BY "connect";') 
cursor.close() 

问题我该如何使用它的权利,我不希望在开始脚本输入用户名和密码的正则表达式,我想给它自己如我所愿。当我尝试运行它,我把用户名和传球,这个错误MySQLdb的和蟒蛇

Traceback (most recent call last): 
    File "test.py", line 8, in <module> 
    db=MySQLdb.Connect('host="localhost", user="%s", passwd="%s"' % x,y) 
TypeError: not enough arguments for format string 

回答

0

你并不需要使用字符串插值("%s" % ...)这里经过。

import MySQLdb 
user = raw_input('user> ') 
password = raw_input('passwd> ') 
db=MySQLdb.connect(host="localhost", user=user, passwd=password) 
cursor=db.cursor() 
cursor.execute('GRANT ALL ON squidctrl.* TO [email protected] IDENTIFIED BY "connect";') 
cursor.execute('GRANT ALL ON squidlog.* TO [email protected] IDENTIFIED BY "connect";') 
cursor.close() 
+0

我改变了变量名称为'raw_input'提示过 - 如果你只是改变你的代码中的'MySQLdb.connect'线也调整变量名。 – AKX 2012-03-12 07:33:51

+0

非常感谢!它的工作 – nosensus 2012-03-12 07:34:10

+0

@ user1259251:请接受这个答案是正确的(如果可以的话),通过左侧的复选标记图标,然后在上/下行按钮下面。 :) – AKX 2012-03-12 07:37:20