MySQLdb和python中的正则表达式

时间:2012-03-12 07:25:58

标签: python mysql-python

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 sams@localhost IDENTIFIED BY "connect";')
cursor.execute('GRANT ALL ON squidlog.* TO sams@localhost IDENTIFIED BY "connect";')
cursor.close()

问我怎么才能正确使用它,我不想在开头的脚本中输入用户名和密码,我想按照自己的意愿自己做。当我尝试它运行我输入用户名并通过,在那个错误后

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

1 个答案:

答案 0 :(得分: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 sams@localhost IDENTIFIED BY "connect";')
cursor.execute('GRANT ALL ON squidlog.* TO sams@localhost IDENTIFIED BY "connect";')
cursor.close()