《Mysql學(xué)習(xí)淺談MySQL在cmd和python下的常用操作》要點(diǎn):
本文介紹了Mysql學(xué)習(xí)淺談MySQL在cmd和python下的常用操作,希望對(duì)您有用。如果有疑問(wèn),可以聯(lián)系我們。
環(huán)境配置1:安裝mysql,環(huán)境變量添加mysql的bin目錄MYSQL學(xué)習(xí)
環(huán)境配置2:python安裝MySQL-PythonMYSQL學(xué)習(xí)
請(qǐng)根據(jù)自身操作系統(tǒng)下載安裝,否則會(huì)報(bào)c ++ compile 9.0,import _mysql等錯(cuò)誤MYSQL學(xué)習(xí)
windows10 64位操作系統(tǒng)可到 http://www.lfd.uci.edu/~gohlke/pythonlibs/ 下載安裝MySQL-Python包,至于whl和tar.gz在windows和Linux下的安裝辦法可查看我的上一篇文章MYSQL學(xué)習(xí)
一 、cmd命令下的操作:MYSQL學(xué)習(xí)
連接mysql:mysql -u root -pMYSQL學(xué)習(xí)
查看所有數(shù)據(jù)庫(kù):show databases;MYSQL學(xué)習(xí)
創(chuàng)建test數(shù)據(jù)庫(kù):create database test;MYSQL學(xué)習(xí)
刪除數(shù)據(jù)庫(kù):drop database test;MYSQL學(xué)習(xí)
使用(切換至)test數(shù)據(jù)庫(kù):use test;MYSQL學(xué)習(xí)
查看當(dāng)前數(shù)據(jù)庫(kù)下的表:show tables;MYSQL學(xué)習(xí)
創(chuàng)建UserInfo表:create table UserInfo(id int(5) NOT NULL auto_increment,username varchar(10),password varchar(20) NOT NULL,PRIMARY KEY(id));MYSQL學(xué)習(xí)
刪除表:drop table UserInfo;MYSQL學(xué)習(xí)
判斷數(shù)據(jù)是否存在:select * from UserInfo where name like 'elijahxb';MYSQL學(xué)習(xí)
增數(shù)據(jù):insert into UserInfo(username,password) value('eljiahxb','123456');MYSQL學(xué)習(xí)
查數(shù)據(jù):select * from UserInfo; select id from UserInfo; select username from UserInfo;MYSQL學(xué)習(xí)
改數(shù)據(jù):update UserInfo set username = 'Zus' where id=1; update UserInfo set username='Zus';MYSQL學(xué)習(xí)
刪數(shù)據(jù):delete from UserInfo; delete from UserInfo where id=1;MYSQL學(xué)習(xí)
斷開(kāi)連接:quitMYSQL學(xué)習(xí)
二、python下的操作:MYSQL學(xué)習(xí)
# -*- 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:可以不傳此參數(shù),但此時(shí)value每一條數(shù)據(jù)的字段數(shù)必須與數(shù)據(jù)庫(kù)中的字段數(shù)一致. 傳此參數(shù)時(shí),則表示只穿指定字段的字段值. :param value:類(lèi)型必須為只有一組信息的元組,或者包括多條信息的元組組成的列表 :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下的常用操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持維易PHP.MYSQL學(xué)習(xí)
歡迎參與《Mysql學(xué)習(xí)淺談MySQL在cmd和python下的常用操作》討論,分享您的想法,維易PHP學(xué)院為您提供專業(yè)教程。
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.snjht.com/jiaocheng/11302.html