โดย teenproza7 » 16/05/2019 3:21 pm
Source code ใน file
python_36
โค้ด: เลือกทั้งหมด
#-*- coding=utf-8 -*-
import psycopg2 as p
con = None
try:
con = p.connect(database='test', user='openpg', passwd='openpgpwd')
cur = con.cursor()
cur.execute('drop table if exists test_python')
print ('drop ตาราง test_python เรียบร้à¸à¸¢')
sql = """ create table if no exists test_python (
id serial no 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 p.DatabaseError:
print ('Error %s' %p.DatabaseError)
if con:
con.close()
ในส่วนของ
โค้ด: เลือกทั้งหมด
sql = """ create table if no exists test_python (
id serial no null
ใช้งานไม่ได้ ต้องเปลี่ยนจาก no เป็น not
[quote="teenproza7"]ผมดูวิดีโอบทเรียนจาก [url=https://www.mindphp.com/vdo-tutorial-python/3373-python-tutorial-34-postgresql.html]บทเรียน สอน Python บทที่ 34 การติดต่อกับฐานข้อมูล PostgreSQL ด้วย Python[/url] ปรากฏว่าหัวข้อบทเรียนแล้ว วิดีโอในหน้าเว็บไม่ตรงกันดังภาพ
และ ไฟล์ Source Code ก็เป็นไฟล์ของการลบข้อมูล[/quote]
Source code ใน file [url=https://www.mindphp.com/vdo-tutorial-python/3371-python-tutorial-36-postgresql.html?hitcount=0]python_36[/url]
[code]
#-*- coding=utf-8 -*-
import psycopg2 as p
con = None
try:
con = p.connect(database='test', user='openpg', passwd='openpgpwd')
cur = con.cursor()
cur.execute('drop table if exists test_python')
print ('drop ตาราง test_python เรียบร้à¸à¸¢')
sql = """ create table if no exists test_python (
id serial no 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 p.DatabaseError:
print ('Error %s' %p.DatabaseError)
if con:
con.close()
[/code]
ในส่วนของ [code]
sql = """ create table if no exists test_python (
id serial no null
[/code]
[size=150][color=#FF0000]ใช้งานไม่ได้ ต้องเปลี่ยนจาก no เป็น not[/color][/size]