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

2021-08-14 06:17:52 字數 3844 閱讀 8829

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

第一種:使用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進行開發,推薦使用第二種方法進行資料庫更新。

flask-sqlalchemy中配置相關鏈結

python使用mysqldb操作mysql資料庫相關連線 

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

最近專案的兩次版本迭代中,根據業務需求的變化,需要對資料庫進行更新,兩次分別使用了不同的方式進行更新。第一種 使用python的mysqldb模組利用原生的sql語句進行更新 1 import mysqldb 2 主機名 3 host 127.0.0.1 4 使用者名稱 5 user root 6 ...

資料庫Schema兩種含義

1 資料庫schema有兩種含義,一種是概念上的schema,指的是一組ddl語句集,該語句集完整地描述了資料庫的結構。還有一種是物理上的 schema,指的是資料庫中的乙個名字空間,它包含一組表 檢視和儲存過程等命名物件。物理schema可以通過標準sql語句來建立 更新和修改。例 如以下sql語...

資料庫兩種驗證鏈結方式

第一種 server 伺服器名稱 資料庫的例項名 uid 登入名 pwd 密碼 database 資料庫名稱 第二種 data source 伺服器名稱 資料庫例項名 initial catalog 資料庫名稱 user id 使用者名稱 password 密碼 以 windows 身份驗證方式登入...