抄下 sqlx 文档的代码
// 定义结构体
type Person struct {
FirstName string `db:"first_name"`
LastName string `db:"last_name"`
Email string `db:"email"`
}
// 创建对象
personStructs := Person{FirstName: "Ardie", LastName: "Savea", Email: "[email protected]"}
// 插入到数据库
// sqlx 将 sql 语言中的“:field”替换为对象字段的值
_, err = db.NamedExec(`INSERT INTO person (first_name, last_name, email)
VALUES (:first_name, :last_name, :email)`, personStructs)
1
NoOneNoBody 2023-07-23 22:09:39 +08:00
dataclass + f-format/string.Template ?
sql 执行需要另外实现 |
2
Vegetable 2023-07-23 22:40:27 +08:00
https://pymysql.readthedocs.io/en/latest/modules/cursors.html#pymysql.cursors.Cursor.execute
execute("INSERT INTO person (first_name, last_name, email) VALUES (%(first_name)s, %(last_name)s, %(email)s)", args=dict(first_name="",last_name="",email="")) 只需要解决 class to dict 就行了,方案多的是 有点原始了,这不就是 ORM 吗?这种不上不下的操作是什么特殊的需求 |
3
knightdf 2023-07-24 09:12:49 +08:00 1
随便上个 ORM 都比这个强吧?轻量的 peewee ,或者直接 sqlalchemy
|
4
craftx OP @NoOneNoBody 直接格式化 sql ,不太好吧?
|
6
NoOneNoBody 2023-07-24 21:13:56 +08:00
|