{"id":141,"date":"2022-03-15T23:31:25","date_gmt":"2022-03-15T14:31:25","guid":{"rendered":"https:\/\/www.kd2.jp\/memo3\/?p=141"},"modified":"2022-03-15T23:39:57","modified_gmt":"2022-03-15T14:39:57","slug":"sqlalchemy%e3%81%a7hello-world","status":"publish","type":"post","link":"https:\/\/www.kd2.jp\/memo3\/?p=141","title":{"rendered":"SQLAlchemy\u3067Hello World (OR\u30de\u30c3\u30d4\u30f3\u30b0)"},"content":{"rendered":"<p>Python 3.8.10<br \/>\npsycopg\u304c\u5fc5\u8981\u3002<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;eclipse&quot;,&quot;lineNumbers&quot;:true,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">from sqlalchemy import create_engine\r\nfrom sqlalchemy import Column, Integer, String\r\nfrom sqlalchemy.ext.declarative import declarative_base\r\nfrom sqlalchemy.orm import sessionmaker\r\n\r\ndb_string = \"postgresql:\/\/user:password@localhost:5432\/testdb\"\r\n\r\ndb = create_engine(db_string)\r\nbase = declarative_base()\r\n\r\n# OR\u30de\u30c3\u30d4\u30f3\u30b0\r\nclass Hello(base):\r\n    __tablename__ = 'hello'\r\n\r\n    id = Column(Integer, primary_key=True)\r\n    msg = Column(String)\r\n\r\nSession = sessionmaker(db)\r\nsession = Session()\r\n\r\n# \u30c6\u30fc\u30d6\u30eb\u304c\u5b58\u5728\u3057\u306a\u3051\u308c\u3070\u4f5c\u6210\u3002\r\nbase.metadata.create_all(db)\r\n\r\n# INSERT\r\ndata = Hello(msg=\"Hello World!\")\r\nsession.add(data)\r\nsession.commit()\r\n\r\n# SELECT\r\nselect = session.query(Hello)\r\nfor row in select:\r\n    print(row.msg)\r\n\r\n# UPDATE\r\ndata.msg = \"Hello Wolrd! 2\"\r\nsession.commit()\r\n\r\n# SELECT\r\nselect = session.query(Hello)\r\nfor row in select:\r\n    print(row.msg)\r\n\r\n# Delete\r\nsession.delete(data)\r\nsession.commit()<\/pre>\n<\/div>\n<p>\u5b9f\u884c\u7d50\u679c<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;null&quot;,&quot;mime&quot;:&quot;text\/plain&quot;,&quot;theme&quot;:&quot;eclipse&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">python test.py\r\nHello World!\r\nHello Wolrd! 2<\/pre>\n<\/div>\n<p>\u4f5c\u6210\u3055\u308c\u305f\u30c6\u30fc\u30d6\u30eb<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;null&quot;,&quot;mime&quot;:&quot;text\/plain&quot;,&quot;theme&quot;:&quot;eclipse&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">testdb=&gt; \\d hello;\r\n                                 Table \"public.hello\"\r\n Column |       Type        | Collation | Nullable |              Default\r\n--------+-------------------+-----------+----------+-----------------------------------\r\n id     | integer           |           | not null | nextval('hello_id_seq'::regclass)\r\n msg    | character varying |           |          |\r\nIndexes:\r\n    \"hello_pkey\" PRIMARY KEY, btree (id)<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python 3.8.10 psycopg\u304c\u5fc5\u8981\u3002 from sqlalchemy import create_engine from sqlalchemy import Column, Integer, String &hellip; <a class=\"more-link\" href=\"https:\/\/www.kd2.jp\/memo3\/?p=141\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"screen-reader-text\">SQLAlchemy\u3067Hello World (OR\u30de\u30c3\u30d4\u30f3\u30b0)<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[9,4],"tags":[],"_links":{"self":[{"href":"https:\/\/www.kd2.jp\/memo3\/index.php?rest_route=\/wp\/v2\/posts\/141"}],"collection":[{"href":"https:\/\/www.kd2.jp\/memo3\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kd2.jp\/memo3\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kd2.jp\/memo3\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kd2.jp\/memo3\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=141"}],"version-history":[{"count":5,"href":"https:\/\/www.kd2.jp\/memo3\/index.php?rest_route=\/wp\/v2\/posts\/141\/revisions"}],"predecessor-version":[{"id":146,"href":"https:\/\/www.kd2.jp\/memo3\/index.php?rest_route=\/wp\/v2\/posts\/141\/revisions\/146"}],"wp:attachment":[{"href":"https:\/\/www.kd2.jp\/memo3\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kd2.jp\/memo3\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kd2.jp\/memo3\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}