#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2016
    Posts
    4
    Rep Power
    0

    Connection to database fails


    My code :

    Code:
    #!/usr/bin/python
    import mysql.connector as mariadb
    somename=raw_input("Enter a name to query : ")
    db_connection=mariadb.connect(user='someuser',password='somepassword',database='employees')
    cursor=db_connection.cursor()
    cursor.execute("SELECT frist_name, last_name FROM employees WHERE first_name=%s",(somename))
    for first_name, last_name in cursor:
    	print "First Name : %s Last Name : %s "%(first_name, last_name)
    Error I received:

    Code:
    tc@myPythonScripts$ python databaseconnection.py
    Traceback (most recent call last):
      File "databaseconnection.py", line 2, in <module>
        import mysql.connector as mariadb
    ImportError: No module named mysql.connector
    MariaDB provides Python support through the MySQL Python package which I installed from source and compiled it manually. The link is here.

    I tried to install mysql_connector using pip but it just fails, not even sure if this is the right way of doing it.

    Code:
    tc$ sudo pip install https://cdn.mysql.com/Downloads/Connector-Python/mysql-connector-python-2.1.3.tar.gz
    
    Collecting https://cdn.mysql.com/Downloads/Connector-Python/mysql-connector-python-2.1.3.tar.gz
      Using cached https://cdn.mysql.com/Downloads/Connector-Python/mysql-connector-python-2.1.3.tar.gz
    Installing collected packages: mysql-connector-python
      Running setup.py install for mysql-connector-python: started
        Running setup.py install for mysql-connector-python: finished with status 'error'
        Complete output from command /usr/local/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-bolDee-build/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-TFfWJJ-record/install-record.txt --single-version-externally-managed --compile:
        usage: -c [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
           or: -c --help [cmd1 cmd2 ...]
           or: -c --help-commands
           or: -c cmd --help
        
        error: option --single-version-externally-managed not recognized
        
        ----------------------------------------
    I am using the following

    OS: Tiny Core Linux
    Database : MariaDB
    Other Info: Mariadb is a varient of MySQL
    The code below is taken from here.
    Editor : Geany

    Hardware Info:
    CPU : Pentium 4
    RAM : 2GB
    HDD : 160 GB

    I tried to google but nothing of consequence. Any help will be highly appreciated.

    ~gocool
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    656
    Rep Power
    39
    Try import MySQLdb first and see if that works. It looks like you are using an old tutorial. Python MySQL Database Access
    Code:
    import MySQLdb
    
    ## note the abbreviations for password and db.  These may be for convenience
    ## only, but this is what I use (unless there are typos below)
    db_connection=MySQLdb.connect(host='localhost', user='someuser',
                                                     passwd='somepassword',db='employees')
    cursor=db_connection.cursor()

    Comments on this post

    • gocool agrees : Thank you dwblas. This worked for me.
    Last edited by dwblas; May 4th, 2016 at 01:52 PM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    656
    Rep Power
    39
    If you are using Python 3.X install mysqlclient instead https://pypi.python.org/pypi/mysqlclient
    mysqlclient is a fork of MySQL-python. It adds Python 3.3~ support and merges some pull requests.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2016
    Posts
    4
    Rep Power
    0
    Originally Posted by dwblas
    Try import MySQLdb first and see if that works. It looks like you are using an old tutorial. Python MySQL Database Access
    Code:
    import MySQLdb
    
    ## note the abbreviations for password and db.  These may be for convenience
    ## only, but this is what I use (unless there are typos below)
    db_connection=MySQLdb.connect(host='localhost', user='someuser',
                                                     passwd='somepassword',db='employees')
    cursor=db_connection.cursor()

    Thank you dwblas. I will bookmark this link for future use.

    Additionally I had to do the following to get it to work:

    1. I had to include the following in the /etc/ld.so.conf file:
    /usr/local/mysql/lib
    ~/myMariadb/MySQL-python-1.2.5/build/bdist.linux-i686
    ~/myMariadb/MySQL-python-1.2.5/build/lib.linux-i686-2.7

    followed by $sudo ldconfig

    2. Since I am using Tiny Core Linux, I had to put all the library directories in the ".filetool.lst" file

    (still figuring out what all I did so that I can add it here)
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2016
    Posts
    4
    Rep Power
    0
    This is for other user's who have the same issue. Please note this is only for "Tiny Core" linux, for other distribution use your discretion.

    1. To start the mysql/mairadb during reboots add the following in the /opt/bootlocal.sh:
    Code:
    sudo /usr/local/mysql/bin/mysqld_safe &
    2. To retain the state add the following in the /opt/.filetool.lst:
    Code:
    usr/local/mysql
    usr/local/lib/python2.7
    etc/ld.so.conf
    Yes, there is no "/" at the beginning of the path.

    3. The path for all db connection related stuff is stored in /etc/ld.so.conf:
    Code:
    /usr/local/mysql/lib
    /home/tc/MySQL-python-1.2.5/build/bdist.linux-i686
    /home/tc/MySQL-python-1.2.5/build/lib.linux-i686-2.7
    4. All the path related stuff instead of ld.so.conf I thought could be entered in ./profile. But for some reason it did not work. So I just added the line to invoke the ld.so.conf in the ~/.profile, which is done by just giving the command "sudo ldconfig". In short just add the below line right after the PATH variable in ~/.profile:
    Code:
    sudo ldconfig

IMN logo majestic logo threadwatch logo seochat tools logo