日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

MySQL与Python交互入门

發布時間:2024/9/15 数据库 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MySQL与Python交互入门 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

預計閱讀時間:?11分鐘

MySQL入門

一、基本命令

1、啟動服務以管理員身份運行cmdnet start 服務名稱 2、停止服務以管理員身份運行cmdnet stop 服務名稱 3、連接數據庫格式:mysql - u root - p ->輸入密碼 4、退出登錄(斷開連接)exit或quit 5、查看版本(連接后可以執行)select version() 6、顯示當前時間(連接后可以執行)select now() 7、遠程連接mysql - h ip地址 - u 用戶名 - p ->輸入對方mysql密碼

二、數據庫操作命令

1、創建數據庫create database 數據庫名 charset = utf8 2、刪除數據庫drop database 數據庫名 3、切換數據庫use 數據庫名 4、查看當前選擇的數據庫select database()

三、表操作命令

1、查看數據庫中所有表show tables 2、創建表create table 表名(列及類型)eg:create table student(id int auto_increment primary key,name varchar(20) not null)注:auto_increment 自增長 primary key 主鍵 not null 非空 3、刪除表drop table 表名 4、查看表結構desc 表名 5、查看建表語句show create table 表名 6、重命名表rename table 原表名 to 新表名 7、修改表alter table 表名 add | change | drop 列名

四、數據操作命令

1、增a、全列插入insert into 表名 values(...)eg: insert?into?student?values(0,?"tom",?"北京")主鍵列是自動增長,但是在全列插入時需要占位,通常使用0,插入成功以后以實際數據為準b、缺省插入insert into 表名(列1,列2..) values(值1,值2..)c、同時插入多條數據insert into 表名 values(...), (...), ... 2、刪delete from 表名 where 條件不寫條件則全刪 3、改update 表名 set 列1 = 值1, 列2 = 值2, ... where 條件 4、查查詢表中的全部數據select * from 表名

五、查

1、基本語法select * from 表名from關鍵字后面是表名,表示數據來源于這張表select后面寫表中的列名,如果是?*?表示在結果集中顯示表中的所有列在select后面的列名部分,可以使用as為列名起別名,這個別名顯示在結果集中如果要查詢多個列,之間使用逗號分隔# eg:select name as a,age from student; 2、消除重復行在select后面列前面使用distinct可以消除重復的行eg:select distinct gender from student 3、條件查詢a、語法select * from 表名 where 條件b、比較運算符等于(=) 大于(>) 小于(<) 大于等于(>=) 小于等于(<=) 不等于(!= 或 <>)c、邏輯運算符and or notd、模糊查詢like% 表示任意多個任意字符_ 表示一個任意字符e、范圍查詢in 表示在一個非連續的范圍內between。。。and。。。表示在一個連續的范圍內eg:where id in (8, 10, 13)f、空判斷注意:null與""是不同的判斷空:is null判斷非空:is not nullg、優先級小括號,not,比較運算符。邏輯運算符and比or優先級高,同時出現并希望先選or,需要結合括號來使用 4、聚合為了快速得到統計數,提供了5個聚合函數a、count(*) 表示計算總行數,括號中可以寫 * 或列名b、max(列) 表示求此列的最大值c、min(列) 表示求此列的最小值d、sum(列) 表示求此列的和e、avg(列) 表示求此列的平均值 5、分組按照字段分組,表示此字段相同的數據會被放到一個集合中。分組后,只能查詢出相同的數據列,對于有差異的數據列無法顯示在結果集中可以對分組后的數據進行統計,做聚合運算select 列1, 列2, 聚合... from 表名 group by 列1, 列2 having 列1, 列2eg: 查詢男女生總數select gender, count(*) from student group by genderwhere與having的區別:where是對from后面指定的表進行篩選,屬于對原始數據的篩選;having是對group by的結果進行篩選。 6、排序select * from 表名 order by 列1 asc | desc, 列2 asc | desc, ...a、將數據按照列1進行排序,如果某些列1的值相同則按照列2排序b、默認按照從小到大的順序c、asc升序d、desc降序 7、分頁select * from 表名 limit start, count從start開始,看count條

六、關聯

建表語句 1、create table class(id int auto_increment primary key, name varchar(20) not null, stuNum int not null) 2、create table students(id int auto_increment primary key, name varchar(20) not null, gender bit default 1, classid int not bull, foreign key(classid) references class(id))插入一些數據: insert?into?class?values(0,?"python1",?50),?(0,?"python2",?60),?(0,?"python3",?70) insert into students values(0, "tom", 1, 1)關聯查詢: select students.name, class.name from class inner join students on class.id = students.classid分類:1、表A inner join 表B表A與表B匹配的行會出現在結果集中2、表A left join 表B表A與表B匹配的行會出現在結果集中,外加表A中獨有的數據,未對應的數據使用null填充3、表A right join 表B表A與表B匹配的行會出現在結果集中,外加表B中獨有的數據,未對應的數據使用null填充

交互

進行python與mysql的交互需要安裝pymysql庫,安裝也很簡單,常規的pip install pymysql就可以了。

import?pymysql# 連接數據庫 # 參數一:mysql服務所在主機的IP # 參數二:用戶名 # 參數三:密碼 # 參數四:要連接的數據庫名 # db = pymysql.connect("localhost", "root", "123456", "student") db = pymysql.connect("192.168.18.1", "root", "123456", "student")# 創建一個cursor對象 cursor = db.cursor()sql = "select version()"# 執行sql語句 cursor.execute(sql)# 獲取返回的信息 data = cursor.fetchone() print(data)# 斷開 cursor.close() db.close()

創建數據庫表

import?pymysqldb = pymysql.connect("localhost", "root", "123456", "student") cursor = db.cursor()# 檢查表是否存在,如果有則刪除 cursor.execute("drop table if exists bancard")# 建表 sql = "create table bandcard(id int auto_increment primary key, money int not null)" cursor.execute(sql)cursor.close() db.close()

插入、刪除、更新

import?pymysqldb = pymysql.connect("localhost", "root", "123456", "student") cursor = db.cursor()sql = "insert into bandcard values(0, 300)"?????????# 插入 # sql = "update bandcard set money=1000 where id=1" # 更新 # sql = "delete from bandcard where money=200"? ? ? # 刪除try:cursor.execute(sql)db.commit() # 執行這條才插入 except:# 如果提交失敗,回滾到上一次數據db.rollback() cursor.close() db.close()

查詢

import?pymysql''' fetchone() 功能:獲取下一個查詢結果集,結果集是一個對象fetchall() 功能:接收全部的返回的行rowcount 是一個只讀屬性,返回execute()方法影響的行數 '''db = pymysql.connect("localhost", "root", "123456", "student") cursor = db.cursor()sql = "select * from bandcard where money>200" try:cursor.execute(sql)reslist = cursor.fetchall()for?row in?reslist:print("%d--%d"?% (row[0], row[1])) except:# 如果提交失敗,回滾到上一次數據db.rollback() cursor.close() db.close()

? ???精 彩 文 章?

  • 程序員,你喜歡抽哪種香煙?(python數據分析)

  • 面試官:聽說你sql寫的挺溜的,你說一說查詢sql的執行過程

  • 當你裝不上Python外部包時,試試這個網站

END

來和小伙伴們一起向上生長呀~~~

掃描下方二維碼,添加小詹微信,可領取千元大禮包并申請加入 Python學習交流群,群內僅供學術交流,日常互動,如果是想發推文、廣告、砍價小程序的敬請繞道!一定記得備注「交流學習」,我會盡快通過好友申請哦!

(添加人數較多,請耐心等待)

(掃碼回復 1024? 即可領取IT資料包)

總結

以上是生活随笔為你收集整理的MySQL与Python交互入门的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。