REMitchell/python-scraping

Chapter 05 mySQLBasicExample

schulenj opened this issue · 4 comments

Thanks for writing this book. As a non-programmer it has been a fun project working through the various examples. I've run into a wall trying to run the mySQLBasic Example. Running in Windows 7. mySQL installed. PyMySQL installed. The mySQL command prompt is open. Copied the subject code into notepad and saved as mySQL01.py. Running results in the following

image

I have tried resetting my root password based on instructions here: http://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html but have been unsuccessful. It is seems that there are two possible passwords, the one set during install and ''. To enter mysql I just hit enter, so it would seem that the password is ''.

What am I missing here?

Thanks!

Would you like to try "conn = pymysql.connect(host='127.0.0.1', user='root', passwd='yourpassword', db='mysql')" to see whether it works?

Use help(pymysql.connect) can see why

Help on function Connect in module pymysql:

Connect(*args, **kwargs)
    Establish a connection to the MySQL database. Accepts several
    arguments:
    
    host: Host where the database server is located
    user: Username to log in as
    password: Password to use.
    database: Database to use, None to not use a particular one.
    port: MySQL port to use, default is usually OK. (default: 3306)
    bind_address: When the client has multiple network interfaces, specify
        the interface from which to connect to the host. Argument can be
        a hostname or an IP address.
    unix_socket: Optionally, you can use a unix socket rather than TCP/IP.
    charset: Charset you want to use.
    sql_mode: Default SQL_MODE to use.
    read_default_file:
        Specifies  my.cnf file to read these parameters from under the [client] section.
    conv:
        Conversion dictionary to use instead of the default one.
        This is used to provide custom marshalling and unmarshaling of types.
        See converters.
    use_unicode:
        Whether or not to default to unicode strings.
        This option defaults to true for Py3k.
    client_flag: Custom flags to send to MySQL. Find potential values in constants.CLIENT.
    cursorclass: Custom cursor class to use.
    init_command: Initial SQL statement to run when connection is established.
    connect_timeout: Timeout before throwing an exception when connecting.
        (default: 10, min: 1, max: 31536000)
    ssl:
        A dict of arguments similar to mysql_ssl_set()'s parameters.
        For now the capath and cipher arguments are not supported.
    read_default_group: Group to read from in the configuration file.
    compress; Not supported
    named_pipe: Not supported
    autocommit: Autocommit mode. None means use server default. (default: False)
    local_infile: Boolean to enable the use of LOAD DATA LOCAL command. (default: False)
    max_allowed_packet: Max size of packet sent to server in bytes. (default: 16MB)
        Only used to limit size of "LOAD LOCAL INFILE" data packet smaller than default (16KB).
    defer_connect: Don't explicitly connect on contruction - wait for connect call.
        (default: False)
    auth_plugin_map: A dict of plugin names to a class that processes that plugin.
        The class will take the Connection object as the argument to the constructor.
        The class needs an authenticate method taking an authentication packet as
        an argument.  For the dialog plugin, a prompt(echo, prompt) method can be used
        (if no authenticate method) for returning a string from the user. (experimental)
    db: Alias for database. (for compatibility to MySQLdb)
    passwd: Alias for password. (for compatibility to MySQLdb)
        """

I recommend this code to try to run with the [SQLite3] (https://goo.gl/Xa8B7f)

This code works with version 0.7.11 of pymysql:

import pymysql

conn = pymysql.connect(host='127.0.0.1',
                       user='root',
                       password='password',
                       db='mysql')
cur = conn.cursor()
cur.execute("USE scraping")
cur.execute("SELECT * FROM pages WHERE id=1")
print(cur.fetchone())
cur.close()
conn.close()