环境配置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下的操作:
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 mysqldb10 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 = 330618 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.tablename35 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.tablename40 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.tablename49 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.tablename61 slist = []62 if type(value)==tuple:63 valuelen = value64 execmany = false65 else:66 valuelen = value[0]67 execmany = true68 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在cmd和python下的常用操作汇总分享的详细内容。
