ขอวิธีแก้ เพิ่มข้อมูลลง database ผิดพลาดหน่อยครับ

แนะนำ สอบถาม ภาษา C สำหรับผู้เริ่มต้น ภาษา Java ภาษา Python

Moderator: mindphp, ผู้ดูแลกระดาน

ภาพประจำตัวสมาชิก
chakirin.bfds
PHP Super Hero Member
PHP Super Hero Member
โพสต์: 783
ลงทะเบียนเมื่อ: 22/12/2020 10:19 am

ขอวิธีแก้ เพิ่มข้อมูลลง database ผิดพลาดหน่อยครับ

โพสต์ที่ยังไม่ได้อ่าน โดย chakirin.bfds »

เขียนโปรเเกรม selenium เพื่ออ่านกระทู้ จากนั้นต้องการนำ source code จากกระทู้ที่อ่านบันทึกลงฐานข้อมูล
ผมได้สร้างฐานข้อมูลไว้เเล้ว เเละสามารถเชื่อมต่อฐานข้อมูลได้เเล้ว เเต่ไม่สามารถเพิ่มข้อมูลลงฐานข้อมูลได้ ไม่ทราบว่าผมเขียนโปรเเกรมผิดตรงไหนหรือเปล่าครับ

code ที่เขียน

โค้ด: เลือกทั้งหมด

# -*- coding: utf-8 -*-
from selenium import webdriver #อิมพอร์ตโมดูล
import time
import psycopg2 as p
import configparser
from urllib.parse import urlparse
from urllib.request import urlopen, Request


con = None
config = configparser.ConfigParser()
config.read('admin.conf')

host=config['options']['host']
user=config['options']['user']
database=config['options']['database']
Username = "0000"
password = "00000"

driver = webdriver.Firefox()
time.sleep(2)
# โปรเเกรมเพื่อการล็อคอินเข้าเว็บ
web_mindphp = 'https://www.mindphp.com/forums/ucp.php?mode=login&redirect=viewforum.php%3Ff%3D338'
driver.get(web_mindphp)
for i in range(4):
            try:
                if driver.find_element_by_name("username"):
                    print('1')
                    #login = wait.until(ec.visibility_of_element_located((By.NAME, "username")))
                    #ActionChains(driver).move_to_element(login).perform()
                    x = driver.find_element_by_name("username")
                    x.clear()
                    x.send_keys(Username)
                    print('2')
                    x = driver.find_element_by_name("password")
                    x.clear()
                    x.send_keys(password)
                    time.sleep(3)
                    driver.find_element_by_name("login").click()
                    time.sleep(3)
                    print('login')
                    break
            except:
                if self.link_click("??????????????"):
                    print("??????????????")
                    break
                pass
            print("??????")
            time.sleep(2)

page = "https://www.mindphp.com/forums/viewforum.php?f=338"

driver.get(page)
time.sleep(2)
page_copy = page
# xpath_topic = " //a[@class='topictitle'] "
# all_ids_topic = driver.find_elements_by_xpath(xpath_topic)
save_link = {}
save_link_base ={}
no = 1
next = 1

for a in driver.find_elements_by_xpath("//div[@class='pagination']/ul/li/a"):
    # print(a.get_attribute('class'))
    # if a.get_attribute('class') == 'button button-icon-only':
    #     break
    if no == 104 :
        break
    else:
        print(a)
        time.sleep(2)
        for i in driver.find_elements_by_xpath(" //a[@class='topictitle'] "):
            save_link[next] = i.get_attribute('href')
            save_link_base[no] = i.get_attribute('href')+' , '+i.text
            # i.get_attribute('href')
            print(no, i.text)
            # print(i.get_attribute('href'))


            if next == 26:  # loop การเปิดอ่านกระทู้
                for n, m in save_link.items():
                    print(n)
                    print(m)
                    driver.get(m)
                    # driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
                    # time.sleep(2)
                    URL = m
                    req = Request(URL, headers={
                        'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 			Safari/537.36'})
                    urlinfo = urlparse(URL)
                    html = str(urlopen(req).read())
                    #บันทึกลงDatabase
                    try:
                        con = p.connect(host=host, database=database, user=user)
                        cur = con.cursor()
                        print("เชื่อมต่อฐานข้อมูลเเล้ว")
                        sql = "INSERT INTO test_01( source)\
                                VALUES ('%s')"%( html)
                        sql = sql.encode('utf - 8')
                        try:
                            cur.execute(sql)
                            con.commit()
                            print("เพิ่มข้อมูลเรียบร้อยเเล้ว")
                        except:
                            con.rollback()
                            print("ข้อมูลผิดพลาด")
                    except p.Error:
                        print("ติดต่อฐานข้อมูลผิดพลาด")

                    if con:
                        con.close()

                driver.get(page_copy)
                if driver.find_elements_by_xpath("//li[@class='arrow next']"):  # loop การเปิดหน้าถัดไป
                    page_copy = driver.find_elements_by_xpath("//li[@class='arrow next']/a[@class='button button-icon-only']")[
                        0].get_attribute('href') #เก็บลิ้งของหน้า
                    driver.find_elements_by_xpath("//li[@class='arrow next']/a[@class='button button-icon-only']")[0].click()
                    next = 1
                    no += 1
                    save_link = {}

                else:
                    break
                time.sleep(2)

            else:

                no+=1
                next+=1


ผลการรันตอนบันทึกลงฐานข้อมูล
1
viewtopic.php?f=338&t=52159
เชื่อมต่อฐานข้อมูลเเล้ว
ข้อมูลผิดพลาด
2
viewtopic.php?f=338&t=53698
เชื่อมต่อฐานข้อมูลเเล้ว
ข้อมูลผิดพลาด
3
viewtopic.php?f=338&t=58392
เชื่อมต่อฐานข้อมูลเเล้ว
ข้อมูลผิดพลาด
4
viewtopic.php?f=338&t=71909
เชื่อมต่อฐานข้อมูลเเล้ว
ข้อมูลผิดพลาด
sql สร้าง database

โค้ด: เลือกทั้งหมด

# -*- coding=utf8 -*-
import psycopg2 as p
import configparser
con = None
config = configparser.ConfigParser()
config.read('admin.conf')

host=config['options']['host']
user=config['options']['user']
database=config['options']['database']



try:
    con = p.connect(host = host, database= database, user= user )
    cur = con.cursor()
    cur.execute("DROP TABLE IF EXISTS test_01")
    print("Drop ตาราง test_01 เรียบร้อยแล้ว")

# Create table as per requirement
    sql = """CREATE TABLE IF NOT EXISTS test_01(
            id serial  NOT NULL,
            topic character varying(100) NOT NULL,
            source text  NOT NULL,
            CONSTRAINT test_pythons_pkey PRIMARY KEY (id)
            ) """
    cur.execute(sql)
    con.commit()
    print("สร้างตารางเสร็จเเล้ว")

except p.DatabaseError:
    print('Error %s' %p.DatabaseError)

if con:
    con.close()
admin.conf

โค้ด: เลือกทั้งหมด

[options]
host = localhost
database = test_selenium
port = 5432
user = postgres
Username = 00000
password = 000000
ภาพประจำตัวสมาชิก
MBMoo
PHP VIP Members
PHP VIP Members
โพสต์: 25599
ลงทะเบียนเมื่อ: 04/06/2020 10:05 am

Re: ขอวิธีแก้ เพิ่มข้อมูลลง database ผิดพลาดหน่อยครับ

โพสต์ที่ยังไม่ได้อ่าน โดย MBMoo »

ทดสอบเฉพาะ sql

เออเร่อแจ้ง ให้ใส่ค่า topic ด้วย
ERROR: null value in column "topic" violates not-null constraint
DETAIL: Failing row contains (2, null, 555).

********* Error *********

ERROR: null value in column "topic" violates not-null constraint
SQL state: 23502
Detail: Failing row contains (2, null, 555).

พอใส่ topic จะสามารถใช้ได้ เนืองจาก ต้องสร้างกำหนดให้ Not Null

โค้ด: เลือกทั้งหมด

INSERT INTO test_01(source,topic) VALUES ('555','555')
ภาพประจำตัวสมาชิก
chakirin.bfds
PHP Super Hero Member
PHP Super Hero Member
โพสต์: 783
ลงทะเบียนเมื่อ: 22/12/2020 10:19 am

Re: ขอวิธีแก้ เพิ่มข้อมูลลง database ผิดพลาดหน่อยครับ

โพสต์ที่ยังไม่ได้อ่าน โดย chakirin.bfds »

ยังแก้ไม่ได้ครับ
ขึ้น error แบบเดิม
1
viewtopic.php?f=338&t=58392
เชื่อมต่อฐานข้อมูลเเล้ว
ข้อมูลผิดพลาด
ภาพประจำตัวสมาชิก
MBMoo
PHP VIP Members
PHP VIP Members
โพสต์: 25599
ลงทะเบียนเมื่อ: 04/06/2020 10:05 am

Re: ขอวิธีแก้ เพิ่มข้อมูลลง database ผิดพลาดหน่อยครับ

โพสต์ที่ยังไม่ได้อ่าน โดย MBMoo »

chakirin.bfds เขียน: 08/01/2021 4:19 pm ยังแก้ไม่ได้ครับ
ขึ้น error แบบเดิม
1
viewtopic.php?f=338&t=58392
เชื่อมต่อฐานข้อมูลเเล้ว
ข้อมูลผิดพลาด
ตรง ตัวแปล html มี ซิงเกิลโคทอยู่ ทำให้ใส่ข้อมูลลงเบสไม่ได้
C++ & java & Python-1.png
ภาพประจำตัวสมาชิก
MBMoo
PHP VIP Members
PHP VIP Members
โพสต์: 25599
ลงทะเบียนเมื่อ: 04/06/2020 10:05 am

Re: ขอวิธีแก้ เพิ่มข้อมูลลง database ผิดพลาดหน่อยครับ

โพสต์ที่ยังไม่ได้อ่าน โดย MBMoo »

แนะนำแทนค่า อย่างอื่นลงไปแทนที่ ' เช่นคำว่า (single quote) ก็จะสามารถบันทึกลงดาต้าเบสได้

โค้ด: เลือกทั้งหมด

html = html.replace("'", "(single quote)")
ภาพประจำตัวสมาชิก
chakirin.bfds
PHP Super Hero Member
PHP Super Hero Member
โพสต์: 783
ลงทะเบียนเมื่อ: 22/12/2020 10:19 am

Re: ขอวิธีแก้ เพิ่มข้อมูลลง database ผิดพลาดหน่อยครับ

โพสต์ที่ยังไม่ได้อ่าน โดย chakirin.bfds »

bolue เขียน: 08/01/2021 4:50 pm แนะนำแทนค่า อย่างอื่นลงไปแทนที่ ' เช่นคำว่า (single quote) ก็จะสามารถบันทึกลงดาต้าเบสได้

โค้ด: เลือกทั้งหมด

html = html.replace("'", "(single quote)")
ทำได้เเล้วครับ
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 81