Python更新數(shù)據(jù)庫(kù)腳本兩種方法及對(duì)比介紹
最近項(xiàng)目的兩次版本迭代中,根據(jù)業(yè)務(wù)需求的變化,需要對(duì)數(shù)據(jù)庫(kù)進(jìn)行更新,兩次分別使用了不同的方式進(jìn)行更新。
第一種:使用python的MySQLdb模塊利用原生的sql語(yǔ)句進(jìn)行更新
import MySQLdb
#主機(jī)名
HOST = '127.0.0.1'
#用戶名
USER = "root"
#密碼
PASSWD = "123456"
#數(shù)據(jù)庫(kù)名
DB = "db_name"
# 打開(kāi)數(shù)據(jù)庫(kù)連接
db=MySQLdb.connect(HOST,USER,PASSWD,DB)
# 獲取操作游標(biāo)
cursor=db.cursor()
if __name__ == '__main__':
if cursor:
command_a = "update tables_one set status=5 where status=0"
# 使用execute方法執(zhí)行SQL語(yǔ)句
cursor.execute(command_a)
# 提交到數(shù)據(jù)庫(kù)執(zhí)行
db.commit()
command2 = "select field from tables_one where id =12"
ret2 = cursor.execute(command2)
# 獲取所有記錄列表
ret2=cursor.fetchall()
for item in ret2:
command3 = "insert into tables_two(name) values (%s);" % (item[0])
fin=cursor.execute(command3)
db.commit()
# 關(guān)閉數(shù)據(jù)庫(kù)連接
db.close()
數(shù)據(jù)庫(kù)查詢?nèi)N方式
- fetchone(): 該方法獲取下一個(gè)查詢結(jié)果集。結(jié)果集是一個(gè)對(duì)象
- fetchall():接收全部的返回結(jié)果行.
- rowcount: 這是一個(gè)只讀屬性,并返回執(zhí)行execute()方法后影響的行數(shù)。
第二種:使用python的框架flask和sqlalchemy進(jìn)行更新
# -*- coding:utf-8 -*-
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.sql import text
HOST = '127.0.0.1'
USER = "root"
PASSWD = "123456"
DB = "carrier_test"
CHARTSET = "utf8"
app = Flask(__name__,instance_relative_config = True)
#鏈接數(shù)據(jù)庫(kù)路徑
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://%s:%s@127.0.0.1:3306/%s?charset=%s' %(USER,PASSWD,DB,CHARTSET)
#如果設(shè)置成 True (默認(rèn)情況),F(xiàn)lask-SQLAlchemy 將會(huì)追蹤對(duì)象的修改并且發(fā)送信號(hào)。這需要額外的內(nèi)存, 如果不必要的可以禁用它。
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
#如果設(shè)置成 True,SQLAlchemy 將會(huì)記錄所有 發(fā)到標(biāo)準(zhǔn)輸出(stderr)的語(yǔ)句,這對(duì)調(diào)試很有幫助。
app.config['SQLALCHEMY_ECHO'] = False
# 數(shù)據(jù)庫(kù)連接池的大小。默認(rèn)是數(shù)據(jù)庫(kù)引擎的默認(rèn)值 (通常是 5)。
app.config['SQLALCHEMY_POOL_SIZE'] = 6
db = SQLAlchemy(app)
class Table_one(db.Model):
__tablename__ = 'table_one'
id = db.Column('id', db.Integer, primary_key=True, autoincrement=True)
com_name = db.Column('com_name', db.String(30), nullable=False)
com_about = db.Column('com_about', db.String(200), nullable=False)
def __repr__(self):
return '<table_one com_name %r>' % self.com_name
class Table_two(db.Model):
__tablename__ = 'table_two'
id = db.Column('id', db.Integer, primary_key=True, autoincrement=True)
reason = db.Column('reason', db.String(128), nullable=True)
create_time = db.Column('create_time', db.TIMESTAMP, server_default=text('now()'))
status = db.Column('status', db.Integer, nullable=False, default=0)
def __repr__(self):
return '<table_two id %r>' % self.id
def db_commit_all(lists):
try:
db.session.add_all(lists)
db.session.commit()
return 'SUCCESS'
except Exception,e:
return 'Fail!!!'
def commits_to_three_judge():
com_sta_obj = Table_one.query.filter_by(com_name='只是測(cè)試使用,不用關(guān)心表間關(guān)系').all()
for ite in com_sta_obj:
ship_obj = Table_two.query.filter_by(id=ite.id).first()
if ship_obj:
if int(ship_obj.status) == 2:
ite.status = 0
print db_commit_all([ite])
print '表同步結(jié)束'
64
if __name__=='__main__':
#執(zhí)行更新數(shù)據(jù)庫(kù)函數(shù)
commits_to_three_judge()
兩種方式對(duì)比:
1.在實(shí)際項(xiàng)目中,數(shù)據(jù)庫(kù)的更新 需要用到很多相關(guān)函數(shù)進(jìn)行數(shù)據(jù)的收集,判斷是否滿足條件等,而這些相關(guān)函數(shù)在項(xiàng)目中都是用 Sqlalchemy進(jìn)行數(shù)據(jù)相關(guān)操作,比如第二種方法里的db_commit_all()函數(shù)
2.使用第二種方法,直接復(fù)制這些函數(shù)到腳本中即可,如果使用第一種方法,則需要重寫相關(guān)函數(shù),增加開(kāi)發(fā)時(shí)間,浪費(fèi)精力。
3.如果項(xiàng)目中是使用flask進(jìn)行開(kāi)發(fā),推薦使用第二種方法進(jìn)行數(shù)據(jù)庫(kù)更新。
總結(jié)
以上所述是小編給大家介紹的Python更新數(shù)據(jù)庫(kù)腳本兩種方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Python實(shí)戰(zhàn)之Elasticsearch的高級(jí)實(shí)現(xiàn)詳解
- python中的elasticsearch_dsl查詢語(yǔ)句轉(zhuǎn)換成es查詢語(yǔ)句詳解
- python中elasticsearch_dsl模塊的使用方法
- python 使用elasticsearch 實(shí)現(xiàn)翻頁(yè)的三種方式
- Python操作Elasticsearch處理timeout超時(shí)
- python3實(shí)現(xiàn)elasticsearch批量更新數(shù)據(jù)
- python更新數(shù)據(jù)庫(kù)中某個(gè)字段的數(shù)據(jù)(方法詳解)
- Python調(diào)用Elasticsearch更新數(shù)據(jù)庫(kù)的操作方法
相關(guān)文章
Python算法之求n個(gè)節(jié)點(diǎn)不同二叉樹(shù)個(gè)數(shù)
本文先向大家分享了建立二叉樹(shù)的簡(jiǎn)單代碼,其次介紹了Python計(jì)算n個(gè)節(jié)點(diǎn)不同二叉樹(shù)個(gè)數(shù)的問(wèn)題及實(shí)現(xiàn)代碼示例,具有一定參考價(jià)值,需要的朋友可以了解下。2017-10-10
Python個(gè)人博客程序開(kāi)發(fā)實(shí)例信息顯示
這篇文章主要介紹了怎樣用Python來(lái)實(shí)現(xiàn)一個(gè)完整的個(gè)人博客系統(tǒng),我們通過(guò)實(shí)操上手的方式可以高效的鞏固所學(xué)的基礎(chǔ)知識(shí),感興趣的朋友一起來(lái)看看吧2022-12-12
python計(jì)算機(jī)視覺(jué)opencv卡號(hào)識(shí)別示例詳解
這篇文章主要為大家介紹了python計(jì)算機(jī)視覺(jué)opencv卡號(hào)識(shí)別的實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下 希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11
jupyter notebook 調(diào)用環(huán)境中的Keras或者pytorch教程
這篇文章主要介紹了jupyter notebook 調(diào)用環(huán)境中的Keras或者pytorch教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04
python關(guān)于倒排列的知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家分享的是一篇關(guān)于python關(guān)于倒排列的知識(shí)點(diǎn)總結(jié),有需要的朋友們可以參考下。2020-10-10
3行Python代碼實(shí)現(xiàn)圖像照片摳圖和換底色的方法
這篇文章主要介紹了3行Python代碼實(shí)現(xiàn)圖像照片摳圖和換底色的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
將不規(guī)則的Python多維數(shù)組拉平到一維的方法實(shí)現(xiàn)
這篇文章主要介紹了將不規(guī)則的Python多維數(shù)組拉平到一維的方法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
python訪問(wèn)抓取網(wǎng)頁(yè)常用命令總結(jié)
這篇文章主要介紹了python訪問(wèn)抓取網(wǎng)頁(yè)常用命令的相關(guān)資料,需要的朋友可以參考下2017-04-04

