คนที่กำลังวิธีการสร้างฐานข้อมูล ผมมีวิธีมาแชร์ครับ เป็นไลบารี่ของภาษา Python ที่ใช้สำหรับการเชื่อมต่อสำหรับฐานข้อมูลเพื่อจะใช้ในการสร้างตาราง โดยจะต้องติดตั้ง postgresql ลงเครื่องก่อน และทำการติดตั้ง flask_sqlalchemy ด้วย แล้วทำการเรียกใช้ module flask_sqlalchemy ประโยชน์ของ module ตัวนี้คือ สามารถเขียนโดยใช้ภาษา python เพื่อในสร้างตาราง ได้เลย ส่วนของการติดตั้งนะครับ
ใช้คำสั่ง
โค้ด: เลือกทั้งหมด
pip install SQLAlchemy
โค้ด: เลือกทั้งหมด
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app =Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI']='postgresql://postgres:@localhost/test'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class usertest(db.Model):
id = db.Column(db.Integer,primary_key=True)
fristname = db.Column(db.String(100),nullable=False,unique=False)
lastname = db.Column(db.String(100), nullable=False, unique=False)
username = db.Column(db.String(100),unique=True,nullable=False)
password = db.Column(db.String(100),primary_key=False, unique=False,nullable=False)
level = db.Column(db.Integer, primary_key=False, unique=False, nullable=False)
def __init__(self, fristname, lastname, username,password,level):
self.fristname=fristname
self.lastname = lastname
self.username=username
self.password=password
self.level = level
if __name__ == '__main__':
db.create_all()
app.run(debug=True)
ผลลัพธ์
อ้างอิง
https://www.scotch.io/tutorials/authentication-and-authorization-with-flask-login
https://www.realpython.com/using-flask-login-for-user-management-with-flask/
https://www.blog.openshift.com/use-flask-login-to-add-user-authentication-to-your-python-application/