DAY 13 资料库-建立并操作Heroku PostgreSQL

Heroku PostgreSQL是一种Heroku提供的PostgreSQL服务,可免费使用,免费版使用有一定的限制,如果需要付费方案可见Heroku Postgres
PostgreSQL是一种开源的物件-关联式资料库资料库管理系统,用来储存与管理资料

建立资料库

https://elements.heroku.com/addons/heroku-postgresql
进入网页点击右上Install Heroku Postgres
https://ithelp.ithome.com.tw/upload/images/20210925/20140165yi1TWCXOft.jpg
选择资费方案,这边我选择预设的Hobby Dev方案
App to provision to搜寻并选择你的app
https://ithelp.ithome.com.tw/upload/images/20210925/20140165PQQnRQlaLV.png
将Heroku Postgres提供给你的app
Submit Order Form将表单送出
https://ithelp.ithome.com.tw/upload/images/20210925/20140165O8mtE3Ywqn.png
在resources页面会看到你的Heroku PostgreSQL
https://ithelp.ithome.com.tw/upload/images/20210925/20140165H5xNV3gSP5.png

连线至Heroku PostgreSQL

psycopg2是python语言的PostgreSQL资料库接口,它的主要优势在於完全支持Python DB API 2.0,以及安全的多执行绪支持

安装psycopg

如前面没有安装到psycopg,输入安装psycopg

pip uninstall psycopg2

连线到资料库

import psycopg2到你的专案

import psycopg2

在heroku你的app仪表网页下的resources页面,点击进入Heroku PostgreSQL
https://ithelp.ithome.com.tw/upload/images/20210925/201401651CkWrftRYA.png
点击settings-->Database Credentials显示你的资料库验证资讯
https://ithelp.ithome.com.tw/upload/images/20210925/20140165iGs4iUHf14.png
将Database Credentials中的资料填至psycopg2.connect函数中并执行

conn = psycopg2.connect(database="资料库名称",
						user="使用者帐号",
						password="密码",
						host="主机位址",
						port="5432")
print("Opened database successfully")

https://ithelp.ithome.com.tw/upload/images/20210925/20140165jZwzX5mzLR.png

建立资料表

这边建立一个范例资料表

id(主键) 姓名 使用者id
cursor = conn.cursor()
cursor.execute("CREATE TABLE userdata (id serial PRIMARY KEY, name VARCHAR(50), userid VARCHAR(50));")
print("Create table successfully")
cursor.close()

https://ithelp.ithome.com.tw/upload/images/20210925/201401658tJUAcVi0Z.png

插入资料至资料表

cursor = conn.cursor()
cursor.execute("INSERT INTO userdata (name, userid) VALUES (%s, %s);", ("小明", "a123456"))
cursor.execute("INSERT INTO userdata (name, userid) VALUES (%s, %s);", ("小王", "b654321"))
cursor.execute("INSERT INTO userdata (name, userid) VALUES (%s, %s);", ("小华", "c987654"))
print("Inserted 3 rows of data")
conn.commit()
cursor.close()

https://ithelp.ithome.com.tw/upload/images/20210925/20140165AFaQL4awfM.png

读取资料表资料

cursor.execute("SELECT * FROM userdata;")#选择资料表userdata
rows = cursor.fetchall() #读出所有资料

for row in rows:   #将读到的资料全部print出来
    print("Data row = (%s, %s, %s)" %(str(row[0]), str(row[1]), str(row[2])))

https://ithelp.ithome.com.tw/upload/images/20210925/20140165nxvire53bM.png

更新资料

cursor = conn.cursor()
cursor.execute("UPDATE inventory SET userid = %s WHERE name = %s;", ("d123789", "小明"))
conn.commit()
print("Updated 1 row of data")
cursor.close()

https://ithelp.ithome.com.tw/upload/images/20210925/20140165OUWrXTtZ7C.png

删除资料

cursor = conn.cursor()
cursor.execute("DELETE FROM userdata WHERE name = %s;", ("小华",))
print("Deleted 1 row of data")
conn.commit()
cursor.close()

https://ithelp.ithome.com.tw/upload/images/20210925/201401651ox1DNEDnB.png
参考:
https://www.itsfun.com.tw/psycopg2/wiki-4311324-5543304


<<:  D24: 工程师太师了: 第12.5话

>>:  聊天室(上)- 客制元件Xib建立

D25 - 用 Swift 和公开资讯,打造投资理财的 Apps { 三大法人成交比重 资料分析 }

台湾股市有揭露三大法人当日买进卖出的金额,在市场上会有流派依照这些进出的资讯,调整手上的资金部位。因...

Day24 参加职训(机器学习与资料分析工程师培训班),Python程序设计 & Pytorch

上午: Python程序设计 今日教学keras使用内建mnist dataset练习 from t...

27/一起成为国际研讨会讲师!!!(实战篇)

演讲之前的准备 演讲之前的准备(材料、预演(练习时间感))等等, 投影片修改。 我应该请别人帮我re...

Day5|【Git】动手建立、初始储存库(Repository)!

这里我们先看一张图,大概了解一下 Git 在发布专案时的流程。 先有个概念,之後会逐一详细解释。 开...

Day 04 HTML/JavaScript Attribute vs Property

Attribute vs Property attribute:属性在 HTML 会被称为 attr...