浅谈MySQL在cmd和python下的常用操作
作者:jingxian 时间:2024-01-16 13:11:17
环境配置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)
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
我是如何从玩Photoshop变成老板的
![](https://img.aspxhome.com/file/UploadPic/20084/10/2008410114044665s.jpg)
python 利用pywifi模块实现连接网络破解wifi密码实时监控网络
Python网络爬虫信息提取mooc代码实例
PyTorch策略梯度算法详情
![](https://img.aspxhome.com/file/2023/7/86617_0s.png)
MYSQL中 char 和 varchar的区别
mysql 8.0.12 解压版安装教程 个人亲测!
![](https://img.aspxhome.com/file/2023/2/119702_0s.png)
ASP技巧:ASP中三个常用语句的使用技巧
Vue路由跳转传参或者打开新页面跳转问题
![](https://img.aspxhome.com/file/2023/9/133049_0s.png)
python多进程 主进程和子进程间共享和不共享全局变量实例
![](https://img.aspxhome.com/file/2023/1/85261_0s.jpg)
MySQL的时间差函数TIMESTAMPDIFF、DATEDIFF的用法
Python编写生成验证码的脚本的教程
vuejs实现标签选项卡动态更改css样式的方法
"模板化"——限制还是激发
Flask使用SocketIO实现WebSocket与Vue进行实时推送
![](https://img.aspxhome.com/file/2023/6/112416_0s.jpg)
基于Bootstrap+jQuery.validate实现Form表单验证
![](https://img.aspxhome.com/file/2023/7/126807_0s.jpg)
Firefox 3.5 新增加的支持(整理)
教程:打造SQL Server2000的安全策略
Vue CLI3.0中使用jQuery和Bootstrap的方法
![](https://img.aspxhome.com/file/2023/5/139945_0s.jpg)
Python+OpenCV实现图像融合的原理及代码
![](https://img.aspxhome.com/file/2023/9/101239_0s.jpg)