日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

运用SQLAlchemy

發布時間:2025/3/20 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 运用SQLAlchemy 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
  • result = engine.execute(s)
  • for row in result:
  • Info["UserId"]=row[0]
  • Info["UserTitle"] = row[1]
  • Info["UserCode"] = row[2]
  • Info["UserType"] = row[3]
  • result.close()
  • 返回的result為一個元組,


    echo設置為True,目的是SqlAlchemy將會把執行sql命令的過程輸出到標準輸出。這主要是便于調試,但如果是用于生產環境中應該設置為False。
    ? ??客戶端連接到數據庫postgresql:
    客戶端connect >sqlalchemy->psycopg2->DBAPI->postgresql
    = ('postgresql+psycopg2://postgres:root@localhost:5432/ttt'=True)創建引擎:指定 ? ? 要連接的數據庫+數據庫接口://帳號:密碼@localhost:5432/數據庫名稱?
    metadata元數據,實例化metadata,綁定到engine上,相當于初始化了表結構
    user = Table('book',metadata,
    Column('id',Integer,primary_key=True),
    Column('name',String(20)),
    )用Table來創建表,表的名稱,類型,字段定義user不是表名,是數據表book對應的類名
    (=True)創建數據表,checkfirst=True,數據庫相關對象已經存在的化就不會在創建了
    定義列 ? 查詢行
    When we use?literal(文字的)?strings, the Core can’t?adapt(適應)?our SQL to work on different database backends.?
    也就是不推薦使用原生sql查詢

    插入數據最方便的插入user.insert().execute([{'id':6,'name':'1183532@qq.com'},{'id':5,'name':'118352@qq.com'}])
    user.insert().execute(id=7, name='hello')
    刪除指定數據:coon.execute(user.delete().where(user.c.id == 7))清空表中的數據:coon.execute(user.delete())
    更新數據s = user.update().where(user.c.id == 7).values(name='say hello')
    r = coon.execute(s)
    查詢數據:s = select([user.c.name,user.c.id])
    result = coon.execute(s)
    for row in result:
    print rows = select([user,])
    result = coon.execute(s)
    for row in result:
    print row



    #coding:utf-8
    from sqlalchemy import create_engine
    from sqlalchemy import MetaData,Column,Sequence,ForeignKey,Integer
    from sqlalchemy import Table,String
    from sqlalchemy.sql import select,text
    engine = create_engine('postgresql+psycopg2://postgres:root@localhost:5432/ttt', echo=True)
    metadata = MetaData()
    metadata.bind = engine
    #數據庫表與對象之間的映射關系
    user = Table('book',metadata,
    Column('id',Integer,primary_key=True),
    Column('name',String(20)),
    )
    address = Table('address',metadata,
    Column('id',Integer,primary_key=True),
    Column('user_id',None,ForeignKey('user.id')),
    Column('email',String(60),nullable=False),
    )
    #創建數據表
    metadata.create_all(checkfirst=True)























































































    來自為知筆記(Wiz)

    轉載于:https://www.cnblogs.com/wuqingzangyue/p/5770026.html

    總結

    以上是生活随笔為你收集整理的运用SQLAlchemy的全部內容,希望文章能夠幫你解決所遇到的問題。

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