• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • MsSql
  • Mysql
  • oracle
  • MariaDB
  • DB2
  • SQLite
  • PostgreSQL
  • MongoDB
  • Redis
  • Access
  • 数据库其它
  • sybase
  • HBase
您的位置:首页 > 数据库 >Mysql > MySQL在cmd和python下的常用操作解析

MySQL在cmd和python下的常用操作解析

作者:匿名 字体:[增加 减小] 来源:互联网 时间:2018-12-05

匿名通过本文主要向大家介绍了python,MySQL,解析等相关知识,希望本文的分享对您有所帮助
本文主要为大家带来一篇浅谈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)

相关推荐:

如何利用CMD连接本机mysql数据库

如何登录mysql以及cmd如何连接mysql数据库?

php 中执行cmd命令的方法

以上就是MySQL在cmd和python下的常用操作解析的详细内容,更多请关注微课江湖其它相关文章!

分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • 基于Mysql的IP处理函数inet_aton()与inet_ntoa()的深入分析
  • Mysql中校对集utf8_unicode_ci与utf8_general_ci的区别说明
  • IP处理函数inet_aton()和inet_ntoa()使用说明
  • 利用mysql的inet_aton()和inet_ntoa()函数存储IP地址的方法分享
  • 如何实现python3实现并发访问水平切分表
  • mysql大表中count()的用法以及mysql中count()的优化
  • 分享一个纯 Python 实现的 MySQL 客户端操作库
  • Python Unittest怎么进行自动化的单元测试
  • python使用unittest测试接口步奏详解
  • Python怎么统计字母出现的次数

相关文章

  • 2018-12-05SQL语句实现SQL Server 2000及Sql Server 2005日志收缩(批量)
  • 2017-05-11mysql性能优化脚本mysqltuner.pl使用介绍
  • 2018-12-05中小软件公司项目管理(3.3 项目外部关键成功因素)
  • 2017-05-11总结MySQL建表、查询优化的一些实用小技巧
  • 2018-12-05select into 和 insert into select 两种表复制语句
  • 2018-12-05关于MySQL安装包如何使用的详细介绍
  • 2017-05-11windows下忘记MySQL密码的修改方法
  • 2018-12-05在Mysql开发中经常会掉进的坑 - 无法启动Mysql
  • 2018-12-05 MongoDB学习笔记《二》
  • 2018-12-05MySQL之-MySQL高可用实现的详细介绍

文章分类

  • MsSql
  • Mysql
  • oracle
  • MariaDB
  • DB2
  • SQLite
  • PostgreSQL
  • MongoDB
  • Redis
  • Access
  • 数据库其它
  • sybase
  • HBase

最近更新的内容

    • SQL Server2005下的安全操作技巧分享
    • 关于扩展技术的7篇文章推荐
    • 将Sql Server对象的当前拥有者更改成目标拥有者
    • MySQL之——MySQL Cluster集群搭建详解(基于RPM安装包)
    • SQL Server 2008中的代码安全(六) 对称密钥加密
    • sqlserver下Kill 所有连接到某一数据库的连接
    • MySQL 不允许从远程访问的解决方法
    • 有关mysql_errno()函数的文章推荐10篇
    • Lost connection to MySQL server during query的解决
    • 55最佳实践系列:MongoDB最佳实践

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有