In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
Editor to share with you how to add functions to the MySQL module of Python, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
The MySQLdb module is often used when using Python to operate MySQL databases. During the development process, it is found that some parameters of MySQLdb.connect cannot be set. What we can see from this page is that in connect, the option that can be set is quite different from that of client_flags and MySQLcapi. The following editor will explain how to add functions to the MySQL module of Python.
How to add function to MySQL Module of Python
A very important parameter, MYSQL_OPT_READ_TIMEOUT, cannot be set. If this parameter is not set, the extreme MySQL will live in hang, automatically switch IP drift, and the client will not be able to reconnect to the new MySQL.
It's easy to add Option to MySQLdb, just modify _ mysql.c, the file that maps Python objects to MySQL operations, add parameters, and add a mysql_option.
The following is the modified gitdiff file
Diff--gita/_mysql.cb/_mysql.cindexd42cc54..61a9b34100644---a/_mysql.c+++b/_mysql.c@@-489,9+489,10@@_mysql_ConnectionObject_Initialize ("named_pipe", "init_command", "read_default_file", "read_default_group", "client_flag", "ssl",-"local_infile", + "local_infile", "read_timeout", NULL}; intconnect_timeout=0;+intread_timeout=0;intcompress=-1,named_pipe=-1,local_infile=-1 Char*init_command=NULL,*read_default_file=NULL,@@-500,7+501,7@@_mysql_ConnectionObject_Initialize (self- > converter=NULL;self- > open=0;check_server_init (- 1) -if (! PyArg_ParseTupleAndKeywords (args,kwargs, "| ssssisOiiisssiOi:connect", + if (! PyArg_ParseTupleAndKeywords (args,kwargs, "| ssssisOiiisssiOii:connect", kwlist,&host,&user,&passwd,&db,&port,&unix_socket,&conv,@@-509,7+510,8@@_mysql_ConnectionObject_Initialize (& init_command,&read_default_file,&read_default_group,&client_flag,&ssl,-&local_infile/*DONOTPATCHFORRECONNECT,IDIOTS+&local_infile,&read_timeout+/*DONOTPATCHFORRECONNECT,IDIOTSIFYOUDOTHIS) IWILLNOTSUPPORTYOURPACKAGES.*/)) return-1 @ @-540 cinematography 650542 camera mystical connection ObjectObjective initialize (mysql_options (& (self- > connection), MYSQL_OPT_CONNECT_TIMEOUT, (char*) & timeout);} + + if (read_timeout) {+ unsignedinttimeout=read_timeout;+mysql_options (& (self- > connection), MYSQL_OPT_READ_TIMEOUT, (char*) & timeout); +} + if (roommates connection 1) {mysql_options (& (self- > connection), MYSQL_OPT_COMPRESS,0); client_flag | = CLIENT_COMPRESS
After the code has been modified, pythonsetup.pyinstall can be used, if there is a problem that mysql_config can not find. You also need to modify the setup_posix.py file.
Hoterran@hoterran-laptop:~/Projects/MySQL-python-1.2.3 $gitdiffsetup_posix.pydiff--gita/setup_posix.pyb/setup_posix.pyindex86432f5..f4f08f1100644---a/setup_posix.py+++b/setup_posix.py@@-23,7+23,7@@defmysql_config (what): ifret/256 > 1:raiseEnvironmentError ("% snotfound"% (mysql_config.path,)) returndata-mysql_config.path= "mysql_config" + mysql_config.path= "/ usr/local/mysql/bin/mysql_config" defget_config (): importos,sys
How to add function to MySQL Module of Python
The compilation passed. Let's try the parameter read_timeout that we added.
Conn=MySQLdb.connect (host=DB_SERVER,user=DB_USERNAME,passwd=DB_PASSWORD,db=DB_NAME,port=int (DB_PORT), client_flag=2,read_timeout=10)
Then before executing the statement, after you try to gdbhang the mysql for 10 seconds, the python will throw an abnormal error.
OperationalError: (2013 conn,sql conn,sql) > / home/hoterran/Projects/dbaas/trunk/dbtest.py (18) () > mydb.execute_sql (conn,sql) (Pdb)-- Return-- > / home/hoterran/Projects/dbaas/trunk/dbtest.py (18) ()-> None > mydb.execute_sql (conn,sql) (Pdb) OperationalError: (2013 None connection to MySQL server OperationalError') > (1) ()-> None is all the contents of the article "how to add functionality to SQL modules" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.