MySQL在cmd和python下的经常使用操纵剖析【MySQL教程】,python,MySQL,解析
作者:搜教程发布时间:2019-11-27分类:MySQL教程浏览:53评论:0
环境设置1:装置mysql,环境变量增加mysql的bin目次
环境设置2:python装置MySQL-Python
请依据本身操纵系统下载装置,不然会报c ++ compile 9.0,import _mysql等毛病
windows10 64位操纵系统可到 http://www.lfd.uci.edu/~gohlke/pythonlibs/ 下载装置MySQL-Python包,至于whl和tar.gz在windows和Linux下的装置要领可检察我的上一篇文章
一 、cmd敕令下的操纵:
衔接mysql:mysql -u root -p
检察一切数据库:show databases;
建立test数据库:create database test;
删除数据库:drop database test;
运用(切换至)test数据库:use test;
检察当前数据库下的表:show tables;
建立UserInfo表:create table UserInfo(id int(5) NOT NULL auto_increment,username varchar(10),password varchar(20) NOT NULL,PRIMARY KEY(id));
删除表:drop table UserInfo;
推断数据是不是存在:select * from UserInfo where name like 'elijahxb';
增数据:insert into UserInfo(username,password) value('eljiahxb','123456');
查数据:select * from UserInfo; select id from UserInfo; select username from UserInfo;
改数据:update UserInfo set username = 'Zus' where id=1; update UserInfo set username='Zus';
删数据:delete from UserInfo; delete from UserInfo where id=1;
断开衔接:quit
二、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: 列表 :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:能够不传此参数,但此时value每一条数据的字段数必需与数据库中的字段数一致。 传此参数时,则示意只穿指定字段的字段值。 :param value:范例必需为只要一组信息的元组,或许包括多条信息的元组构成的列表 :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)
相干引荐:
怎样应用CMD衔接本机mysql数据库
怎样登录mysql以及cmd怎样衔接mysql数据库?
php 中实行cmd敕令的要领
以上就是MySQL在cmd和python下的经常运用操纵剖析的细致内容,更多请关注ki4网别的相干文章!
相关推荐
- python数据类型有哪几种?_Python教程,python
- MySQL如何使用授权命令grant_MySQL教程,MySQL,grant
- python针对Excel表格的操作_Python教程,python,excel
- 实例解析Python单元测试及unittest框架用法_Python教程,python,单元测试,unittest框架
- Python如何使用xlrd实现读取合并单元格_Python教程,python,xlrd
- 手把手教你在python中如何使用while True语句_Python教程,python,while true
- 给大家分享一下日常学习python的心得(详解)_Python教程,python
- MySQL 连接查询超级详解_MySQL教程,MySQL,连接查询
- python如何另起一行?_Python教程,python
- python是一种跨平台、开源、免费的高级动态编程语言,对么_Python教程,python,跨平台,开源,免费,编程语言
你 发表评论:
欢迎- MySQL教程排行
-
- 1mysql中key 、primary key 、unique key 与index区别详解_MySQL教程,mysql
- 2关于一道mysql查询面试题的思考解决过程_MySQL教程,mysql
- 3关于linux下mysql去除严格模式_MySQL教程,linux
- 4分离数据库和附加数据库的区别_MySQL教程,数据库
- 5如何修改mysql的默认时区_MySQL教程,mysql,时区
- 6利用mysql生成唯一序号_MySQL教程,mysql
- 7mysql数据库如何创建数据表_MySQL教程,mysql,数据表
- 8看看MySQL 5.6, 5.7, 8.0的新特性!_MySQL教程,MySQL
- 9mysql实现每个专业分数段统计人数_MySQL教程,mysql
- 最新文章
- 广而告之