0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

DB操作 

0
Posted at

メモなので過度な期待はよしてください。
少しでも役に立てれば嬉しいです。

DB作成・取り出し

SQLiteを使わない方式で。

mkdb.py
from sqlalchemy import Column, Integer, String, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

engine = create_engine('sqlite:///test.db')
Base = declarative_base()

# 多分ここはほとんどテンプレ
class User(Base):
    __tablename__ = 'test'

    id = Column(Integer, primary_key=True)
    hello = Column(String)
    good = Column(String)

    def __repr__(self):
        return "User<{}, {}, {}>".format(self.id, self.hello, self.good)


Base.metadata.create_all(engine)
SessionMaker = sessionmaker(bind=engine)
session = SessionMaker()

def add(hello,good):#この関数でDBにデータを追加する
    session.add(User(hello = hello, good = good))
    session.commit()

def get():#ここでDBのデータを取り出す
    test = session.query(User)
    print(type(test))
    for row in test:
        print("%s,%s"%(row.plan,row.date))

こんな感じです。テーブル作成とかはほとんどテンプレだと思うので、使うだけであれば深く考える必要はないと思います。

ちなみにこのプログラムは他のプログラムにimportして使うことを想定しているのでこのプログラム単体で動作することは確認していません。

0
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?