SQLAlchemy 訪問Mysql數(shù)據(jù)庫彈出Warning,VARIABLE_VALUE,如何解決?
問題描述
1.寫了一段使用SQLAlchemy 編程的代碼,程序本身沒問題,就是訪問數(shù)據(jù)庫時(shí)產(chǎn)生一個(gè)錯(cuò)誤提示,但是即使有錯(cuò)誤代碼頁能正常運(yùn)行,操作結(jié)果也正確。這個(gè)錯(cuò)誤是關(guān)于代碼編碼方式的錯(cuò)誤,但是調(diào)了很長時(shí)間這個(gè)錯(cuò)誤就是不能消除,求解救
2.這里是代碼:(1)orm.py
from sqlalchemy.ext.declarative import declarative_basefrom sqlalchemy import Column,Integer,StringBase = declarative_base()class Account(Base): __tablename__ = u’account’ id = Column(Integer,primary_key = True) salary = Column(Integer) username = Column(String(20),nullable = False) password = Column(String(200),nullable = False) title = Column(String(50))def is_active(self):return True def get_id(self):return self.id def is_authenticated(self):return True def is_anonymous(self):return Falsefrom sqlalchemy import create_enginefrom sqlalchemy.orm import scoped_session,sessionmakerdb_connect_string = ’mysql://root:admin@localhost:3306/test_database?charset=utf8’ssl_args = {’ssl’:{’cert’:’/home//ssl/client-cert.pem’, ’key’:'/home/shouse/ssl/client-cert.pem', ’ca’:’/home/shouse/ssl/ca-cert.pem’}}engine = create_engine(db_connect_string,connect_args = ssl_args)SessionType = scoped_session(sessionmaker(bind = engine,expire_on_commit = False))def GetSession(): return SessionType()from contextlib import contextmanager@contextmanagerdef session_scope(): session = GetSession() try:yield sessionsession.commit() except:session.rollback()raise finally:session.close()
(2)databaseoperation.py代碼:
# -*- coding:utf-8 -*-import ormfrom sqlalchemy import or_def InsertAccount(user,password,title,salary): with orm.session_scope() as session:account = orm.Account(username = user,password =password,title = title,salary = salary)session.add(account)def GetAccount(id=None,username = None): with orm.session_scope() as session:return session.query(orm.Account).filter(or_(orm.Account.id==id,orm.Account.username==username)).first()def Delete(username): with orm.session_scope() as session:account = GetAccount(username = username) if account: session.delete(account)def Update(id,username,password,title,salary): with orm.session_scope() as session:account = session.query(orm.Account).filter(orm.Account.id==id).first()if not account:returnaccount.username=usernameaccount.password=passwordaccount.title = titleaccount.salary = salaryInsertAccount(’Lliy’,’123’,’System Manager’,3000)InsertAccount(’Rebeca Li’,’12221’,’Accountant’,3000)GetAccount(2)Delete(’David Li’)Update(1,’admin’,’1111’,’System Admin’,2000)
3.錯(cuò)誤提示:
4.數(shù)據(jù)庫截圖
問題解答
回答1:這是一個(gè)警告Warning,而不是錯(cuò)誤Error,但是不知道觸發(fā)那種機(jī)制導(dǎo)致代碼反饋這種警告Warning
