In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces you to use Python to manipulate mysql database specific methods, the contents of the article are carefully selected and edited by the author, with a certain pertinence, for everyone's reference significance is still relatively great, the following with the author to understand the specific methods of using Python to manipulate mysql database.
#! / usr/bin/python#-*-coding: utf-8-*-import MySQLdbclass mysql: def _ init__ (self, sql, host='127.0.0.1', username='root', password='root', dbname='dbname') self.username = username self.password = password self.dbname = dbname self.sql = sql self.mysqldb = MySQLdb.connect (host, self.username, self.password, self.dbname Charset= "utf8") # query operation def query (self): try: cursor = self.mysqldb.cursor () cursor.execute (self.sql) data = cursor.fetchall () return data except Exception as e: print e # insert operation def insert (self): try : cursor = self.mysqldb.cursor () cursor.execute (self.sql) self.mysqldb.commit () self.mysqldb.close () return 'ok' except Exception as e: print e # delete operation def delete (self): try: cursor = self.mysqldb.cursor () Cursor.execute (self.sql) self.mysqldb.commit () self.mysqldb.close () except Exception as e: print e # modify operation def update (self): try: cursor = self.mysqldb.cursor () cursor.execute (self.sql) self.mysqldb.commit ( ) self.mysqldb.close () except Exception as e: print eif _ _ name__== "_ _ main__": passwords gaogd'import MySQLdbtry conn = MySQLdb.connect (host='localhost') User='root', passwd='root', port=3306) cur = conn.cursor () cur.execute ('create database if not exists python') conn.select_db (' python') cur.execute ('create table test (id int,info varchar (20))') value = [1,'hi rollen'] cur.execute ('insert into test values (% s), value) values = [] for i in range (20): values.append ((I) 'hi rollen' + str (I)) cur.executemany (' insert into test values (% s), values) # # key: this cur.executemany can insert multiple values cur.execute ('update test set info= "I am rollen" where id=3') conn.commit () cur.close () conn.close () except MySQLdb.Error, e: print "Mysql Error% d:% s"% (e.args [0]) E.args [1])
Common functions:
Then, this connection object also provides support for transaction operations, a standard method
Commit () submit
Rollback () rollback
The method used by cursor to execute the command:
Callproc (self,procname,args): used to execute a stored procedure. The parameters received are the stored procedure name and parameter list, and the return value is the number of affected rows.
Execute (self, query, args): executes a single sql statement. The parameters received are the sql statement itself and the list of parameters used, and the return value is the number of affected rows.
Executemany (self, query, args): executes a single sql statement, but repeats the parameters in the parameter list. The return value is the number of affected rows.
Nextset (self): move to the next result set
The method used by cursor to receive the return value:
Fetchall (self): receives all the returned result rows.
Fetchmany (self, size=None): receives the size bar and returns the result row. If the value of size is greater than the number of result rows returned, cursor.arraysize data is returned.
Fetchone (self): returns a result row.
Scroll (self, value, mode='relative'): move the pointer to a row. If mode='relative', means moving the value bar from the current row, if mode='absolute', means moving the value bar from the first row of the result set.
After reading the specific methods of using Python to manipulate mysql database, many readers must have some understanding. If you need to get more industry knowledge and information, you can continue to follow our industry information column.
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.