Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Examples of using MySQL under cmd and python

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the use of MySQL in cmd and python examples, the article is very detailed, has a certain reference value, interested friends must read it!

Environment configuration 1: install mysql, add the bin directory of mysql in the environment variable

Environment configuration 2:python installation MySQL-Python

Please download and install according to your own operating system, otherwise you will report errors such as c + + compile 9.0 import _ mysql, etc.

Windows10 64-bit operating system can be downloaded and installed MySQL-Python package at http://www.lfd.uci.edu/~gohlke/pythonlibs/. As for the installation method of whl and tar.gz under windows and Linux, please see my previous article.

1. Operations under the cmd command:

Connect 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) test database: use test

View the table under the current database: show tables

Create the 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'

Added data: insert into UserInfo (username,password) value ('eljiahxb','123456')

Check data: select * from UserInfo; select id from UserInfo; select username from UserInfo

Change the 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 1815 @ Author: Elijah# @ Site: # @ File: sql_helper.py# @ Software: 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 may not be passed, but at this time the number of fields in each piece of data in value must be the same as the number of fields in the database. When this parameter is passed, it means that only the field value of the specified field is worn. : param value: the type must be a tuple with only one set of information Or a list of tuples containing multiple messages: 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) are all the contents of this article entitled" examples of the use of MySQL under cmd and python ". Thank you for reading! Hope to share the content to help you, more related 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: 211

*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.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report