วิธีการ เพิ่มข้อมูลลงฐานข้อมูล
Postgres ด้วย Module
psycopg2 โดย ภาษา
Python Postgres คือระบบจัดการฐานข้อมูลที่เหมาะกับการทำระบบ ERP โดย Postgres เป็นระบบจัดการฐานข้อมูลที่ตอนนี้เป็นที่นิยม และใช้กันอย่างแพร่หลาย
เริ่มจากติดตั้ง psycopg2
ทำการเชื่อมต่อ กับ postgresql
โค้ด: เลือกทั้งหมด
con = p.connect(database='test',user='postgres',password='01249')
cur = con.cursor()
หลังจากนั้นใช้ภาษา sql ในการ insert
โค้ด: เลือกทั้งหมด
sql = """INSERT INTO table_test_1 (name)
VALUES ('bo')"""
sql = sql.encode('utf-8')
เมื่อเพิ่มข้อมูลสำเร็จ ให้แสดงผลตามที่เราต้องการ
โค้ด: เลือกทั้งหมด
try:
cur.execute(sql)
con.commit()
print('เพิ่มข้อมูลเรียบร้อยแล้ว')
except :
con.rollback()
print('เพิ่มข้อมูลผิดพลาด')
โค้ด: เลือกทั้งหมด
try:
con = p.connect(database='test',user='postgres',password='01249')
cur = con.cursor()
sql = """INSERT INTO table_test_1 (name)
VALUES ('bo')"""
sql = sql.encode('utf-8')
try:
cur.execute(sql)
con.commit()
print('เพิ่มข้อมูลเรียบร้อยแล้ว')
except :
con.rollback()
print('เพิ่มข้อมูลผิดพลาด')
except p.DatabaseError:
print('Error %s' %p.DatabaseError)
if con:
con.close()
ผลลัพธ์
- Python Knowledge-1.png (1.23 KiB) Viewed 2032 times
- Python Knowledge-2.png (6.23 KiB) Viewed 2032 times
หากต้องการศึกษาเรื่อง Postgres และ psycopg2 หรือ python เพิ่มเติม สามารถศึกษาได้จากในบนเรียนของ python vdo python
https://www.mindphp.com/vdo-tutorial-python.html
บทเรียน Python
https://www.mindphp.com/%E0%B8%9A%E0%B8 ... ython.html
วิธีการ เพิ่มข้อมูลลงฐานข้อมูล [url=https://mindphp.com/%E0%B8%84%E0%B8%B9%E0%B9%88%E0%B8%A1%E0%B8%B7%E0%B8%AD/73-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3/3418-postgresql.html]Postgres[/url] ด้วย Module [url=https://www.mindphp.com/developer/tips-python/7494-pgadmin-psycopg2.html]psycopg2 [/url]โดย ภาษา [url=https://www.mindphp.com/%E0%B8%84%E0%B8%B9%E0%B9%88%E0%B8%A1%E0%B8%B7%E0%B8%AD/73-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3/2417-python-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3.html]Python[/url] Postgres คือระบบจัดการฐานข้อมูลที่เหมาะกับการทำระบบ ERP โดย Postgres เป็นระบบจัดการฐานข้อมูลที่ตอนนี้เป็นที่นิยม และใช้กันอย่างแพร่หลาย
เริ่มจากติดตั้ง psycopg2
[code]
import psycopg2 as p
[/code]
ทำการเชื่อมต่อ กับ postgresql
[code] con = p.connect(database='test',user='postgres',password='01249')
cur = con.cursor()[/code]
หลังจากนั้นใช้ภาษา sql ในการ insert
[code]sql = """INSERT INTO table_test_1 (name)
VALUES ('bo')"""
sql = sql.encode('utf-8')[/code]
เมื่อเพิ่มข้อมูลสำเร็จ ให้แสดงผลตามที่เราต้องการ
[code] try:
cur.execute(sql)
con.commit()
print('เพิ่มข้อมูลเรียบร้อยแล้ว')
except :
con.rollback()
print('เพิ่มข้อมูลผิดพลาด')[/code]
[code]try:
con = p.connect(database='test',user='postgres',password='01249')
cur = con.cursor()
sql = """INSERT INTO table_test_1 (name)
VALUES ('bo')"""
sql = sql.encode('utf-8')
try:
cur.execute(sql)
con.commit()
print('เพิ่มข้อมูลเรียบร้อยแล้ว')
except :
con.rollback()
print('เพิ่มข้อมูลผิดพลาด')
except p.DatabaseError:
print('Error %s' %p.DatabaseError)
if con:
con.close()[/code]
ผลลัพธ์
[attachment=1]Python Knowledge-1.png[/attachment]
[attachment=0]Python Knowledge-2.png[/attachment]
หากต้องการศึกษาเรื่อง Postgres และ psycopg2 หรือ python เพิ่มเติม สามารถศึกษาได้จากในบนเรียนของ python vdo python
https://www.mindphp.com/vdo-tutorial-python.html
บทเรียน Python
https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B9%80%E0%B8%A3%E0%B8%B5%E0%B8%A2%E0%B8%99%E0%B8%AD%E0%B8%AD%E0%B8%99%E0%B9%84%E0%B8%A5%E0%B8%99%E0%B9%8C/83-python.html