A brief discussion on common operations of MySQL in cmd and python

A brief discussion on common operations of MySQL in cmd and python

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:
  • Some basic methods of using Python to operate MySQL
  • Basic methods of operating MySQL in Python programs
  • Python operation MySQL detailed explanation and examples
  • Simple implementation method of Python operation MySQL
  • Example code for using Python to operate MySQL database
  • How to use Python to operate MySQL database
  • Getting Started with MySQL in Python

<<:  JavaScript implements the protocol example in which the user must check the box

>>:  Linux kernel device driver advanced character device driver notes

Recommend

Vue implements zip file download

This article example shares the specific code of ...

MySQL master-slave replication principle and points to note

Written in front I have been writing a special to...

Detailed explanation of the basic usage of VUE watch listener

Table of contents 1. The following code is a simp...

How to implement h5 input box prompt + normal text box prompt

XML/HTML CodeCopy content to clipboard < input...

How to quickly delete all tables in MySQL without deleting the database

This article uses an example to describe how to q...

Detailed explanation of VUE responsiveness principle

Table of contents 1. Responsive principle foundat...

React Fiber structure creation steps

Table of contents React Fiber Creation 1. Before ...

VMware vCenter 6.7 installation process (graphic tutorial)

background I originally wanted to download a 6.7 ...

Detailed explanation of using top command to analyze Linux system performance

Introduction to Linux top command The top command...

Summary of 6 solutions for implementing singleton mode in JS

Preface Today, I was reviewing the creational pat...