MySQL在cmd和python下的常用操作
环境配置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)
MySqlHelper
MySQL在cmd和python下的常用操作的更多相关文章
- 二叉树的python可视化和常用操作代码
二叉树是一个重要的数据结构, 本文基于"二叉查找树"的python可视化 pybst 包, 做了一些改造, 可以支持更一般的"二叉树"可视化. 关于二叉树和二叉 ...
- python os 模块常用操作
python 2.7 os 常用操作 官方document链接 文件和目录 os.access(path, mode) 读写权限测试 应用: try: fp = open("myfile&q ...
- windows控制台,cmd,命令提示符下的基础操作
打开dos命令窗口1.win+r-->运行-->cmd 2.摁住shift+鼠标右击 选择 在此处打开命令窗口3.在磁盘某文件夹下,选择标题栏中输入框,输入cmd 回车 windows下常 ...
- Python数据类型及常用操作
Python字符串类型 1.用途: 用来记录有描述性的状态.比如:人名,地址等. 2.定义方式: 创建字符串非常简单,在‘ ’,“ ”,‘’‘ ’‘’内一填写一系列的字符例如:msg='hello' ...
- Python字符串的常用操作学习
>>> name = "I love my job!" >>> name.capitalize() #首字母大写 'I love my job! ...
- Python集合的常用操作
字典常用的就是,他的去重. set集合是python的一个基本数据类型. set中的元素是不重复的.⽆无序的.⾥面的元素必须是可hash的(int, str, tuple,bool). 我们可以这样来 ...
- Linux下Vim常用操作
linux下Vim的常用操作 linux 首先\(ctrl+Alt+t\)打开小框框 \(./\):相当于手机上的\(home\)键 \(ls\):当前文件夹的东东 \(mkdir\) ...
- 初识python: 字符串常用操作
直接上代码示例: #!/user/bin env python # author:Simple-Sir # time:20180914 # 字符串常用操作 name = 'lzh lyh' print ...
- mysql下的常用操作
本文继 linux下安装mysql,记录下在工作中最常用的mysql语句 MySQL添加字段和删除字段 添加字段: alter table `user_movement_log`Add column ...
随机推荐
- review41
套接字是基于TCP协议的网络通信. 基于UDP
- 网页seo
两大 网站布局和代码编辑 网站结构布局优化 1.控制首页链接数量 100以内2.偏平化的目录层次 百度蜘蛛点三次 超过三次,找不到结果. 导航SEO优化导航最好是文字 图片最好都加上 title 和a ...
- compile to 32-bit elf file
nasm -f elf -o a.o a.asm gcc -c -m32 -o b.o b.c ld -s -m elf_i386 -Ttext 0x30400 -o b.bin b.o a.o
- NSLog Release
//将下面的代码插入到pch文件中即可 #ifndef __OPTIMIZE__ # define NSLog(…) NSLog(__VA_ARGS__) #else # define NSLog(… ...
- SMOTE RF MLP demo use cross_val_score to find best argument 处理不平衡数据的demo代码 先做smote处理 再用交叉验证找到最好的模型参数 实践表明MLP更好
# _*_coding:UTF-8_*_ from sklearn.externals.six import StringIO from sklearn import tree import pydo ...
- @angular/cli项目构建--httpClient
app.module.ts update imports: [ HttpClientModule] product.component.ts import {Component, OnInit} fr ...
- 【SQL查询】查询的值为空时,给出默认值_NVL函数
格式为: NVL( string1, replace_with) 功能:如果string1为NULL,则NVL函数返回replace_with的值,否则返回string1的值. 引申一下,此NVL的作 ...
- 把数据库里面的数据导出来csv
function export_csv($filename, $data, $head = '') { $string = $head; foreach ($data as $key => $v ...
- https页面 和 http请求的问题
(1)强制升级http 静态资源地址为https地址 https页面中不能使用http请求,http页面中可以使用https请求. 关于在https 页面有一些http的请求,可以在<head& ...
- Linux U盘 启动盘
/****************************************************************************** * Linux U盘 启动盘 * 说明: ...