Environment configuration 1: Install MySQL and add the bin directory of MySQL to the environment variable Environment configuration 2: Python installation MySQL-Python Please download and install according to your own operating system, otherwise errors such as c++ compile 9.0, import _mysql, etc. will be reported For Windows 10 64-bit operating system, you can go to http://www.lfd.uci.edu/~gohlke/pythonlibs/ to download and install the MySQL-Python package. For the installation methods of whl and tar.gz under Windows and Linux, please refer to my previous article 1. Operations under cmd command: Connect to mysql:mysql -u root -p View all databases: show databases; Create a test database: create database test; Delete database: drop database test; Use (switch to) the test database: use test; View the tables under the current database: show tables; Create a UserInfo table: create table UserInfo(id int(5) NOT NULL auto_increment, username varchar(10), password varchar(20) NOT NULL, PRIMARY KEY(id)); Delete table: drop table UserInfo; Determine whether the data exists: select * from UserInfo where name like 'elijahxb'; Add data: insert into UserInfo(username,password) value('eljiahxb','123456'); Check data: select * from UserInfo; select id from UserInfo; select username from UserInfo; Change data: update UserInfo set username = 'Zus' where id=1; update UserInfo set username='Zus'; Delete data: delete from UserInfo; delete from UserInfo where id=1; Disconnect: quit 2. Operations under Python: # -*- coding: utf-8 -*- #!/usr/bin/env python # @Time : 2017/6/4 18:11 # @Author : Elijah # @Site : # @File : sql_helper.py # @Software: PyCharm Community Edition import MySQLdb class MySqlHelper(object): def __init__(self, **args): self.ip = args.get("IP") self.user = args.get("User") self.password = args.get("Password") self.tablename = args.get("Table") self.port = 3306 self.conn = self.conn = MySQLdb.Connect(host=self.ip,user=self.user,passwd=self.password,port=self.port,connect_timeout=5,autocommit=True) self.cursor = self.conn.cursor() def Close(self): self.cursor.close() self.conn.close() def execute(self,sqlcmd): return self.cursor.execute(sqlcmd) def SetDatabase(self,database): return self.cursor.execute("use %s;"%database) def GetDatabasesCount(self): return self.cursor.execute("show databases;") def GetTablesCount(self): return self.cursor.execute("show tables;") def GetFetchone(self, table = None): if not table: table = self.tablename self.cursor.execute("select * from %s;"%table) return self.cursor.fetchone() def GetFetchmany(self,table=None,size=0): if not table: table = self.tablename count = self.cursor.execute("select * from %s;"%table) return self.cursor.fetchmany(size) def GetFetchall(self, table=None): ''' :param table: list :return: ''' if not table: table = self.tablename self.cursor.execute("select * from %s;"%table) return self.cursor.fetchall() def SetInsertdata(self,table=None,keyinfo=None,value=None): """ :param table: :param keyinfo: This parameter can be omitted, but the number of fields in each value data item must be consistent with the number of fields in the database. When this parameter is passed, it means only the field value of the specified field is displayed. :param value: The type must be a tuple containing only one set of information, or a list of tuples containing multiple pieces of information:return: """ if not table: table = self.tablename slist = [] if type(value)==tuple: valuelen = value execmany = False else: valuelen = value[0] execmany = True for each in range(len(valuelen)): slist.append("%s") valuecenter = ",".join(slist) if not keyinfo: sqlcmd = "insert into %s values(%s);"%(table,valuecenter) else: sqlcmd = "insert into %s%s values(%s);" % (table,keyinfo,valuecenter) print(sqlcmd) print(value) if execmany: return self.cursor.executemany(sqlcmd,value) else: return self.cursor.execute(sqlcmd, value) The above brief discussion on the common operations of MySQL under cmd and python is all the content that the editor shares with you. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. You may also be interested in:
|
<<: JavaScript implements the protocol example in which the user must check the box
>>: Linux kernel device driver advanced character device driver notes
Thanks to the development of the Internet, we can...
In MySQL, you can use the REVOKE statement to rem...
Table of contents Preface What is index pushdown?...
Background In Docker, four containers are created...
Recently I found that even if the TD of the table ...
When to install If you use the protoc command and...
To display the JSON data in a beautiful indented ...
Effect picture (if you want a triangle, please cl...
Preface There are two types of nginx modules, off...
Our network management center serves as the manag...
use Flexible boxes play a vital role in front-end...
You can manage and deploy Docker containers in a ...
Table of contents Preface Mysql case when syntax:...
introduce Monitors the health of HTTP servers in ...
In general, MySQL provides a variety of storage e...