# pypdo **Repository Path**: dreammo/pypdo ## Basic Information - **Project Name**: pypdo - **Description**: 将pymysql封装成为类似PHP中的PDO操作数据库对象,让你操作python和PHP有一样的使用感觉。将python中的dict和PHP中的管理数组对应使用即可 - **Primary Language**: Python - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 3 - **Forks**: 2 - **Created**: 2018-10-29 - **Last Updated**: 2022-11-10 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # pypdo 将pymysql封装成为类似PHP中的PDO操作数据库对象,让你操作python和PHP有一样的使用感觉。将python中的dict和PHP中的管理数组对应使用即可 ### 初始化连接 ``` from pypdo.pdo import PDO config = { "host":"localhost", "user":"root", "password":"root", "port":3306, "charset":"UTF8", "db":"db", "debug":True } try: connect = PDO(**config) #get databse connect object except Exception as e: print(e) ``` #### 插入数据 ``` # insert many rows rows = [{"title":"hello world","conent":"conent"},{"title":"title2","conetnt":"content2"}] last_insert_id = connect.insert("table",rows) # insert single row row = {"title":"hello world","content":"content"} last_insert_id = connect.insert("table",row) ``` ### 更新数据 ``` where = {"id":1} update = {"title":"update"} # update table set title="update" where id = 1 affect_rows = connect.update("table",update,where) ``` ### 删除数据 ``` where = {"id":1} # delete from table where id = 1 affect_rows = connect.delete("table",where) ``` ### 查询数据 ``` cols = ["title","id"] where = ["id":[1,2,3]] order = {"id":"DESC"} offset = 0 limit = 100 # select title,id from table where id in (1,2,3) order by id desc limit 0,100 rows = connect.select(cols,"table",where,order,offset,limit) ``` ### model层 ,对pypdo再次封装.对于一个表写一个model类,table写的是对应操作的表名。 ``` from pypdo.model import Model class Test(Model): def __init__(self): Model.__init__(self) test_model = Test()    #插入    last_insert_id = test_model.add({"title":"title","content":"content"}) print(last_insert_id) ```