非常白痴的问题,插入数据库提交失败,一直走IntegrityError错误,没打印错误信息,以为插不进去,弄了好久,最后打印了错误信息
(sqlite3.IntegrityError) samples.file_type may not be NULL [SQL: u‘INSERT INTO samples (file_size, file_type, md5, crc32, sha1, sha256, sha512, ssdeep) VALUES (?, ?, ?, ?, ?, ?, ?, ?)‘] [parameters: (53, None, ‘hello‘, ‘crc32‘, ‘sha1‘, ‘sha256‘, ‘sha512‘, None)]
提示为file_type不能为空,但是我插入的时候file_type设置的是空
发出来第一是注意打印错误信息;第二设置非空数据一定不能传NULL,Python目前用的少比较容易忘
from sqlalchemy import Column, Integer, String from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Text, Index from sqlalchemy.exc import SQLAlchemyError, IntegrityError from sqlalchemy.orm import sessionmaker Base = declarative_base() class Sample(Base): """Submitted files details.""" __tablename__ = "samples" id = Column(Integer(), primary_key=True) file_size = Column(Integer(), nullable=False) file_type = Column(Text(), nullable=False) md5 = Column(String(32), nullable=False) crc32 = Column(String(8), nullable=False) sha1 = Column(String(40), nullable=False) sha256 = Column(String(64), nullable=False) sha512 = Column(String(128), nullable=False) ssdeep = Column(String(255), nullable=True) __table_args__ = Index("hash_index", "md5", "crc32", "sha1", "sha256", "sha512", unique=True), def __repr__(self): return "<Sample(‘{0}‘,‘{1}‘)>".format(self.id, self.sha256) def __init__(self, md5, crc32, sha1, sha256, sha512, file_size, file_type=None, ssdeep=None): self.md5 = md5 self.sha1 = sha1 self.crc32 = crc32 self.sha256 = sha256 self.sha512 = sha512 self.file_size = file_size if file_type: self.file_type = file_type if ssdeep: self.ssdeep = ssdeep engine = create_engine("sqlite:///cuckoo.db",echo=True) #返回连接引擎 metadata = Base.metadata metadata.create_all(engine) #连接或者创建 #sample_mike = Sample("hello", "crc32", "sha1", "sha256", "sha512", 53, None, None) #非空数据不能传空
sample_mike = Sample("hello", "crc32", "sha1", "sha256", "sha512", 53, "aa", None)
Session = sessionmaker(bind=engine) session = Session() session.add(sample_mike) #数据库会话try: session.commit() #提交except IntegrityError as e: print eexcept SQLAlchemyError as e: print e
最后在网上看到一个比较有用的工具sqlite,查看db数据库
sqlite、db、insert.py下载 http://files.cnblogs.com/files/aliflycoris/db.zip
时间: 2024-10-21 23:46:01