orm_sqlalchemy
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.5 KiB

from s1_models import *
from sqlalchemy import insert
# 方式1
ins1 = cookies.insert().values(
cookie_name="chocolate chip",
cookie_recipe_url="http://some.aweso.me/cookie/recipe.html",
cookie_sku="CC01",
quantity="12",
unit_cost="0.50"
)
# 方式2
ins2 = insert(cookies).values(
cookie_name="chocolate chip",
cookie_recipe_url="http://some.aweso.me/cookie/recipe.html",
cookie_sku="CC01",
quantity="12",
unit_cost="0.50"
)
# 打印 INSERT 语句的结构,不包含数据
print(str(ins1))
# 打印插入的数据
print(ins1.compile().params)
# 执行插入
# result1 = connection.execute(ins1)
# print(result1.inserted_primary_key)
# 方式3
ins3 = cookies.insert()
result3 = connection.execute(
ins3,
cookie_name="dark chocolate chip1",
cookie_recipe_url="http://some.aweso.me/cookie/recipe_dark.html",
cookie_sku="CC02",
quantity="1",
unit_cost="0.75"
)
# print(result3.inserted_primary_key)
ins4 = cookies.insert()
inventory_list = [
{
'cookie_name': 'peanut butter',
'cookie_recipe_url': 'http://some.aweso.me/cookie/peanut.html',
'cookie_sku': 'PB01',
'quantity': '24',
'unit_cost': '0.25'
},
{
'cookie_name': 'oatmeal raisin',
'cookie_recipe_url': 'http://some.okay.me/cookie/raisin.html',
'cookie_sku': 'EWW01',
'quantity': '100',
'unit_cost': '1.00'
}
]
result4 = connection.execute(ins4, inventory_list)
print(result4.inserted_primary_key_rows)