Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #100204 > unrolled thread

How to connect the MYSQL database to Python program?

Started byICT Ezy <ictezy@gmail.com>
First post2015-12-09 09:51 -0800
Last post2015-12-11 10:39 -0800
Articles 12 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  How to connect the MYSQL database to Python program? ICT Ezy <ictezy@gmail.com> - 2015-12-09 09:51 -0800
    Re: How to connect the MYSQL database to Python program? Chris Angelico <rosuav@gmail.com> - 2015-12-10 04:57 +1100
      Re: How to connect the MYSQL database to Python program? ICT Ezy <ictezy@gmail.com> - 2015-12-11 10:36 -0800
        Re: How to connect the MYSQL database to Python program? ICT Ezy <ictezy@gmail.com> - 2015-12-11 10:39 -0800
        Re: How to connect the MYSQL database to Python program? Larry Martell <larry.martell@gmail.com> - 2015-12-11 13:51 -0500
          Re: How to connect the MYSQL database to Python program? ICT Ezy <ictezy@gmail.com> - 2015-12-11 11:00 -0800
            Re: How to connect the MYSQL database to Python program? Igor Korot <ikorot01@gmail.com> - 2015-12-11 14:11 -0500
              Re: How to connect the MYSQL database to Python program? ICT Ezy <ictezy@gmail.com> - 2015-12-11 11:13 -0800
          Re: How to connect the MYSQL database to Python program? ICT Ezy <ictezy@gmail.com> - 2015-12-11 11:11 -0800
    Re: How to connect the MYSQL database to Python program? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-12-09 21:42 +0000
      Re: How to connect the MYSQL database to Python program? ICT Ezy <ictezy@gmail.com> - 2015-12-11 10:35 -0800
      Re: How to connect the MYSQL database to Python program? ICT Ezy <ictezy@gmail.com> - 2015-12-11 10:39 -0800

#100204 — How to connect the MYSQL database to Python program?

FromICT Ezy <ictezy@gmail.com>
Date2015-12-09 09:51 -0800
SubjectHow to connect the MYSQL database to Python program?
Message-ID<a1dc235b-b02e-40c6-adb7-9c280833372b@googlegroups.com>
Pl explain me how to connect the MYSQL database to Python program?

[toc] | [next] | [standalone]


#100207

FromChris Angelico <rosuav@gmail.com>
Date2015-12-10 04:57 +1100
Message-ID<mailman.98.1449683869.12405.python-list@python.org>
In reply to#100204
On Thu, Dec 10, 2015 at 4:51 AM, ICT Ezy <ictezy@gmail.com> wrote:
> Pl explain me how to connect the MYSQL database to Python program?

You start by looking for a module that lets you do that. You can use
your favourite web search engine, or go directly to PyPI.

Then you learn how to use that module, including learning SQL if you
don't already know it.

ChrisA

[toc] | [prev] | [next] | [standalone]


#100289

FromICT Ezy <ictezy@gmail.com>
Date2015-12-11 10:36 -0800
Message-ID<9261e7b4-48ad-45f2-ba65-066177d21f6b@googlegroups.com>
In reply to#100207
On Wednesday, December 9, 2015 at 9:58:02 AM UTC-8, Chris Angelico wrote:
> On Thu, Dec 10, 2015 at 4:51 AM, ICT Ezy <ictezy@gmail.com> wrote:
> > Pl explain me how to connect the MYSQL database to Python program?
> 
> You start by looking for a module that lets you do that. You can use
> your favourite web search engine, or go directly to PyPI.
> 
> Then you learn how to use that module, including learning SQL if you
> don't already know it.
> 
> ChrisA

Now, I installed MYSQLDB and following code was done correctly. 

#!/usr/bin/python 

import MySQLdb 

# Open database connection 
db = MySQLdb.connect("localhost","TESTDB") 

# prepare a cursor object using cursor() method 
cursor = db.cursor() 

# execute SQL query using execute() method. 
cursor.execute("SELECT VERSION()") 

# Fetch a single row using fetchone() method. 
data = cursor.fetchone() 

print "Database version : %s " % data 

# disconnect from server 
db.close() 


Then done following SQL statements: 

#!/usr/bin/python 

import MySQLdb 

# Open database connection 
db = MySQLdb.connect("localhost","TESTDB" ) 

# prepare a cursor object using cursor() method 
cursor = db.cursor() 


Then not correctly work following SQL statements: 

>>> import MySQLdb 
>>> db = MySQLdb.connect("localhost","TESTDB" ) 
>>> cursor = db.cursor() 
>>> sql = """CREATE TABLE EMPLOYEE ( 
         FIRST_NAME  CHAR(20) NOT NULL, 
         LAST_NAME  CHAR(20), 
         AGE INT, 
         SEX CHAR(1), 
         INCOME FLOAT )""" 
>>> cursor.execute(sql) 

Traceback (most recent call last): 
  File "<pyshell#116>", line 1, in <module> 
    cursor.execute(sql) 
  File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 205, in execute 
    self.errorhandler(self, exc, value) 
  File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 36, in defaulterrorhandler 
    raise errorclass, errorvalue 
OperationalError: (1046, "Aucune base n'a \xe9t\xe9 s\xe9lectionn\xe9e") 
>>> 

How to solve the problems. pl explain me 

[toc] | [prev] | [next] | [standalone]


#100290

FromICT Ezy <ictezy@gmail.com>
Date2015-12-11 10:39 -0800
Message-ID<b04ff08d-6196-4391-bf3e-2f0aecf9d936@googlegroups.com>
In reply to#100289
On Friday, December 11, 2015 at 10:36:33 AM UTC-8, ICT Ezy wrote:
> On Wednesday, December 9, 2015 at 9:58:02 AM UTC-8, Chris Angelico wrote:
> > On Thu, Dec 10, 2015 at 4:51 AM, ICT Ezy <ictezy@gmail.com> wrote:
> > > Pl explain me how to connect the MYSQL database to Python program?
> > 
> > You start by looking for a module that lets you do that. You can use
> > your favourite web search engine, or go directly to PyPI.
> > 
> > Then you learn how to use that module, including learning SQL if you
> > don't already know it.
> > 
> > ChrisA
> 
> Now, I installed MYSQLDB and following code was done correctly. 
> 
> #!/usr/bin/python 
> 
> import MySQLdb 
> 
> # Open database connection 
> db = MySQLdb.connect("localhost","TESTDB") 
> 
> # prepare a cursor object using cursor() method 
> cursor = db.cursor() 
> 
> # execute SQL query using execute() method. 
> cursor.execute("SELECT VERSION()") 
> 
> # Fetch a single row using fetchone() method. 
> data = cursor.fetchone() 
> 
> print "Database version : %s " % data 
> 
> # disconnect from server 
> db.close() 
> 
> 
> Then done following SQL statements: 
> 
> #!/usr/bin/python 
> 
> import MySQLdb 
> 
> # Open database connection 
> db = MySQLdb.connect("localhost","TESTDB" ) 
> 
> # prepare a cursor object using cursor() method 
> cursor = db.cursor() 
> 
> 
> Then not correctly work following SQL statements: 
> 
> >>> import MySQLdb 
> >>> db = MySQLdb.connect("localhost","TESTDB" ) 
> >>> cursor = db.cursor() 
> >>> sql = """CREATE TABLE EMPLOYEE ( 
>          FIRST_NAME  CHAR(20) NOT NULL, 
>          LAST_NAME  CHAR(20), 
>          AGE INT, 
>          SEX CHAR(1), 
>          INCOME FLOAT )""" 
> >>> cursor.execute(sql) 
> 
> Traceback (most recent call last): 
>   File "<pyshell#116>", line 1, in <module> 
>     cursor.execute(sql) 
>   File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 205, in execute 
>     self.errorhandler(self, exc, value) 
>   File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 36, in defaulterrorhandler 
>     raise errorclass, errorvalue 
> OperationalError: (1046, "Aucune base n'a \xe9t\xe9 s\xe9lectionn\xe9e") 
> >>> 
> 
> How to solve the problems. pl explain me

I follow this link:
http://www.tutorialspoint.com/python/python_database_access.htm

[toc] | [prev] | [next] | [standalone]


#100293

FromLarry Martell <larry.martell@gmail.com>
Date2015-12-11 13:51 -0500
Message-ID<mailman.155.1449859955.12405.python-list@python.org>
In reply to#100289
On Fri, Dec 11, 2015 at 1:36 PM, ICT Ezy <ictezy@gmail.com> wrote:
> On Wednesday, December 9, 2015 at 9:58:02 AM UTC-8, Chris Angelico wrote:
>> On Thu, Dec 10, 2015 at 4:51 AM, ICT Ezy <ictezy@gmail.com> wrote:
>> > Pl explain me how to connect the MYSQL database to Python program?
>>
>> You start by looking for a module that lets you do that. You can use
>> your favourite web search engine, or go directly to PyPI.
>>
>> Then you learn how to use that module, including learning SQL if you
>> don't already know it.
>>
>> ChrisA
>
> Now, I installed MYSQLDB and following code was done correctly.
>
> #!/usr/bin/python
>
> import MySQLdb
>
> # Open database connection
> db = MySQLdb.connect("localhost","TESTDB")

The connect should look like this:

db= MySQLdb.connect(host, user, passwd, db)

Or to be clearer:

db= MySQLdb.connect(host="localhost", user="user", passwd="password",
db="TESTDB")

[toc] | [prev] | [next] | [standalone]


#100297

FromICT Ezy <ictezy@gmail.com>
Date2015-12-11 11:00 -0800
Message-ID<a31e76fc-6bf6-4a2c-a916-5e51980080ca@googlegroups.com>
In reply to#100293
On Friday, December 11, 2015 at 10:52:49 AM UTC-8, Larry....@gmail.com wrote:
> On Fri, Dec 11, 2015 at 1:36 PM, ICT Ezy <ictezy@gmail.com> wrote:
> > On Wednesday, December 9, 2015 at 9:58:02 AM UTC-8, Chris Angelico wrote:
> >> On Thu, Dec 10, 2015 at 4:51 AM, ICT Ezy <ictezy@gmail.com> wrote:
> >> > Pl explain me how to connect the MYSQL database to Python program?
> >>
> >> You start by looking for a module that lets you do that. You can use
> >> your favourite web search engine, or go directly to PyPI.
> >>
> >> Then you learn how to use that module, including learning SQL if you
> >> don't already know it.
> >>
> >> ChrisA
> >
> > Now, I installed MYSQLDB and following code was done correctly.
> >
> > #!/usr/bin/python
> >
> > import MySQLdb
> >
> > # Open database connection
> > db = MySQLdb.connect("localhost","TESTDB")
> 
> The connect should look like this:
> 
> db= MySQLdb.connect(host, user, passwd, db)
> 
> Or to be clearer:
> 
> db= MySQLdb.connect(host="localhost", user="user", passwd="password",
> db="TESTDB")

if there was error generated, i remove password and user

>>> db= MySQLdb.connect(host="localhost", user="testuser", passwd="test123",db="TESTDB")

Traceback (most recent call last):
  File "<pyshell#147>", line 1, in <module>
    db= MySQLdb.connect(host="localhost", user="testuser", passwd="test123",db="TESTDB")
  File "C:\Python27\lib\site-packages\MySQLdb\__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
  File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 193, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
OperationalError: (1045, "Acc\xe8s refus\xe9 pour l'utilisateur: 'testuser'@'@localhost' (mot de passe: OUI)")
>>> 
pl check it

[toc] | [prev] | [next] | [standalone]


#100298

FromIgor Korot <ikorot01@gmail.com>
Date2015-12-11 14:11 -0500
Message-ID<mailman.157.1449861067.12405.python-list@python.org>
In reply to#100297
 Hi,

On Fri, Dec 11, 2015 at 2:00 PM, ICT Ezy <ictezy@gmail.com> wrote:
> On Friday, December 11, 2015 at 10:52:49 AM UTC-8, Larry....@gmail.com wrote:
>> On Fri, Dec 11, 2015 at 1:36 PM, ICT Ezy <ictezy@gmail.com> wrote:
>> > On Wednesday, December 9, 2015 at 9:58:02 AM UTC-8, Chris Angelico wrote:
>> >> On Thu, Dec 10, 2015 at 4:51 AM, ICT Ezy <ictezy@gmail.com> wrote:
>> >> > Pl explain me how to connect the MYSQL database to Python program?
>> >>
>> >> You start by looking for a module that lets you do that. You can use
>> >> your favourite web search engine, or go directly to PyPI.
>> >>
>> >> Then you learn how to use that module, including learning SQL if you
>> >> don't already know it.
>> >>
>> >> ChrisA
>> >
>> > Now, I installed MYSQLDB and following code was done correctly.
>> >
>> > #!/usr/bin/python
>> >
>> > import MySQLdb
>> >
>> > # Open database connection
>> > db = MySQLdb.connect("localhost","TESTDB")
>>
>> The connect should look like this:
>>
>> db= MySQLdb.connect(host, user, passwd, db)
>>
>> Or to be clearer:
>>
>> db= MySQLdb.connect(host="localhost", user="user", passwd="password",
>> db="TESTDB")
>
> if there was error generated, i remove password and user
>
>>>> db= MySQLdb.connect(host="localhost", user="testuser", passwd="test123",db="TESTDB")
>
> Traceback (most recent call last):
>   File "<pyshell#147>", line 1, in <module>
>     db= MySQLdb.connect(host="localhost", user="testuser", passwd="test123",db="TESTDB")
>   File "C:\Python27\lib\site-packages\MySQLdb\__init__.py", line 81, in Connect
>     return Connection(*args, **kwargs)
>   File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 193, in __init__
>     super(Connection, self).__init__(*args, **kwargs2)
> OperationalError: (1045, "Acc\xe8s refus\xe9 pour l'utilisateur: 'testuser'@'@localhost' (mot de passe: OUI)")
>>>>

Is the account testuser exist? Does it have a password "test123"?
But more imp[ortantly - this does not have anything to do with Python.

Start by trying to connect from mySQL and try to execute some
insert/update/delete statement.

Then when you succeed, start writing python code.

Thank you.

> pl check it
> --
> https://mail.python.org/mailman/listinfo/python-list

[toc] | [prev] | [next] | [standalone]


#100300

FromICT Ezy <ictezy@gmail.com>
Date2015-12-11 11:13 -0800
Message-ID<a194e95a-cf45-4460-8fcc-2c4a9f3398a5@googlegroups.com>
In reply to#100298
On Friday, December 11, 2015 at 11:11:22 AM UTC-8, Igor Korot wrote:
> Hi,
> 
> On Fri, Dec 11, 2015 at 2:00 PM, ICT Ezy <ictezy@gmail.com> wrote:
> > On Friday, December 11, 2015 at 10:52:49 AM UTC-8, Larry....@gmail.com wrote:
> >> On Fri, Dec 11, 2015 at 1:36 PM, ICT Ezy <ictezy@gmail.com> wrote:
> >> > On Wednesday, December 9, 2015 at 9:58:02 AM UTC-8, Chris Angelico wrote:
> >> >> On Thu, Dec 10, 2015 at 4:51 AM, ICT Ezy <ictezy@gmail.com> wrote:
> >> >> > Pl explain me how to connect the MYSQL database to Python program?
> >> >>
> >> >> You start by looking for a module that lets you do that. You can use
> >> >> your favourite web search engine, or go directly to PyPI.
> >> >>
> >> >> Then you learn how to use that module, including learning SQL if you
> >> >> don't already know it.
> >> >>
> >> >> ChrisA
> >> >
> >> > Now, I installed MYSQLDB and following code was done correctly.
> >> >
> >> > #!/usr/bin/python
> >> >
> >> > import MySQLdb
> >> >
> >> > # Open database connection
> >> > db = MySQLdb.connect("localhost","TESTDB")
> >>
> >> The connect should look like this:
> >>
> >> db= MySQLdb.connect(host, user, passwd, db)
> >>
> >> Or to be clearer:
> >>
> >> db= MySQLdb.connect(host="localhost", user="user", passwd="password",
> >> db="TESTDB")
> >
> > if there was error generated, i remove password and user
> >
> >>>> db= MySQLdb.connect(host="localhost", user="testuser", passwd="test123",db="TESTDB")
> >
> > Traceback (most recent call last):
> >   File "<pyshell#147>", line 1, in <module>
> >     db= MySQLdb.connect(host="localhost", user="testuser", passwd="test123",db="TESTDB")
> >   File "C:\Python27\lib\site-packages\MySQLdb\__init__.py", line 81, in Connect
> >     return Connection(*args, **kwargs)
> >   File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 193, in __init__
> >     super(Connection, self).__init__(*args, **kwargs2)
> > OperationalError: (1045, "Acc\xe8s refus\xe9 pour l'utilisateur: 'testuser'@'@localhost' (mot de passe: OUI)")
> >>>>
> 
> Is the account testuser exist? Does it have a password "test123"?
> But more imp[ortantly - this does not have anything to do with Python.
> 
> Start by trying to connect from mySQL and try to execute some
> insert/update/delete statement.
> 
> Then when you succeed, start writing python code.
> 
> Thank you.
> 
> > pl check it
> > --
> > https://mail.python.org/mailman/listinfo/python-list

OK, I have done well now, misspelling occurred. Thanks

[toc] | [prev] | [next] | [standalone]


#100299

FromICT Ezy <ictezy@gmail.com>
Date2015-12-11 11:11 -0800
Message-ID<5882883a-cce3-4021-ab5b-888e87a6e2b4@googlegroups.com>
In reply to#100293
On Friday, December 11, 2015 at 10:52:49 AM UTC-8, Larry....@gmail.com wrote:
> On Fri, Dec 11, 2015 at 1:36 PM, ICT Ezy <ictezy@gmail.com> wrote:
> > On Wednesday, December 9, 2015 at 9:58:02 AM UTC-8, Chris Angelico wrote:
> >> On Thu, Dec 10, 2015 at 4:51 AM, ICT Ezy <ictezy@gmail.com> wrote:
> >> > Pl explain me how to connect the MYSQL database to Python program?
> >>
> >> You start by looking for a module that lets you do that. You can use
> >> your favourite web search engine, or go directly to PyPI.
> >>
> >> Then you learn how to use that module, including learning SQL if you
> >> don't already know it.
> >>
> >> ChrisA
> >
> > Now, I installed MYSQLDB and following code was done correctly.
> >
> > #!/usr/bin/python
> >
> > import MySQLdb
> >
> > # Open database connection
> > db = MySQLdb.connect("localhost","TESTDB")
> 
> The connect should look like this:
> 
> db= MySQLdb.connect(host, user, passwd, db)
> 
> Or to be clearer:
> 
> db= MySQLdb.connect(host="localhost", user="user", passwd="password",
> db="TESTDB")

Ok Larry, I have success now, my names are misspelling. work well

[toc] | [prev] | [next] | [standalone]


#100211

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2015-12-09 21:42 +0000
Message-ID<mailman.102.1449697508.12405.python-list@python.org>
In reply to#100204
On 09/12/2015 17:51, ICT Ezy wrote:
> Pl explain me how to connect the MYSQL database to Python program?
>

Use a search engine.  Then run up an editor, write some code, run said 
code.  If you then have problems state your OS, Python version and 
provide us with the full traceback.

An alternative is to write a cheque for (say) GBP 1000 payable to the 
Python Software Foundation and if you're lucky somebody will do your 
homework for you.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#100288

FromICT Ezy <ictezy@gmail.com>
Date2015-12-11 10:35 -0800
Message-ID<7a25bebc-cace-4b53-9961-442e50fa9e2c@googlegroups.com>
In reply to#100211
On Wednesday, December 9, 2015 at 1:45:26 PM UTC-8, Mark Lawrence wrote:
> On 09/12/2015 17:51, ICT Ezy wrote:
> > Pl explain me how to connect the MYSQL database to Python program?
> >
> 
> Use a search engine.  Then run up an editor, write some code, run said 
> code.  If you then have problems state your OS, Python version and 
> provide us with the full traceback.
> 
> An alternative is to write a cheque for (say) GBP 1000 payable to the 
> Python Software Foundation and if you're lucky somebody will do your 
> homework for you.
> 
> -- 
> My fellow Pythonistas, ask not what our language can do for you, ask
> what you can do for our language.
> 
> Mark Lawrence

Now, I installed MYSQLDB and following code was done correctly.

#!/usr/bin/python

import MySQLdb

# Open database connection
db = MySQLdb.connect("localhost","TESTDB")

# prepare a cursor object using cursor() method
cursor = db.cursor()

# execute SQL query using execute() method.
cursor.execute("SELECT VERSION()")

# Fetch a single row using fetchone() method.
data = cursor.fetchone()

print "Database version : %s " % data

# disconnect from server
db.close()


Then done following SQL statements:

#!/usr/bin/python

import MySQLdb

# Open database connection
db = MySQLdb.connect("localhost","TESTDB" )

# prepare a cursor object using cursor() method
cursor = db.cursor()


Then not correctly work following SQL statements:

>>> import MySQLdb
>>> db = MySQLdb.connect("localhost","TESTDB" )
>>> cursor = db.cursor()
>>> sql = """CREATE TABLE EMPLOYEE (
         FIRST_NAME  CHAR(20) NOT NULL,
         LAST_NAME  CHAR(20),
         AGE INT,
         SEX CHAR(1),
         INCOME FLOAT )"""
>>> cursor.execute(sql)

Traceback (most recent call last):
  File "<pyshell#116>", line 1, in <module>
    cursor.execute(sql)
  File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 205, in execute
    self.errorhandler(self, exc, value)
  File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
OperationalError: (1046, "Aucune base n'a \xe9t\xe9 s\xe9lectionn\xe9e")
>>> 

How to solve the problems. pl explain me

[toc] | [prev] | [next] | [standalone]


#100291

FromICT Ezy <ictezy@gmail.com>
Date2015-12-11 10:39 -0800
Message-ID<962016f7-71ae-4bf0-8942-43ac8a769a89@googlegroups.com>
In reply to#100211
On Wednesday, December 9, 2015 at 1:45:26 PM UTC-8, Mark Lawrence wrote:
> On 09/12/2015 17:51, ICT Ezy wrote:
> > Pl explain me how to connect the MYSQL database to Python program?
> >
> 
> Use a search engine.  Then run up an editor, write some code, run said 
> code.  If you then have problems state your OS, Python version and 
> provide us with the full traceback.
> 
> An alternative is to write a cheque for (say) GBP 1000 payable to the 
> Python Software Foundation and if you're lucky somebody will do your 
> homework for you.
> 
> -- 
> My fellow Pythonistas, ask not what our language can do for you, ask
> what you can do for our language.
> 
> Mark Lawrence

I follow this link:
http://www.tutorialspoint.com/python/python_database_access.htm

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web