简体   繁体   English

使用核心SQLAlchemy插入和更新

[英]Insert and update with core SQLAlchemy

I have a database that I don't have metadata or orm classes for (the database already exists). 我有一个数据库,我没有元数据或orm类(数据库已经存在)。

I managed to get the select stuff working by: 我设法通过以下方式获得选择的东西:

from sqlalchemy.sql.expression import ColumnClause
from sqlalchemy.sql import table, column, select, update, insert
from sqlalchemy.ext.declarative import *
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
import pyodbc

db = create_engine('mssql+pyodbc://pytest')
Session = sessionmaker(bind=db)
session = Session()

list = []
list.append (column("field1"))
list.append (column("field2"))
list.append (column("field3"))

s = select(list)
s.append_from('table')
s.append_whereclause("field1 = 'abc'")
s = s.limit(10)

result = session.execute(s)
out = result.fetchall()

print(out)

So far so good. 到现在为止还挺好。

The only way I can get an update/insert working is by executing a raw query like: 我可以获得更新/插入工作的唯一方法是执行原始查询,如:

session.execute(<Some sql>)

I would like to make it so I can make a class out of that like: 我想这样做,所以我可以创建一个类,如:

u = Update("table")
u.Set("file1","some value")
u.Where(<some conditon>)

seasion.execute(u)

Tried (this is just one of the approaches I tried): 尝试过(这只是我尝试过的方法之一):

i = insert("table")
v = i.values([{"name":"name1"}, {"name":"name2"}])

u = update("table")
u = u.values({"name": "test1"})

I can't get that to execute on: 我不能让它执行:

session.execute(i)

or 要么

session.execute(u)

Any suggestion how to construct an insert or update without writing ORM models? 任何建议如何在不编写ORM模型的情况下构造插入或更新?

As you can see from the SQLAlchemy Overview documentation, sqlalchemy is build with two layers: ORM and Core . SQLAlchemy Overview文档中可以看出,sqlalchemy是用两层构建的: ORMCore Currently you are using only some constructs of the Core and building everything manually. 目前,您只使用Core一些构造并手动构建所有内容。

In order to use Core you should let SQLAlchemy know some meta information about your database in order for it to operate on it. 为了使用Core您应该让SQLAlchemy知道有关您的数据库的一些元信息,以便它对它进行操作。 Assuming you have a table mytable with columns field1, field2, field3 and a defined primary key , the code below should perform all the tasks you need: 假设您有一个表mytable其中包含列field1, field2, field3和一个已定义的primary key ,下面的代码应该执行您需要的所有任务:

from sqlalchemy.sql import table, column, select, update, insert

# define meta information
metadata = MetaData(bind=engine)
mytable = Table('mytable', metadata, autoload=True)

# select
s = mytable.select() # or:
#s = select([mytable]) # or (if only certain columns):
#s = select([mytable.c.field1, mytable.c.field2, mytable.c.field3])
s = s.where(mytable.c.field1 == 'abc')
result = session.execute(s)
out = result.fetchall()
print(out)

# insert
i = insert(mytable)
i = i.values({"field1": "value1", "field2": "value2"})
session.execute(i)

# update
u = update(mytable)
u = u.values({"field3": "new_value"})
u = u.where(mytable.c.id == 33)
session.execute(u)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM