浅谈MySQL在cmd和python下的常用操作,mysqlpython

条件安顿1:安装mysql,遇到变量加多mysql的bin目录

浅谈MySQL在cmd和python下的常用操作,mysqlpython

条件布置一:安装mysql,蒙受变量增多mysql的bin目录

境况布署二:python安装MySQL-Python

请遵照本人操作系统下载安装,不然会报c ++ compile 九.0,import
_mysql等错误

windows拾 60人操作系统可到
下载安装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

2、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)

如上那篇浅谈MySQL在cmd和python下的常用操作正是作者分享给大家的全体内容了,希望能给大家一个参照,也盼望我们多多扶助帮客之家。

情状陈设一:安装mysql,处境变量增添mysql的bin目录
情形布署二:python安装MySQL-Python 请遵照自家操…

MySQL在cmd和python下的常用操作,mysqlcmdpython

条件安插一:安装mysql,境况变量加多mysql的bin目录

条件安插二:python安装MySQL-Python

  请依照自家操作系统下载安装,不然会报c ++ compile 玖.0,import
_mysql等错误

  windows十 6二人操作系统可到
 
下载安装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下的操作:

  

图片 1

 1 # -*- coding: utf-8 -*-
 2 #!/usr/bin/env python
 3 
 4 # @Time    : 2017/6/4 18:11
 5 # @Author  : Elijah
 6 # @Site    : 
 7 # @File    : sql_helper.py
 8 # @Software: PyCharm Community Edition
 9 import MySQLdb
10 
11 class MySqlHelper(object):
12     def __init__(self,**args):
13         self.ip = args.get("IP")
14         self.user = args.get("User")
15         self.password = args.get("Password")
16         self.tablename = args.get("Table")
17         self.port = 3306
18         self.conn = self.conn = MySQLdb.Connect(host=self.ip,user=self.user,passwd=self.password,port=self.port,connect_timeout=5,autocommit=True)
19         self.cursor = self.conn.cursor()
20 
21     def Close(self):
22         self.cursor.close()
23         self.conn.close()
24     def execute(self,sqlcmd):
25         return self.cursor.execute(sqlcmd)
26     def SetDatabase(self,database):
27         return self.cursor.execute("use %s;"%database)
28     def GetDatabasesCount(self):
29         return self.cursor.execute("show databases;")
30     def GetTablesCount(self):
31         return self.cursor.execute("show tables;")
32     def GetFetchone(self, table = None):
33         if not table:
34             table = self.tablename
35         self.cursor.execute("select * from %s;"%table)
36         return self.cursor.fetchone()
37     def GetFetchmany(self,table=None,size=0):
38         if not table:
39             table = self.tablename
40         count = self.cursor.execute("select * from %s;"%table)
41         return self.cursor.fetchmany(size)
42     def GetFetchall(self,table=None):
43         '''
44         :param table: 列表
45         :return:
46         '''
47         if not table:
48             table = self.tablename
49         self.cursor.execute("select * from %s;"%table)
50         return self.cursor.fetchall()
51     def SetInsertdata(self,table=None,keyinfo=None,value=None):
52         """
53         :param table:
54         :param keyinfo:可以不传此参数,但此时value每一条数据的字段数必须与数据库中的字段数一致。
55                         传此参数时,则表示只穿指定字段的字段值。
56         :param value:类型必须为只有一组信息的元组,或者包含多条信息的元组组成的列表
57         :return:
58         """
59         if not table:
60             table = self.tablename
61         slist = []
62         if type(value)==tuple:
63             valuelen = value
64             execmany = False
65         else:
66             valuelen = value[0]
67             execmany = True
68         for each in range(len(valuelen)):
69             slist.append("%s")
70         valuecenter = ",".join(slist)
71         if not keyinfo:
72             sqlcmd = "insert into %s values(%s);"%(table,valuecenter)
73         else:
74             sqlcmd = "insert into %s%s values(%s);" % (table,keyinfo,valuecenter)
75         print(sqlcmd)
76         print(value)
77         if execmany:
78             return self.cursor.executemany(sqlcmd,value)
79         else:
80             return self.cursor.execute(sqlcmd, value)

MySqlHelper

 

遭逢安排一:安装mysql,遇到变量增添mysql的bin目录
碰到布署二:python安装MySQL-Python 请依据本身操作…

条件计划二:python安装MySQL-Python

请依照本身操作系统下载安装,不然会报c ++ compile 九.0,import
_mysql等错误

windows10 陆拾位操作系统可到
下载安装MySQL-Python包,至于whl和tar.gz在windows和Linux下的装置情势可查看自身的上壹篇文章

1 、cmd命令下的操作:

连接mysql:mysql -u root -p

查阅全数数据库:show databases;

创建test数据库:create database test;

发表评论

电子邮件地址不会被公开。 必填项已用*标注