How to connect the MYSQL database to Python program?
ICT Ezy
ictezy at gmail.com
Fri Dec 11 13:39:05 EST 2015
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 at 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
More information about the Python-list
mailing list