Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How does Python call the MySQL module

Shulou Source: shulou.com Published: 2022-06-01 17:55:36 09月16日 Update

Python how to call the MySQL module, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Call MySQL

In fact, the mysql module is somewhat similar to a role of JDBC, providing an interface to access the database, by calling the corresponding interface to access and operate the database. Then this module is equivalent to a jdbc jar package, we need to make the corresponding configuration, we can download and install it in Linux.

The download link is: https://pypi.python.org/pypi/MySQL-python/

At present, the latest version is 1.2.5, we choose the source version and install it under Linux.

In fact, the installation is very simple, it is just a python setup.py install.

But there are still some problems in the actual operation.

The first is to prompt that mysql_config does not exist, which is mainly because it cannot be accessed in the environment variable, so we can configure it, such as adding soft links.

Ln-s / usr/local/mysql_5.7/bin/mysql_config / usr/bin/mysql_config

Then continue to invoke the following command.

Python setup.py install

This time the error is a bit strange, it seems that the gcc process reported an error, can not find two files.

Gcc-pthread-fno-strict-aliasing-O2-g-pipe-Wall-Wp,-D_FORTIFY_SOURCE=2-fexceptions-fstack-protector-- param=ssp-buffer-size=4-M64-mtune=generic-D_GNU_SOURCE-fPIC-fwrapv-DNDEBUG-O2-g-pipe-Wall-Wp,-D_FORTIFY_SOURCE=2-fexceptions-fstack-protector-- param=ssp-buffer-size=4-M64-mtune=generic-D_GNU_SOURCE-fPIC-fwrapv-fPIC-Dversion_info= 1)-D__version__=1.2.5-I/usr/local/mysql_5.7/include-I/usr/include/python2.6-c _ mysql.c-o build/temp.linux-x86_64-2.6/_mysql.o

_ mysql.c:29:20: error: Python.h: No such file or directory

_ mysql.c:40:26: error: structmember.h: No such file or directory

_ mysql.c:74: error: expected'=',';', 'asm' or' _ _ attribute__' before'* 'token

_ mysql.c:75: error: expected'=',';', 'asm' or' _ _ attribute__' before'* 'token

This can be solved by installing the package python-devel.

Yum install python-devel

After installation, do a basic verification to see if the module can be loaded properly.

Of course, there are still some twists and turns, and the report is wrong.

> import MySQLdb

/ usr/lib64/python2.6/site-packages/MySQL_python-1.2.5-py2.6-linux-x86_64.egg/_mysql.py:3: UserWarning: Module _ mysql was already imported from / usr/lib64/python2.6/site-packages/MySQL_python-1.2.5-py2.6-linux-x86_64.egg/_mysql.pyc, but / U01/soft1/soft/MySQL-python-1.2.5 is being added to sys.path

Traceback (most recent call last):

File "", line 1, in

File "MySQLdb/__init__.py", line 19, in

Import _ mysql

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.so.20: cannot open shared object file: No such file or directory

> > >

This error or the link can not be accessed, let's reconfigure it.

Ln-s / usr/local/mysql_5.7/lib/libmysqlclient.so.20 / usr/lib64/libmysqlclient.so.20

If you try to load the module again, it will be successful in the following way.

> import MySQLdb

> > >

The next step is to call MySQL.

Simply call a script, connect to the database and create a table, insert data, delete, and exit.

# coding=utf-8

Import MySQLdb

Conn = MySQLdb.connect (host='127.0.0.1',port=22804,user='root', passwd='',db = 'test',)

Cur = conn.cursor ()

# create a data table

Cur.execute ("create table student (id int, name varchar (20), class varchar (30), age varchar (10)")

# insert a piece of data

Cur.execute ("insert into student values ('2pm, tomatoes, tomatoes

# modify the data of query conditions

Cur.execute ("update student set class='xxxx' where name = 'Tom'")

# Delete the data of query conditions

Cur.execute ("delete from student where age='9'")

Cur.close ()

Conn.commit ()

Conn.close ()

After reading the above, have you mastered how Python calls the MySQL module? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Data module that is database or link problem configuration content interface method more condition version error query success twists and turns helpless to this end Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Docker Huawei Xiaomi Shulou Information