Way ORM database link

Way ORM database link

Mysql native connector
1. Connect database
Conn = Connect (
Host = 'localhost',
Port = 3306,
Database = 'python_db',
User = 'the root',
password = '123123',
charset = 'UTF8')
2. Get the cursor
cur conn.cursor = ()
3. operation database
operation: sql_str = '' 'delete from students where name =' King steel egg ';' ''
execute: row_count = cur.execute (sql_str)
submitted: conn.commit ()
Gets results: result = cur.fetchall ()
4. Close the cursor
cur.close ()
5. The database closed
conn.Close ()

MysqlORM connecting
the Flask
Mysql
the Flask connected to the SQLALchemy
SQLALCHEMY_DATABASE_URI = 'mysql + pymysql: // username: password @IP: port number / database'
SQLALCHEMY_DATABASE_URI = 'mysql + pymysql: // Haitang: [email protected]: 3306 / Test'
app is registered in
app.config [ 'SQLALCHEMY_DATABASE_URI']
Create sqlalchemy example
db = SQLAlchemy (app)
added to the instance Manager
manager.add_command ( 'DB', MigrateCommand)
configure the default Redis
REDIS_HOST = "127.0.0.1" the IP
REDIS_PORT = 6379 port
REDIS_NUM = 8 first several databases
SECRET_KEY = "ASLKDJALKSJDALSDJALKSDJASLKDJ98AD9" encrypted string
sESSION_TYPE = 'redis' flask by-packet session, the session from the memory storage location flask to adjust the configuration information redis
SESSION_REDIS = StrictRedis (host = REDIS_HOST, port = REDIS_PORT, db = REDIS_NUM) instance of an object
SESSION_USE_SIGNER = True Open Database encryption
SESSION_PERMANENT = False canceled permanently stored
PERMANENT_SESSION_LIFETIME = 86400 set the default effective duration
instance of an object
redis_store = StrictRedis (host = REDIS_HOST, port = REDIS_PORT, db = REDIS_NUM)
using
redis_store.setex (key valid time, value)

Django
Mysql
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'the HOST': '127.0.0.1', # database host
'PORT': 3306, # database port
'USER': 'root', # database username
'pASSWORD': 'mysql', # database user password
'nAME': 'django_demo' # database name
}
}
Redis
configuration: CACHES = {
"sms_code": {
"BACKEND": "django_redis. cache.RedisCache ",
" LOCATION ":" Redis: //127.0.0.1: 6379/0 ",
" the OPTIONS ": {
" CLIENT_CLASS ":" django_redis.client.DefaultClient ",
}
...
...
...
...
}
connection: redis_cli = get_redis_connection ( 'sms_code')

Guess you like

Origin www.cnblogs.com/xingkongzhizhu/p/11627735.html