And a database .python
1. Connect database
Import pymysql # # 01 connect to the database # Parameter 1: mysql hostname 192.168.245.1 # Parameter 2: username # Parameter 3: password # Parameter 4: name of the connected database DB = pymysql.connect ( " localhost " , " the root " , " root " , " a " ) # 02 objects to create a cursor cursor = db.cursor () # 03 construct sql statement sql = " the SELECT Version () " # 04 executed sql statement cursor.execute (sql) # 05 get return data = Data cursor.fetchone () Print (Data) # ( '5.5.47',) MySQL version number # 06 is disconnected cursor.close () # 07 off db.Close ()
2. Create a table
Import pymysql db = pymysql.connect ( " localhost " , " root " , " root " , " db1 " ) # 02 objects to create a cursor cursor = db.cursor () # Check the table exists, if there is deleted cursor.execute ( " drop the table IF EXISTS info " ) # built table sql = ' the Create the table info (the above mentioned id int AUTO_INCREMENT Primary Key, Money int not null) ' # execute sql statement table cursor.execute (sql)
3. Insert Data
Import pymysql DB = pymysql.connect ( " localhost " , " the root " , " the root " , " A " ) # 02 to create a cursor object cursor = db.cursor () # insert data SQL = ' INSERT INTO BB values (0, 5000) ' the try : cursor.execute (SQL) the except : # If the commit fails go back to the last data db.rollback () # 06 off cursor.close () # 07 Close db.close ()
4. Update Data
Import pymysql # # 01 connect to the database # Parameter 1: mysql hostname # Parameter 2: username # Parameter 3: password # Parameter 4: name of the connected database DB = pymysql.connect ( " localhost " , " the root " , " the root " , " a " ) # 02 to create a cursor object cursor = db.cursor () # update data SQL = ' update BB SET = 6662 WHERE ID = Money. 1 ' the try : the cursor.execute (SQL) the except : # If the commit fails go back to the last data db.rollback () # 06 off cursor.close () # 07 Close db.close ()
5. Delete Data
Import pymysql # # 01 connect to the database # Parameter 1: mysql hostname # Parameter 2: username # Parameter 3: password # Parameter 4: name of the connected database DB = pymysql.connect ( " localhost " , " the root " , " the root " , " a " ) # 02 objects to create a cursor cursor = db.cursor () # delete data SQL = ' the delete from the WHERE Money bb = 6662 ' the try : cursor.execute (SQL) the except : #If the commit fails go back to the last data db.rollback () # 06 off cursor.close () # 07 Close db.close ()
6. Find data
"" " Fetchone () function: get the next result set result set is an object fetchall () function: to receive full return line rowcount: is a read-only property returns the execute () method affect the number of rows (that is, only you checked how many pieces of data) "" " Import pymysql # # 01 connect to the database # parameter 1: mysql hostname # parameter 2: username # parameter 3: password # parameter 4: name of the connected database DB = pymysql.connect ( " localhost " , " root " , " root " , " a " ) # 02 objects to create a cursor cursor = db.Cursor () # insert data sql =' SELECT * WHERE Money from CC> 400 ' the try : the cursor.execute (SQL) reslist = cursor.fetchall () # receive all return line for Row in reslist: Print (Row [0], Row [. 1 ]) # . 3 500 # 4 600 # 5 700 the except : # If the commit fails go back to the last data db.rollback () # 06 off cursor.close () # 07 Close db.close ()
7. The data package CRUD
Import pymysql class My_Sql (): DEF the __init__ (Self, Host, User, the passwd, dbName): self.host = Host self.user = User self.passwd = the passwd self.dbName = dbName DEF Connet (Self): self.db = pymysql.connect (self.host, self.user, self.passwd, self.dbName) self.cursor = self.db.cursor () DEF Close (Self): self.cursor.close () self.db.close () # fetchone () query functions: to get the next result set is a result set objects DEF get_one (Self, SQL): RES = None the try : self.connet () self.cursor.execute (SQL) RES = self.cursor.fetchone () self.close () the except : Print ( " Query failed " ) return RES # or fetchall () query function: to receive all return lines DEF get_all (Self, SQL): RES = () the try : self.connet () self.cursor.execute (SQL) RES=self.cursor.fetchall() self.close() except: print("查询失败") return res def insert(self,sql): return self._edit(sql) def update(self,sql): return self._edit(sql) def delete(self,sql): return self.__edit(sql) def __edit(self,sql): count=0 try: self.connet() count=self.cursor.execute(sql) self.db.commit() self.close() except : print("提交失败了") self.db.rollback() return count