วิธีการ สร้างตารางข้อมูลในฐานข้อมูล
Postgres ด้วย Module
psycopg2 โดย ภาษา
Python Postgres คือระบบจัดการฐานข้อมูลที่เหมาะกับการทำระบบ ERP โดย Postgres เป็นระบบจัดการฐานข้อมูลที่ตอนนี้เป็นที่นิยม และใช้กันอย่างแพร่หลาย
เริ่มจากติดตั้ง psycopg2
ทำการเชื่อมต่อ กับ postgresql
โค้ด: เลือกทั้งหมด
con = p.connect(database='test',user='postgres',password='01249')
cur = con.cursor()
หลังจากนั้นใช้ภาษา sql ในการ สร้าง ตาราง
โค้ด: เลือกทั้งหมด
sql = """CREATE TABLE table_test_1(
id serial NOT NULL,
name character varying(20) NOT NULL,
CONSTRAINT table_test_1_pkey PRIMARY KEY (id))"""
cur.execute(sql)
เมื่อสร้างตาราวงสำเร็จ ให้แสดงผลตามที่เราต้องการ
โค้ด: เลือกทั้งหมด
cur.execute(sql)
con.commit()
print('เรียบร้อย')
โค้ด: เลือกทั้งหมด
try:
con = p.connect(database='test',user='postgres',password='01249')
cur = con.cursor()
# cur.execute('DROP TABLE IF EXISTS test_python')
# print ('Drop test_python เรียบร้อย')
sql = """CREATE TABLE table_test_1(
id serial NOT NULL,
name character varying(20) NOT NULL,
CONSTRAINT table_test_1_pkey PRIMARY KEY (id))"""
cur.execute(sql)
con.commit()
print('เรียบร้อย')
except p.DatabaseError:
print('Error %s' %p.DatabaseError)
if con:
con.close()
ผลลัพธ์
- Python Knowledge-1.png (634 ไบต์) Viewed 2095 times
- Python Knowledge-2.png (7.27 KiB) Viewed 2095 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
วิธีการ เพิ่มข้อมูลลงฐานข้อมูล Postgres ด้วย Module psycopg2
viewtopic.php?f=144&t=67978
วิธีการ แก้ไขข้อมูลในฐานข้อมูล Postgres ด้วย Module psycopg2
viewtopic.php?f=144&t=67979
วิธีการ ลบข้อมูลออกจากฐานข้อมูล Postgres ด้วย Module psycopg2
viewtopic.php?f=144&t=67980
วิธีการ สร้างตารางข้อมูลในฐานข้อมูล [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 ในการ สร้าง ตาราง
[code]sql = """CREATE TABLE table_test_1(
id serial NOT NULL,
name character varying(20) NOT NULL,
CONSTRAINT table_test_1_pkey PRIMARY KEY (id))"""
cur.execute(sql)[/code]
เมื่อสร้างตาราวงสำเร็จ ให้แสดงผลตามที่เราต้องการ
[code] cur.execute(sql)
con.commit()
print('เรียบร้อย')[/code]
[code]try:
con = p.connect(database='test',user='postgres',password='01249')
cur = con.cursor()
# cur.execute('DROP TABLE IF EXISTS test_python')
# print ('Drop test_python เรียบร้อย')
sql = """CREATE TABLE table_test_1(
id serial NOT NULL,
name character varying(20) NOT NULL,
CONSTRAINT table_test_1_pkey PRIMARY KEY (id))"""
cur.execute(sql)
con.commit()
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
วิธีการ เพิ่มข้อมูลลงฐานข้อมูล Postgres ด้วย Module psycopg2 https://www.mindphp.com/forums/viewtopic.php?f=144&t=67978
วิธีการ แก้ไขข้อมูลในฐานข้อมูล Postgres ด้วย Module psycopg2 https://www.mindphp.com/forums/viewtopic.php?f=144&t=67979
วิธีการ ลบข้อมูลออกจากฐานข้อมูล Postgres ด้วย Module psycopg2 https://www.mindphp.com/forums/viewtopic.php?f=144&t=67980