2

I am inserting data into Postgresql database table by using psycopg2 library, but I am getting an error in the insertion command. Can anyone help me to resolve this issue?

def t_b():  
    table = 'create table {} (Title serial, Link char(50), logo_link 
    char(50), Description Text)'.format(name)  
    cur.execute(table)  
    print("Table created :", name)  

def insertion(name, data):  
    data_s = ",".join(data)
    ins = 'insert into {} (Title,Link,logo_link,Description)values({})'.format(name, data_s)  
    cur.execute(ins)  
    conn.commit()  

def main():  
    t_b("ONE")  
    dd = ('abc', '123', 'INsyustriesz', '<htt:ps>' )  
    insertion("ONE", dd)  

if __name__ == '__main__':  
    main()
    cur.execute()  

And the error message appears:

psycopg2.errors.SyntaxError: syntax error at or near ":"  
LINE 1: ... logo_link, Description) values(abc,123,INsyustriesz,htt:ps)

and data is not inserted

1

1 Answer 1

1

The error happens because you're not including single-quotes in the values section of the query. You use single-quotes in dd, but those are to help Python interpret them as strings. I wouldn't recommending stringing everything together in data_s via a join, but actually specifying each element in the VALUES section:

    ins = 'insert into {} (Title,Link,logo_link,Description)values({},{},{},{})'.format((name + data))  
Sign up to request clarification or add additional context in comments.

1 Comment

sir its not working yet sir kindly can you tell me why we are adding(concatenate) name and data this commands also throwing error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.