一个 mysql, sqlite 数据库工具 for python3+
(注: 不支持 python2)
pip3 install zhudb
from zhudb import ZhuSqlite
from zhudb import ZhuMysql
db = ZhuSqlite('dbname')
db = ZhuMysql(
host="127.0.0.1",
user="name",
passwd="pwd",
database="test"
)
fields = ["fname1", "fname2", "fname3"]
db.create_table("test_table", fields<, noid=True>)
- 字段默认 "varchar(128)" 类型
- noid, 创建 table 时不自动创建 'id' 字段, 默认为 False
给每个字段指定类型
fields = [
("fname1", "varchar(128)"),
("fname2", "int not null")
]
db.create_table("test_table", fields<, noid=True>)
返回:
- True -> 创建完成
- False -> 未能创建
db.get_columns()
返回: 字段名 list [' ', ' ']
db.query('SELECT * FROM table')
返回 list 包含 tuple: [ ( ), ( ) ]
db.query_dict('SELECT * FROM table')
返回 list 包含 dict: [ { }, { } ]
y = self.query_iter('SELECT * FROM table')
for curent_row in y:
print(curent_row)
返回 yeild 对象
new_line = {
"field_1": "abc",
"field_2": 123
}
db.insert("test_table", **new_line)
返回 最后插入行的 id
new_line = {
"field_1": "abc",
"field_2": 123
}
db.insert_if_not_exist("test_table", <bykey=['key1', 'key2'], > **new_line)
bykey: 检查哪些字段需要检查重复, 如果 'bykey' 留空, 则检查所有字段 (新行的所有字段的值此前都没有出现过才会插入)
返回
- False -> already existed
- True -> insert completed
data = [
(1, "v1", 123, "v3"),
(2, "val", 567, "val4")
]
db.insert_lines("test_table", data)
fields = ['f1', 'f2', 'f3']
data = [
('wen', 12, 'beijing'),
('zhu', 17, 'chengdu'),
]
db.insert_many("table_name", fields, data)