簡體   English   中英

使用MySQLdb使用python打開MySQL數據庫獲取kwargs錯誤

[英]opening MySQL database with python using MySQLdb getting kwargs error

我正在嘗試使用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()

我唯一的解釋是您不小心修改了connections.py文件的源代碼。 您最有可能刪除了kwargs2 = kwargs.copy()前面的前導空格。 重新安裝mysqldb

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM