MySQL Connectivity With Python
How do we configure python for use with mysql? After installing the MySQL-python-0.9.2.tar.gz the problem arises that the _mysql.c error that at
line 1243 that "@" undefined symbol and /usr/include/mysql.h:4:223:5"@" is not valid in if expression like all errors are out when i give the command python setup.py build.
Please help me and give some suggestion that i can install the connection in python and mysql.
Re: MySQL Connectivity With Python
You require the MySQLdb module for connecting to mysql server using the python code. This module is used for firing queries to the database server and handling of result sets from python code.First of all, check if the module is available or not :
jayant@jayantbox:~$ python
Python 2.5.2 (r252:60911, Apr 21 2008, 11:17:30)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb;
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named MySQLdb
>>>
If you are getting an importError, then the module is not installed. Download the source files from http://sourceforge.net/projects/mysql-python
Compile and install the module:
tar -xvzf MySQL-python-1.2.2.tar.gz
.
.
cd MySQL-python-1.2.2/
.
.
python setup.py build
.
.
sudo python setup.py install
.
.
Installed /usr/lib/python2.5/site-packages/MySQL_python-1.2.2-py2.5-linux-x86_64.egg
Processing dependencies for MySQL-python==1.2.2
I also faced this problem during the build process. The setup process was unable to find the mysql_config program in the path. So i did
export PATH=/usr/local/mysql/bin:$PATH
And then i ran the build process and it was successful.
Re: MySQL Connectivity With Python
Try importing the MySQLdb module in the python environment
jayant@jayantbox:/usr/share/python$ python
Python 2.5.2 (r252:60911, Apr 21 2008, 11:17:30)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib/python2.5/site-packages/PIL/__init__.py", line 19, in
File "build/bdist.linux-x86_64/egg/_mysql.py", line 7, in
File "build/bdist.linux-x86_64/egg/_mysql.py", line 6, in __bootstrap__
ImportError: libmysqlclient_r.so.16: cannot open shared object file: No such file or directory
>>>
If the library is not able to locate shared library, add the library path to the LD_LIBRARY_PATH environment variable
export LD_LIBRARY_PATH=/usr/local/mysql/lib:$LD_LIBRARY_PATH
And then try
jayant@jayantbox:~$ python
Python 2.5.2 (r252:60911, Apr 21 2008, 11:17:30)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>>
Now importing the mysqldb module has been successful.
Re: MySQL Connectivity With Python
Python comes with a bunch of different modules that allow you to add new capabilities to your Python scripts. One of the more useful ones is the MySQLdb module, which allows you to execute SQL queries on a MySQL database through your Python application.
Re: MySQL Connectivity With Python
Now if you are having issues with setting the environment variable LD_LIBRARY_PATH and you have root access then you need to export this variable in /etc/profile file. But if you do not have the root access you can simply import the variable in your local .bash_profile file.
Add the following line at the end of either file "/etc/profile" or "/home/homedir/.bash_profile"
LD_LIBRARY_PATH=/usr/local/mysql/lib:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH