Python python更新資料庫指令碼兩種方法

2022-06-14 03:54:13 字數 3757 閱讀 9062

最近專案的兩次版本迭代中,根據業務需求的變化,需要對資料庫進行更新,兩次分別使用了不同的方式進行更新。

第一種:使用python的mysqldb模組利用原生的sql語句進行更新

1 import mysqldb

2 #主機名

3 host = '127.0.0.1'

4 #使用者名稱

5 user = "root"

6 #密碼

7 passwd = "123456"

8 #資料庫名

9 db = "db_name"

10 # 開啟資料庫連線

11 db=mysqldb.connect(host,user,passwd,db)

12 # 獲取操作游標

13 cursor=db.cursor()

14 15 if __name__ == '__main__':

16 17 if cursor:

18 command_a = "update tables_one set status=5 where status=0"

19 # 使用execute方法執行sql語句

20 cursor.execute(command_a)

21 # 提交到資料庫執行

22 db.commit()

23 24 command2 = "select field from tables_one where id =12"

25 ret2 = cursor.execute(command2)

26 # 獲取所有記錄列表

27 ret2=cursor.fetchall()

28 for item in ret2:

29 command3 = "insert into tables_two(name) values (%s);" % (item[0])

30 fin=cursor.execute(command3)

31 db.commit()

32 # 關閉資料庫連線

33 db.close()

資料庫查詢三種方式

第二種:使用python的框架flask和sqlalchemy進行更新

1 # -*- coding:utf-8 -*-

2 from flask import flask

3 from flask_sqlalchemy import sqlalchemy

4 from sqlalchemy.sql import text

5 6 host = '127.0.0.1'

7 user = "root"

8 passwd = "123456"

9 db = "carrier_test"

10 chartset = "utf8"

11 13 #鏈結資料庫路徑

15 #如果設定成 true (預設情況),flask-sqlalchemy 將會追蹤物件的修改並且傳送訊號。這需要額外的記憶體, 如果不必要的可以禁用它。

17 #如果設定成 true,sqlalchemy 將會記錄所有 發到標準輸出(stderr)的語句,這對除錯很有幫助。

19 # 資料庫連線池的大小。預設是資料庫引擎的預設值 (通常是 5)。

22 23 class table_one(db.model):

24 __tablename__ = 'table_one'

25 26 id = db.column('id', db.integer, primary_key=true, autoincrement=true)

27 com_name = db.column('com_name', db.string(30), nullable=false)

28 com_about = db.column('com_about', db.string(200), nullable=false)

29 30 def __repr__(self):

31 return '' % self.com_name

32 33

34 class table_two(db.model):

35 __tablename__ = 'table_two'

36 37 id = db.column('id', db.integer, primary_key=true, autoincrement=true)

38 reason = db.column('reason', db.string(128), nullable=true)

39 create_time = db.column('create_time', db.timestamp, server_default=text('now()'))

40 status = db.column('status', db.integer, nullable=false, default=0)

41 42 def __repr__(self):

43 return '' % self.id

44 45 def db_commit_all(lists):

46 try:

47 db.session.add_all(lists)

48 db.session.commit()

49 return 'success'

50 except exception,e:

51 return 'fail!!!'

52 53 def commits_to_three_judge():

54 com_sta_obj = table_one.query.filter_by(com_name='只是測試使用,不用關心表間關係').all()

55 for ite in com_sta_obj:

56 ship_obj = table_two.query.filter_by(id=ite.id).first()

57 if ship_obj:

58 if int(ship_obj.status) == 2:

59 ite.status = 0

60 print db_commit_all([ite])

61 print '表同步結束'

62 63 64

65 if __name__=='__main__':

66 #執行更新資料庫函式

67 commits_to_three_judge()

兩種方式對比:

1.在實際專案中,資料庫的更新 需要用到很多相關函式進行資料的收集,判斷是否滿足條件等,而這些相關函式在專案中都是用 sqlalchemy進行資料相關操作,比如第二種方法裡的db_commit_all()函式

2.使用第二種方法,直接複製這些函式到指令碼中即可,如果使用第一種方法,則需要重寫相關函式,增加開發時間,浪費精力。

3.如果專案中是使用flask進行開發,推薦使用第二種方法進行資料庫更新。

奧數 python python 來做奧數題

來做幾道小學奧數題 紅花映綠葉 春 葉綠映花紅 我們熱愛科學 學 好好好好好好 少年早立志向 少年早立志向 有志何懼少年 學生 好學生 三好學生 2004 資料發生器原始碼 data generator.py encoding utf8 import itertools class datagene...

Python python中的隨機數函式

choice seq 從序列的元素中隨機挑選乙個元素,比如random.choice range 10 從0到9中隨機挑選乙個整數。randrange start,stop step 從指定範圍內,按指定基數遞增的集合中獲取乙個隨機數,基數預設值為1 random 隨機生成下乙個實數,它在 0,1 ...

yui datatable 更新table資料

使用render可以重新渲染datatable,之前新增的樣式等資訊也想相應會初始化,另外行定位等也會失效 使用updaterows方法不會刪除樣式等資訊 var records mytable.getrecordset getrecords varodata,i,len,newspeed for ...