# MysqldbPythonWrapper
**mysqldb_wrapper** is a python module that will help you easily communicate with your database and provides encryption with [Fernet](https://cryptography.io/en/latest/fernet/) using the [cryptography](https://cryptography.io/en/latest/) module. This module is inspired by [SQLAlchemy](https://www.sqlalchemy.org/) but is even simpler to use and has less limitations (but is also less complete).
**IMPORTANT:** This module is only made to use with simple mysql queries. You can still use it with more complex queries, but no interface is provided for those.
## Installation
This module is available via pip:
```sh
pip install mysqldb_wrapper
```
## Usage
### The table
First, you need to create a `table`. Here's an example:
```python
from mysqldb_wrapper import Base, Id
class Test(Base):
__tablename__ = "test"
id = Id()
hashed = bytes()
number = int(1)
string = str("string")
boolean = bool(True)
```
To explain in details:
- `id = Id()` is, as its name implies, the database object `id`. **Always put one Id**.
- `hashed = bytes()` will be a hashed string, always declare it empty.
- `number = int(1)` will be a int with a default value `1`.
- `string = str("string")` will be a string with a default value `string`.
- `boolean = bool(True)` will be a boolean with a default value `boolean`.
Easy, right ?
**Note:** This implementation does not support complex types, so keep your table simple ;)
### The database
You need to create your database by hand Although this module will not create the database for you, it will create the tables automatically !
**Note:** keep in mind that if your table change (remove fields, change field types, ...), you will need to remove/update them by hand.
To initialize a session with the database, use:
```python
from mysqldb_wrapper import Session
session = Session(DB_USERNAME, DB_PASSWORD, DB_TEST, FERNET_KEY)
```
So here we have:
- `DB_USERNAME` is the username of the mysql user
- `DB_PASSWORD` is the password of the mysql user
- `DB_TEST` is the name of the database
- `FERNET_KEY` is the encryption key. You can create one with `Fernet.generate_key()`, but keep in mind to always use the same one. (So generate one by hand then keep it somewhere safe)
And now we have all we need to use the mysql database.
### Operations
#### Adding an object
Remember your table ? Just create a new object from it and use the `add` method of the session with it. Also, the table objects supports `**kwargs`.
```python
obj = Test(hashed="abcd", string="word")
session.add(obj)
```
And `obj` now automatically contains the id generated by the database. Also, this method returns the object in case you want to chain methods.
#### Updating an object
Using the `obj` from before:
```python
obj.number = 2553166
obj.string = "a sentence"
obj.boolean = True
session.update(obj)
```
This method also returns the object.
#### Deleting an object
And again:
```python
session.delete(obj)
```
`obj` now is not in the database anymore but you can still continue to use it, just remember it doesn't have an `id` (set to 0). Like the others, this method returns the object.
#### Querying objects
We have more to talk about in this section.
```python
obj = session.query(Test).first()
```
This one is easy. You put the table you want in `query()` and you want to get the first result.
```python
obj_list = session.query(Test).all()
```
Same than before, except this time you retrieve all the results. It is guaranteed to return a list, even if empty.
Now the tricky part:
```python
obj = session.query(Test).where(Test.id == 2).first()
```
This time, we are still querying in the Test table, but we want a specific result, the one where the id is equal to 2. You don't have to worry about doing anything more, everything is handled by the `Base` class your `Test` table inherited from.
Remember we had a hash in our table ? Don't worry, it is handled as well. How does it work ? Like the others field. =)
```python
obj = session.query(Test).where(Test.hashed == "abcd").first()
```
You can also chain the `where` methods, use variables and query all objects:
```python
a_string = "something"
list_obj = session.query(Test).where(Test.id == 2).where(Test.hashed == a_string).all()
```
**IMPORTANT: The `where` method only works with equality as of now. Also, you can only query by id and hashes.**
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
共22个文件
py:7个
txt:5个
pkg-info:2个
资源分类:Python库 所属语言:Python 资源全名:mysqldb_wrapper-0.1.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
资源推荐
资源详情
资源评论
收起资源包目录
mysqldb_wrapper-0.1.tar.gz (22个子文件)
mysqldb_wrapper-0.1
PKG-INFO 363B
test_teardown.sh 406B
test
database
test.py 266B
suite
database.py 4KB
crypt.py 1KB
mysqldb_wrapper
crypt.py 2KB
__init__.py 8KB
LICENSE 1KB
mysqldb_wrapper.egg-info
PKG-INFO 363B
requires.txt 25B
not-zip-safe 1B
SOURCES.txt 460B
top_level.txt 16B
dependency_links.txt 1B
test_setup.sh 507B
setup.cfg 38B
requirements.txt 25B
setup.py 530B
.gitignore 2KB
config.py 215B
README.md 4KB
constants.json 110B
共 22 条
- 1
资源评论
挣扎的蓝藻
- 粉丝: 12w+
- 资源: 15万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功