Thread: Python - MySQL

Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    49
    Rep Power
    0

    Python - MySQL


    Hi all,

    I have a MySQL DB - with a table with fields

    name and age.

    I want to use Python coding to connect to MySQL

    Read the Data from table and display it using python coding.

    Wat coding is required and am i required to have any drivers.

    Let me know, How to work on this.

    thanz
  2. #2
  3. (retired)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2003
    Location
    The Laboratory
    Posts
    10,101
    Rep Power
    0
    Hi,

    You'll need to install pythons MySQLdb module, it's available at Sourceforge.net.

    There's also a brief tutorial on ASPN, which will give you some idea of how to use it.

    There's a detailed tutorial somewhere, but I can't find it right now.

    If you have any problems installing or using it, just ask

    --Simon
  4. #3
  5. (retired)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2003
    Location
    The Laboratory
    Posts
    10,101
    Rep Power
    0
    Found the good tutorial (one devshed of course : here.

    We had a rather lengthy discussion on python and MySQL last month here which is probably worth reading.

    --Simon
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    49
    Rep Power
    0

    Python - MySQL


    #!C:\Python24\Python.exe
    # import MySQL module
    import MySQLdb
    # connect
    db = MySQLdb.connect(host="localhost", user="pan",passwd="bbbc",db="test")
    # create a cursor
    cursor = db.cursor()
    # execute SQL statement
    cursor.execute("SELECT * FROM example1")
    # get the resultset as a tuple
    result = cursor.fetchall()
    # iterate through resultset
    for record in result:
    print record[0] , "-->", record[1]


    Here is My CODE

    I get an Error


    Traceback (most recent call last):
    File "C:\Documents and Settings\Arpan Shah\Desktop\Ex.py", line 5, in -toplevel-
    db = MySQLdb.connect(host="localhost", user="pan",passwd="bbbc",db="test")
    File "C:\Python24\Lib\site-packages\MySQLdb\__init__.py", line 66, in Connect
    return Connection(*args, **kwargs)
    File "C:\Python24\Lib\site-packages\MySQLdb\connections.py", line 134, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
    OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (10061)")

    Please find a solution to this, so that i can get started with DB.

    thanz
  8. #5
  9. (retired)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2003
    Location
    The Laboratory
    Posts
    10,101
    Rep Power
    0
    Is MySQL running?

    Does the user 'pan' with the password 'bbbc' have access to the database 'test'?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    49
    Rep Power
    0
    My mySQL Server is up and running,
    When i use a command line for my server : i just specify the password
    When i use a MySQL Query Browser, I can connect to the DB by just specifying the Username, Port , IP and DB , i dont require a password.

    Let me know, if there somthing wrong in the MySQL Server,

    Thanz
  12. #7
  13. (retired)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2003
    Location
    The Laboratory
    Posts
    10,101
    Rep Power
    0
    Ok, so this is running on your own computer and not a server?

    Try connecting with an empty password (passwd="")
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    49
    Rep Power
    0
    I still dont understand the problem, i get the same error.

    #!C:\Python24\Python.exe
    # import MySQL module
    import MySQLdb
    # connect
    db = MySQLdb.connect(host="localhost", user="Arpan",passwd="",db="test")
    # create a cursor
    cursor = db.cursor()
    # execute SQL statement
    cursor.execute("SELECT * FROM example1")
    # get the resultset as a tuple
    result = cursor.fetchall()
    # iterate through resultset
    for record in result:
    print record[0] , "-->", record[1]









    Traceback (most recent call last):
    File "C:\Documents and Settings\Arpan Shah\Desktop\Ex.py", line 5, in -toplevel-
    db = MySQLdb.connect(host="localhost", user="Arpan",passwd="",db="test")
    File "C:\Python24\Lib\site-packages\MySQLdb\__init__.py", line 66, in Connect
    return Connection(*args, **kwargs)
    File "C:\Python24\Lib\site-packages\MySQLdb\connections.py", line 134, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
    OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (10061)")
    >>>
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    49
    Rep Power
    0
    Hi all,

    Let me re install MySQL Server, Please send me a Link from where i can install a MySQL Server.
    Thanz
  18. #10
  19. (retired)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2003
    Location
    The Laboratory
    Posts
    10,101
    Rep Power
    0
    Ok, connect to MySQL using the command:

    c:\MySQL\bin\mysql -u root

    (or wherever you installed MySQL\bin\)

    Then run:
    GRANT ALL PRIVILEGES TO <yourname> IDENTIFIED BY '<yourpassword>';
    FLUSH PRIVILEGES;

    Exit, change the values in the connection string and try again.

    (Note: this would be a BAD idea on a server, but if this is just your local machine then I'll let it slide).
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    49
    Rep Power
    0
    Hey all,

    I have installed Python 2.3 , i need MySQL db support for 2.3 version, where can i get the download for MySQL - Python 2.3
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    49
    Rep Power
    0
    Hey Simon

    I tires the root command in command prompt it gives me the same error

    C:\Program Files\MySQL\MySQL Server 4.1\bin>mysql -u root
    ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061)

    Let me know wat to do
  24. #13
  25. (retired)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2003
    Location
    The Laboratory
    Posts
    10,101
    Rep Power
    0
    Did you set a root password when you installed?

    If you did, do this:

    mysql -u root -p
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    49
    Rep Power
    0
    New error

    mysql> GRANT ALL PRIVILEGES TO root IDENTIFIED by abcdef;
    ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
    corresponds to your MySQL server version for the right syntax to use near 'TO ro
    ot IDENTIFIED by abcdef' at line 1
    mysql> GRANT ALL PRIVILEGES TO root IDENTIFIED by 'abcdef';
    ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
    corresponds to your MySQL server version for the right syntax to use near 'TO ro
    ot IDENTIFIED by 'abcdef'' at line 1
    mysql>
  28. #15
  29. (retired)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2003
    Location
    The Laboratory
    Posts
    10,101
    Rep Power
    0
    Root will have all privileges anyway, so this isn't necessary.

    You should add a user for the db you're trying to access:
    GRANT ALL PRIVILEGES TO 'Arpan' IDENTIFIED by 'abcdef';
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo