0

我正在尝试使用 mysqldb 使用 python 打开本地 mysql 数据库并收到此错误:

  File "c:\python27\lib\site-packages\MySQLdb\connections.py", line 56, in <module>
class Connection(_mysql.connection):
  File "c:\python27\lib\site-packages\MySQLdb\connections.py", line 154, in Connection
kwargs2 = kwargs.copy()
NameError: name 'kwargs' is not defined
2013-02-17 14:51:37 (Process exited with code 1)

这是 mysqldb 调用的 connection.py 模块中的 Connection 类的代码。第 154 行的错误是此代码的最后一行。

class Connection(_mysql.connection):

"""MySQL Database Connection Object"""

default_cursor = cursors.Cursor

def __init__(self, *args, **kwargs):
    connection = MySQLdb.connect (host = "127.0.0.1", user = "root", passwd = "", db = "test")

"""

Create a connection to the database. It is strongly recommended
that you only use keyword parameters. Consult the MySQL C API
documentation for more information.

host
  string, host to connect

user
  string, user to connect as

passwd
  string, password to use

db
  string, database to use

port
  integer, TCP/IP port to connect to

unix_socket
  string, location of unix_socket to use

conv
  conversion dictionary, see MySQLdb.converters

connect_timeout
  number of seconds to wait before the connection attempt
  fails.

compress
  if set, compression is enabled

named_pipe
  if set, a named pipe is used to connect (Windows only)

init_command
  command which is run once the connection is created

read_default_file
  file from which default client values are read

read_default_group
  configuration group to use from the default file

cursorclass
  class object, used to create cursors (keyword only)

use_unicode
  If True, text-like columns are returned as unicode objects
  using the connection's character set.  Otherwise, text-like
  columns are returned as strings.  columns are returned as
  normal strings. Unicode objects will always be encoded to
  the connection's character set regardless of this setting.

charset
  If supplied, the connection character set will be changed
  to this character set (MySQL-4.1 and newer). This implies
  use_unicode=True.

sql_mode
  If supplied, the session SQL mode will be changed to this
  setting (MySQL-4.1 and newer). For more details and legal
  values, see the MySQL documentation.

client_flag
  integer, flags to use or 0
  (see MySQL docs or constants/CLIENTS.py)

ssl
  dictionary or mapping, contains SSL connection parameters;
  see the MySQL documentation for more details
  (mysql_ssl_set()).  If this is set, and the client does not
  support SSL, NotSupportedError will be raised.

local_infile
  integer, non-zero enables LOAD LOCAL INFILE; zero disables

There are a number of undocumented, non-standard methods. See the
documentation for the MySQL C API for some hints on what they do.

"""

from MySQLdb.constants import CLIENT, FIELD_TYPE
from MySQLdb.converters import conversions
from weakref import proxy, WeakValueDictionary

import types

kwargs2 = kwargs.copy()

感谢您的帮助

这是我来自 helloworld.py 的代码,此代码可以在 google appengine 上运行,但不能在本地 mysql 上运行。我在 connection.py 中更改的唯一行是我添加了连接字符串:

    connection = MySQLdb.connect (host = "127.0.0.1", user = "root", passwd = "", db = "test")

在第 8 行。

import cgi
import datetime
import urllib
import webapp2
from google.appengine.api import rdbms
from google.appengine.ext import db
from google.appengine.api import users
import jinja2
import os

_INSTANCE_NAME = 'ceemee111:ceemee111'

class MainPage(webapp2.RequestHandler):
def get(self):
    conn = rdbms.connect(instance=_INSTANCE_NAME, database='test')
    cursor = conn.cursor()
    cursor.execute('SELECT guest_name, content, ID FROM entries')
    self.response.out.write("""
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
        <head>
           <title>My Guestbook!</title>
        </head>
        <body>
          <table style="border: 1px solid black">
            <tbody>
              <tr>
                <th width="35%" style="background-color: #CCFFCC; margin: 5px">Name</th>
                <th style="background-color: #CCFFCC; margin: 5px">Message</th>
                <th style="background-color: #CCFFCC; margin: 5px">ID</th>
              </tr>""")
    for row in cursor.fetchall():
      self.response.out.write('<tr><td>')
      self.response.out.write((row[0]))
      self.response.out.write('</td><td>')
      self.response.out.write((row[1]))
      self.response.out.write('</td><td>')
      self.response.out.write(row[2])
      self.response.out.write('</td></tr>')

    self.response.out.write("""
      </tbody>
        </table>
          <br /> No more messages! 
          <br /><strong>Sign the guestbook!</strong>
          <form action="/sign" method="post">
          <div>First Name: <input type="text" name="fname" style="border: 1px solid black"></div>
          <div>Message: <br /><textarea name="content" rows="3" cols="60"></textarea></div>
          <div><input type="submit" value="Sign Guestbook"></div>
        </form>
      </body>
    </html>""")
    conn.close()

class Guestbook(webapp2.RequestHandler):
def post(self):
    fname = self.request.get('fname')
    content = self.request.get('content')
    conn = rdbms.connect(instance=_INSTANCE_NAME, database='guestbook')
    cursor = conn.cursor()
    # Note that the only format string supported is %s
    cursor.execute('INSERT INTO entries (guest_name, content) VALUES (%s, %s)', (fname, content))
    conn.commit()
    conn.close()

    self.redirect("/")

app = webapp2.WSGIApplication([('/', MainPage),
                           ('/sign', Guestbook)],
                          debug=True)

if __name__ == "__main__":
    main()
4

1 回答 1

0

我唯一的解释是你不小心修改了connections.py文件的源代码。很可能您删除了 . 前面的前导空格kwargs2 = kwargs.copy()。重新安装mysqldb

于 2013-02-18T05:02:53.770 回答