จากบทความที่แล้วการเขียน Python เพื่อทำการติดต่อฐานข้อมูล PostgreSQLที่เรากล่าวถึงขั้นตอนการเชื่อมต่อฐานข้อมูล
ในบทความนี้จะแนะนำการเขียน Python เพื่อสร้างตารางฐานข้อมูล หรือ Table ใน PostgreSQL
เริ่มกันเลย
1.ทำการเชื่อมต่อฐานข้อมูล **ขอข้ามวิธีการเชื่อมต่อฐานข้อมูล
2.เขียนโค้ดเพื่อทำการสร้างตารางขึ้นมา โดยใช้โค้ด แล้วทำการรัน
[code]
try:
sql = """ create table if not exists test_python (
id serial not null,
name_first character varying(100) not null,
name_last character varying(100) not null,
favorite_color character varying(1000) not null,
constraint test_python_pkey PRIMARY KEY (id)
) """
cur.execute(sql)
con.commit()
print ('สร้างตาราง test_python เรียบร้อยแล้ว')
except n.DatabaseError:
print ('Error %s' %n.DatabaseError)
if con:
con.close()
[/code]
จะได้ผลดังภาพ
[attachment=1]Selection_741.png[/attachment]
4. ในโปรแกรม pgAdmin ก็จะมีการสร้างตารางขึ้นมาเรียบร้อยแล้ว ดังภาพ
[attachment=0]Selection_740.png[/attachment]
เป็นยังไงกันบ้างครับสำหรับวิธีการเขียน Python เพื่อสร้างตารางในฐานข้อมูล PostgreSQL การสร้างตารางในฐานข้อมูลสามารถช่วยให้ผู้ใช้สามารถจัดการกับฐานข้อมูลได้
ศึกษาข้อมูลเพิ่มเติมได้ที่
การเพิ่มข้อมูลลงตาราง PostgreSQL ด้วย Python
ไพทอนสร้างตารางในฐานข้อมูล Mysql Python Create Table Mysql
เขียน Python สร้างตาราง Table ใน PostgresSQL
Moderators: mindphp, ผู้ดูแลกระดาน
-
- PHP Super Member
- Posts: 204
- Joined: 13/05/2019 10:03 am
-
- Similar Topics
- Replies
- Views
- Last post
-
-
python เรียกข้อมูลที่เป็นภาษาไทย จาก postgresSQL ไม่ได้ครับ
by teenproza7 » 24/05/2019 3:51 pm » in Programming - C/C++ & java & Python - 2 Replies
- 416 Views
-
Last post by teenproza7
25/05/2019 10:17 am
-
-
-
การใช้ flask_sqlalchemy สร้างตาราง ฐานข้อมูล ในภาษา python
by jirawoot » 10/07/2019 6:44 pm » in Python Knowledge - 0 Replies
- 569 Views
-
Last post by jirawoot
10/07/2019 6:44 pm
-
-
-
รวมเครื่องมือ สำหรับ เขียน Python กำลังหา Git มาใช้กับ Python IDE
by mindphp » 09/03/2018 2:16 am » in Python Knowledge - 0 Replies
- 1249 Views
-
Last post by mindphp
09/03/2018 2:16 am
-
-
-
Python ขอวิธีการตรวจสอบบฐานข้อมูลว่ามีการสร้าง table หรือยัง
by teenproza7 » 29/05/2019 3:34 pm » in Programming - C/C++ & java & Python - 1 Replies
- 660 Views
-
Last post by mindphp
29/05/2019 4:26 pm
-
-
-
ใช้โปรแกม pycham เขียน python จะติดต่อฐานข้อมูลยังไงครับ
by nuattawoot » 20/06/2017 10:34 am » in Programming - C/C++ & java & Python - 6 Replies
- 1139 Views
-
Last post by nuattawoot
20/06/2017 4:33 pm
-
-
-
เขียน Python ติดต่อฐานข้อมูล PostgreSQL และ pdAdmin III
by teenproza7 » 16/05/2019 2:08 pm » in SQL Knowledge - 1 Replies
- 1356 Views
-
Last post by tatiya
16/05/2019 2:31 pm
-
-
-
Plugin ของ Netbean ทำให้ netbeans เขียน Python ได้
by mindphp » 24/10/2013 6:09 pm » in Programming - C/C++ & java & Python - 1 Replies
- 1850 Views
-
Last post by mindphp
24/03/2018 12:18 pm
-
Who is online
Users browsing this forum: No registered users and 6 guests