{"id":2907,"date":"2021-03-15T00:44:46","date_gmt":"2021-03-15T00:44:46","guid":{"rendered":"https:\/\/www.tejwin.com\/en\/insights\/%e3%80%90introduction2%e3%80%91building-your-own-database-through-tej-api\/"},"modified":"2021-03-15T00:44:46","modified_gmt":"2021-03-15T00:44:46","slug":"%e3%80%90introduction2%e3%80%91building-your-own-database-through-tej-api","status":"publish","type":"insight","link":"https:\/\/www.tejwin.com\/en\/insights\/%e3%80%90introduction2%e3%80%91building-your-own-database-through-tej-api\/","title":{"rendered":"Building your own database through TEJ\u00a0API(II)"},"content":{"rendered":"<figure class=\"wp-block-image\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/17252wDjgTBO5xNCWf9CTQg.jpg\"\/><\/figure>\n<p class=\"wp-block-paragraph\">\u2013 update your database automatically<\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">Continuing with the TEJ API data collecting\/reading\/saving instructions which we have learned in the previous episode, <strong>this episode focuses on the way to update your database automatically.<\/strong> It is consistent with the previous episode, so friends who have not read the previous episode, please go back and read the introduction (1) ~<\/p>\n<\/blockquote>\n<p class=\"wp-block-paragraph\"><strong>link\uff1a<a class=\"ek-link\" href=\"\/en\/insight\/building-your-own-database-through-tej-api\/\">Building your own database through TEJ API<\/a><\/strong><\/p>\n<h2 class=\"wp-block-heading\"><strong>Highlights of this article <\/strong><\/h2>\n<ul class=\"wp-block-list\">\n<li><strong>TEJ API Data Collecting<\/strong><\/li>\n<li><strong>How to Update the Database<\/strong><\/li>\n<li><strong>Windows Task Scheduler<\/strong><\/li>\n<\/ul>\n<h2 class=\"wp-block-heading\"><strong>Links related to this article<\/strong><\/h2>\n<ul class=\"wp-block-list\">\n<li><strong>API Official Website: <\/strong><a href=\"https:\/\/api.tej.com.tw\/\" rel=\"noreferrer noopener\" target=\"_blank\"><strong>TEJ API Official Website<\/strong><\/a><\/li>\n<li><strong>The Product Package: <\/strong><a href=\"https:\/\/eshop.tej.com.tw\/E-Shop\/Edata_caseIntro\/1\" rel=\"noreferrer noopener\" target=\"_blank\"><strong>TEJ E SHOP<\/strong><\/a><\/li>\n<li><strong>Source Code: <\/strong><a href=\"https:\/\/github.com\/tejtw\/TEJAPI_Python_Medium_Rookies\" rel=\"noreferrer noopener\" target=\"_blank\"><strong>TEJ GITHUB<\/strong><\/a><\/li>\n<\/ul>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<h2 class=\"wp-block-heading\">TEJ API Data Collecting\uff0dUsing stock data as an\u00a0example<\/h2>\n<p class=\"wp-block-paragraph\">First we have to install the packages on Python. (tejapi, datetime, sqlite3)<\/p>\n<pre class=\"wp-block-preformatted\">pip install tejapi<br\/>pip install datetime<br\/>pip install sqlite3<\/pre>\n<p class=\"wp-block-paragraph\">The next step is to import these packages and set up <strong>your own api_key<\/strong>. If you don\u2019t have one, you can <a class=\"ek-link\" href=\"https:\/\/api.tej.com.tw\/trial.html\" rel=\"noreferrer noopener\" target=\"_blank\"><strong>apply for a trial <\/strong><\/a> on the TEJ API official website. But the trial version <strong>has limits on the range of data that could be obtained<\/strong><\/p>\n<pre class=\"wp-block-preformatted\">import tejapi<br\/>import datetime<br\/>import sqlite3<\/pre>\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">The database which will be used in this chapter is contained in \u5c0f\u8cc7\u65b9\u6848. If you want to use data without limit, check it on<a href=\"https:\/\/eshop.tej.com.tw\/E-Shop\/Edata_intro\" rel=\"noreferrer noopener\" target=\"_blank\"> TEJ E-shop<\/a><\/p>\n<\/blockquote>\n<pre class=\"wp-block-preformatted\">import tejapi<br\/>tejapi.ApiConfig.api_key = \u201cyour api_key\u201d<\/pre>\n<p class=\"wp-block-paragraph\">Now we can get the data through these codes\ud83d\udc40<\/p>\n<pre class=\"wp-block-preformatted\">TSMC = tejapi.get(<br\/>'TWN\/EWPRCD',  # database<br\/>coid = '2330',  # Stock\/Company symbol<br\/>mdate={'gte':'2020-01-01', 'lte':'2020-12-31'},  # date<br\/>paginate=True,  # data can be obtained in stages through this parameter<br\/>opts={'columns': ['mdate','open_d','high_d','low_d','close_d']},<br\/># columns select<br\/>)TSMC<\/pre>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/0SLUg1CsrFAOCwx1M.png\"\/><\/figure>\n<p class=\"wp-block-paragraph\">Saving data to Database:<\/p>\n<ol class=\"wp-block-list\">\n<li>Grab data through tejapi<\/li>\n<li>Connect to the database (example.db)<\/li>\n<li>Save data into the database (example.db)<\/li>\n<li>Close connection<\/li>\n<\/ol>\n<pre class=\"wp-block-preformatted\"># 1<br\/>TSMC = tejapi.get(<br\/>'TWN\/EWPRCD',  <br\/>coid = '2330',  <br\/>mdate={'gt':'2020-01-01'},  <br\/>paginate=True,  <br\/>opts={'columns': ['mdate','open_d','high_d','low_d','close_d']},<br\/>)<br\/># 2<br\/>conn = sqlite3.connect('example.db')<br\/># 3<br\/>TSMC.to_sql(name='TSMC', con=conn, if_exists=\"replace\", index=False)<br\/># 4<br\/>conn.close()<\/pre>\n<p class=\"wp-block-paragraph\"><strong>Remember to check the database after running the code <\/strong><\/p>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/0eL-udRX9A4p5jj_n.png\"\/><\/figure>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/02pFLlQWsyepYUfAi.png\"\/><\/figure>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<h2 class=\"wp-block-heading\"><strong>How to Update the\u00a0Database<\/strong><\/h2>\n<p class=\"wp-block-paragraph\">Next, we can add for-loop to the steps above, change stock\/company symbols from individual to multi-stocks, and each execution will save data to the database.<\/p>\n<ol class=\"wp-block-list\">\n<li>Collect data from TWN\/EWNPRCSTD.<\/li>\n<li>Select stocks\/company symbols of all listed companies.<\/li>\n<li>Using the append method to store data into the database.<\/li>\n<li>Execute for-loop.<\/li>\n<\/ol>\n<h2 class=\"wp-block-heading\">Windows Task Scheduler<\/h2>\n<ol class=\"wp-block-list\">\n<li>Search <strong>task scheduler<\/strong> on the bottom-left in your windows system.<\/li>\n<li>After entering into the task scheduler, click <strong>create a task <\/strong>on your right, then <strong>name the task<\/strong>(ef. update database).<\/li>\n<li>Switch to <strong>task trigger, create a new one<\/strong>, turn the <strong>trigger to daily in <\/strong>first in the \u201c<strong>trigger <\/strong>\u201c field, and then <strong>set the start time at 3:30 pm.<\/strong> (Be careful! Today\u2019s data will be updated after 3 pm, so we should set the start time after 3 pm.).<\/li>\n<li>Switch to the \u201c<strong>Action<\/strong>\u201d field and <strong>create a new one<\/strong>, then choose to <strong>start a program<\/strong>\ud83d\udc77\uff1a<\/li>\n<\/ol>\n<ul class=\"wp-block-list\">\n<li>First blank \u27a1\ufe0f <strong>python location(path)<\/strong><\/li>\n<li>Second blank \u27a1\ufe0f<strong> file\u2019s name (.py)<\/strong><\/li>\n<li>Third blank \u27a1\ufe0f<strong> file\u2019s location(path)<\/strong><\/li>\n<\/ul>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/0O-8MxDRmlMtQTCS.png\"\/><\/figure>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/0dP0WR9k-rgMpltAw.png\"\/><\/figure>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/0TizzDSU7WhvoWg6Y.png\"\/><\/figure>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/06C-INynBLoP-8Q24.png\"\/><\/figure>\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n<p class=\"wp-block-paragraph\">What we share with you this time is the introduction to get the data through TEJ API and combines with windows task scheduler to complete the goal of automatic database updating. <strong>Surely, we will write more articles related to data analysis and applications through Medium in the future!<\/strong><br \/>Finally, if you like this topic, please click \ud83d\udc4f below, giving us more support and encouragement. Additionally, if you have any questions or suggestions, please leave a message or email us, we will try our best to reply to you.<\/p>\n<h2 class=\"wp-block-heading\">Links related to this article\u00a0again!<\/h2>\n<ul class=\"wp-block-list\">\n<li><strong>API Official Website: <\/strong><a href=\"https:\/\/api.tej.com.tw\/\" rel=\"noreferrer noopener\" target=\"_blank\"><strong>TEJ API Official Website<\/strong><\/a><\/li>\n<li><strong>The Product Package: <\/strong><a href=\"https:\/\/eshop.tej.com.tw\/E-Shop\/Edata_caseIntro\/1\" rel=\"noreferrer noopener\" target=\"_blank\"><strong>TEJ E SHOP<\/strong><\/a><\/li>\n<li><strong>Source Code: <\/strong><a href=\"https:\/\/github.com\/tejtw\/TEJAPI_Python_Medium_Rookies\" rel=\"noreferrer noopener\" target=\"_blank\"><strong>TEJ GITHUB<\/strong><\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u2013 update your database automatically Continuing with the TEJ API data collecting\/reading\/saving instructions which we have learned in the previous episode, this episode focuses on the way to update your database automatically. It is consistent with the previous episode, so friends who have not read the previous episode, please go back and read the introduction [\u2026]<\/p>\n","protected":false},"featured_media":2906,"template":"","tags":[65,70],"insight_category":[16],"class_list":["post-2907","insight","type-insight","status-publish","has-post-thumbnail","hentry","tag-python","tag-tej-api","insight_category-quant-data-science"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/2907","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight"}],"about":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/types\/insight"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/media\/2906"}],"wp:attachment":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/media?parent=2907"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/tags?post=2907"},{"taxonomy":"insight_category","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight_category?post=2907"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}