環境配置1:安裝mysql,環境變數新增mysql的bin目錄
環境配置2:python安裝MySQL-Python
請根據自身作業系統下載安裝,否則會報c ++ compile 9.0,import _mysql等錯誤
windows10 64位元作業系統可至http://www.lfd.uci.edu/~gohlke/pythonlibs/ 下載安裝MySQL-Pythontar,至於whl和.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 varcharword(10),pass 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');
## 〠 UserInfo; select username from UserInfo; 改資料:update UserInfo set username = 'Zus' where id=1; update UserInfo set username='Zus'; ; delete from UserInfo where id=1; 斷開連接:quit ##


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下的常用操作匯總分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!

要在有限的時間內最大化學習Python的效率,可以使用Python的datetime、time和schedule模塊。 1.datetime模塊用於記錄和規劃學習時間。 2.time模塊幫助設置學習和休息時間。 3.schedule模塊自動化安排每週學習任務。

Python在遊戲和GUI開發中表現出色。 1)遊戲開發使用Pygame,提供繪圖、音頻等功能,適合創建2D遊戲。 2)GUI開發可選擇Tkinter或PyQt,Tkinter簡單易用,PyQt功能豐富,適合專業開發。

Python适合数据科学、Web开发和自动化任务,而C 适用于系统编程、游戏开发和嵌入式系统。Python以简洁和强大的生态系统著称,C 则以高性能和底层控制能力闻名。

2小時內可以學會Python的基本編程概念和技能。 1.學習變量和數據類型,2.掌握控制流(條件語句和循環),3.理解函數的定義和使用,4.通過簡單示例和代碼片段快速上手Python編程。

Python在web開發、數據科學、機器學習、自動化和腳本編寫等領域有廣泛應用。 1)在web開發中,Django和Flask框架簡化了開發過程。 2)數據科學和機器學習領域,NumPy、Pandas、Scikit-learn和TensorFlow庫提供了強大支持。 3)自動化和腳本編寫方面,Python適用於自動化測試和系統管理等任務。

兩小時內可以學到Python的基礎知識。 1.學習變量和數據類型,2.掌握控制結構如if語句和循環,3.了解函數的定義和使用。這些將幫助你開始編寫簡單的Python程序。

如何在10小時內教計算機小白編程基礎?如果你只有10個小時來教計算機小白一些編程知識,你會選擇教些什麼�...

使用FiddlerEverywhere進行中間人讀取時如何避免被檢測到當你使用FiddlerEverywhere...


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

記事本++7.3.1
好用且免費的程式碼編輯器

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

WebStorm Mac版
好用的JavaScript開發工具