自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python也能操作MySQL數(shù)據(jù)庫

開發(fā) 后端
我們在進(jìn)行Python編程的時(shí)候,時(shí)常要將一些數(shù)據(jù)保存起來,其中最方便的莫過于保存在文本文件了。今天我們要學(xué)的就是數(shù)據(jù)庫中小編自認(rèn)為最棒的Mysql數(shù)據(jù)庫了。

[[414683]]

大家好,我是Python進(jìn)階者。

前言

我們在進(jìn)行Python編程的時(shí)候,時(shí)常要將一些數(shù)據(jù)保存起來,其中最方便的莫過于保存在文本文件了。但是如果保存的文件太大,用文本文件就不太現(xiàn)實(shí)了,畢竟打開都是個(gè)問題,這個(gè)時(shí)候我們需要用到數(shù)據(jù)庫。提到數(shù)據(jù)庫,相信大部分人都不會(huì)陌生,今天我們要學(xué)的就是數(shù)據(jù)庫中小編自認(rèn)為最棒的Mysql數(shù)據(jù)庫了。

一、下載導(dǎo)入模塊

為了讓Python與Mysql 交互,這里我們需要用到Pymsql模塊才行。

下載模塊:

  1. pip install pymysql 

導(dǎo)入模塊:

  1. import pymysql 

二、創(chuàng)建數(shù)據(jù)庫

打開數(shù)據(jù)庫連接軟件 SqlYong,如圖:

輸入命令:

  1. CREATE DATABASE IF NOT EXISTS people; 

這樣就創(chuàng)建了一個(gè)people 數(shù)據(jù)庫。

三、創(chuàng)建數(shù)據(jù)表,并寫入數(shù)據(jù)

  1. USE people; 
  2. CREATE TABLE IF NOT EXISTS student(id INT PRIMARY KEY AUTO_INCREMENT,NAME CHAR(10) UNIQUE,score INT NOT NULL,tim DATETIME)ENGINE=INNOBASE CHARSET utf8; 
  3. INSERT INTO student(NAME,score,tim)VALUES('fasd',60,'2020-06-01'
  4. SELECT * FROM student; 

通過上述操作便創(chuàng)建了一個(gè)數(shù)據(jù)表Student并向其中寫入了數(shù)據(jù),結(jié)果如下:

我們可以一行代碼刪除這個(gè)插入的 數(shù)據(jù):

  1. TRUNCATE student; 

四、MySQL與Python建立連接

將下圖中的參數(shù)依次填入初始化參數(shù)中,

  1. db=pymysql.connect(host='localhost',user='root',password='123456',port=3306,db='people'

這樣就連接到了people數(shù)據(jù)庫,可以看下連接成功的打印信息:

可以看到我們打印了Mysql的版本和Host信息。

五、創(chuàng)建游標(biāo)執(zhí)行操作

1.創(chuàng)建游標(biāo)

  1. cur=db.cursor 

2.編寫插入數(shù)據(jù)表達(dá)式

  1. sql="INSERT INTO student(NAME,score,tim)VALUES('任性的90后boy',100,now())" 

3.開啟游標(biāo)事件

  1. cur.begin() 

4.執(zhí)行數(shù)據(jù)庫語句,異常判斷

  1. try: 
  2.     cur.execute(sql) 執(zhí)行數(shù)據(jù)庫語句 
  3. except Exception as e: 
  4.     print(e) 
  5.     db.rollback()   發(fā)生異常進(jìn)行游標(biāo)回滾操作 
  6. else
  7.     db.commit()   提交數(shù)據(jù)庫操作 
  8. finally: 
  9.     cur.close()  關(guān)閉游標(biāo) 
  10.     db.close()  關(guān)閉數(shù)據(jù)庫 

5.執(zhí)行插入操作

數(shù)據(jù)庫建立好后,我們可以對它們進(jìn)行插入數(shù)據(jù)的操作。

  1. import time 
  2. db=pymysql.connect(host='localhost',user='root',password='123456',port=3306,db='people'
  3. cur=db.cursor() 
  4. db.begin() 
  5. sql="INSERT INTO student(NAME,score,tim) VALUES ('%s',%d,'%s')" 
  6. data=('HW',90,tt) 
  7. try: 
  8.   cur.execute(sql%data) 
  9. except Exception as e: 
  10.   print(e) 
  11.   db.rollback() 
  12. else
  13.   db.commit() 
  14. finally: 
  15.   cur.close() 
  16.   db.close() 

這樣就可以將數(shù)據(jù)插入進(jìn)去了。我們還可以自定義插入:

  1. import pymysql 
  2. import time 
  3. tt=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) 
  4. db=pymysql.connect(host='localhost',user='root',password='123456',port=3306,db='people'
  5. cur=db.cursor() 
  6. db.begin() 
  7. s=input('string:'
  8. d=input('number:'
  9. sql="INSERT INTO student(NAME,score,tim)VALUES('%s','%s','%s')" 
  10. try: 
  11.   data=(s,d,tt) 
  12.   cur.execute(sql%data) 
  13. except Exception as e: 
  14.   print(e) 
  15.   db.rollback() 
  16. else
  17.   db.commit() 
  18. finally: 
  19.   cur.close() 
  20.   db.close() 

另外,我們也可以同時(shí)插入多條數(shù)據(jù),只需先定義好所有的數(shù)據(jù),然后在調(diào)用即可,這里需要用到插入多條數(shù)據(jù)的函數(shù)Executemany,在這里我插入十萬條數(shù)據(jù),并測試插入時(shí)間,步驟如下:

  1. import pymysql 
  2. import time 
  3. start=time.time() 
  4. tt=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) 
  5. db=pymysql.connect(host='localhost',user='root',password='123456',port=3306,db='people'
  6. cur=db.cursor() 
  7. db.begin() 
  8. sql="insert into student(NAME,score,tim)values(%s,%s,%s)" 
  9. def get(): 
  10.   ab=[] 
  11.   for y in range(1,100000): 
  12.     if y>=100: 
  13.       data=('user-'+str(y),str(str(float('%.f'%(y%100)))),tt) 
  14.     else
  15.       data=('user-'+str(y),str(y),tt) 
  16.     ab.append(data) 
  17.   return ab 
  18.  
  19. try: 
  20.   data=get() 
  21.   cur.executemany(sql,data) 
  22. except Exception as e: 
  23.   print(e) 
  24.   db.rollback() 
  25. else
  26.   db.commit() 
  27. finally: 
  28.   print('插入數(shù)據(jù)完畢'
  29.   cur.close() 
  30.   db.close() 
  31.   end=time.time() 
  32.   print('用時(shí):',str(end-start)) 

6.執(zhí)行更新操作

有些數(shù)據(jù)我們覺得它過時(shí)了,想更改,就要更新它的數(shù)據(jù)。

  1. import time 
  2. db=pymysql.connect(host='localhost',user='root',password='123456',port=3306,db='people'
  3. cur=db.cursor() 
  4. db.begin() 
  5. sql="update student set name='zjj' where score=100 " 當(dāng)分?jǐn)?shù)是100分的時(shí)候?qū)⒚指臑閦jj 
  6. try: 
  7.   cur.execute(sql%data) 
  8. except Exception as e: 
  9.   print(e) 
  10.   db.rollback() 
  11. else
  12.   db.commit() 
  13. finally: 
  14.   cur.close() 
  15.   db.close() 

7.執(zhí)行刪除操作

有時(shí)候一些數(shù)據(jù)如果對于我們來說沒有任何作用了的話了,我們就可以將它刪除了,不過這里是刪除數(shù)據(jù)表中的一條記錄。

  1. import pymysql 
  2. db=pymysql.connect(host='localhost',user='root',password='123456',port=3306,db='people'
  3. cur=db.cursor() 
  4. db.begin() 
  5. sql="delete from student where name='fasd';" 當(dāng)名字等于‘fasd’的時(shí)候刪除這個(gè)記錄 
  6. try: 
  7.   cur.execute(sql) 
  8. except Exception as e: 
  9.   print(e) 
  10.   db.rollback() 
  11. else
  12.   db.commit() 
  13. finally: 
  14.   cur.close() 
  15.   db.close() 

你也可以刪除表中所有的數(shù)據(jù),只需將Sql語句改為:

  1. sql='TRUNCATE student;' 

當(dāng)然你也可以刪除表,但是一般不建議這樣做,以免誤刪:

  1. DROP TABLE IF EXISTS  student; 

8.執(zhí)行查詢操作

有時(shí)候我們需要對數(shù)據(jù)庫中的數(shù)據(jù)進(jìn)行查詢,Python也能輕松幫我們搞定。

  1. import pymysql 
  2. import time 
  3. tt=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) 
  4. db=pymysql.connect(host='localhost',user='root',password='123456',port=3306,db='people'
  5. cur=db.cursor() 
  6. db.begin() 
  7. sql="select * from student;" 
  8. try: 
  9.   cur.execute(sql) 
  10.   res=cur.fetchall() 查詢數(shù)據(jù)庫中的數(shù)據(jù) 
  11.   for y in res: 
  12.     print(y) 打印數(shù)據(jù)庫中標(biāo)的所有數(shù)據(jù),以元祖的形式 
  13. except Exception as e: 
  14.   print(e) 
  15.   db.rollback() 
  16. else
  17.   db.commit() 
  18. finally: 
  19.   cur.close() 
  20.   db.close() 

六、總結(jié)

在我們進(jìn)行網(wǎng)絡(luò)爬蟲的時(shí)候,需要保存大量數(shù)據(jù),這個(gè)時(shí)候數(shù)據(jù)庫就派上用場了,可以更方便而且更快捷保存數(shù)據(jù)。

 

責(zé)任編輯:姜華 來源: Python爬蟲與數(shù)據(jù)挖掘
相關(guān)推薦

2021-08-04 09:00:53

Python數(shù)據(jù)庫Python基礎(chǔ)

2015-03-13 15:30:26

編程數(shù)據(jù)庫創(chuàng)建表單

2019-05-31 08:23:00

Oracle數(shù)據(jù)庫云渡劫

2019-10-21 13:52:14

MySQL數(shù)據(jù)庫命令

2010-06-01 12:51:23

MySQL數(shù)據(jù)庫

2009-06-04 15:59:53

Netbeans操作MMySQL數(shù)據(jù)庫

2011-07-05 10:16:16

Qt 數(shù)據(jù)庫 SQLite

2022-09-01 23:29:22

MySQLPython數(shù)據(jù)庫

2010-05-14 11:12:16

連接MySql

2010-05-28 14:51:47

MySQL數(shù)據(jù)庫

2010-05-27 16:55:23

操作MySQL

2021-06-03 09:30:30

Python操作注冊表regedit

2024-05-08 08:37:44

2010-03-04 13:47:13

Python操作Acc

2024-11-28 08:07:14

2010-03-04 15:31:44

Python SQLI

2011-07-05 18:04:45

QT Mysql

2010-05-28 18:44:45

2018-02-26 20:00:00

編程語言JavaMySQL

2009-09-07 15:25:24

MySQL數(shù)據(jù)庫互操作Silverlight
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)