Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting data into a table in MySQL with PyMySQL

I tried to insert some data into a table like this as below, but it doesn't work for me.

import pymysql

conn = pymysql.connect(host='localhost', user='root', password='1234', charset='utf8', db='kobis')
cur = conn.cursor()

sql = """insert into 'boxoffice' (targetDt, rank, rankOldAndNew, 
                                  movieCd, movieNm, salesAmt, audiCnt)
         values (%d, %d, %s, %d, %s, %d, %d) % cur.execute(sql, 
                   (20180220,11,'OLD',20170511,'Conan',36388900,48011))
    """

conn.commit()

There doesn't come out any error message after running this code, but the new data I tried to insert doesn't show up in the table 'boxoffice' at all...

Does anyone have a good idea to fix this?

like image 892
C. Shin Avatar asked Oct 18 '25 10:10

C. Shin


1 Answers

You need to execute your sql, you missed that line:

import pymysql

conn = pymysql.connect(host='localhost', user='root', password='1234', charset='utf8', db='kobis')
cur = conn.cursor()

sql = """insert into `boxoffice` (targetDt, rank, rankOldAndNew, 
                                  movieCd, movieNm, salesAmt, audiCnt)
         values (%s, %s, %s, %s, %s, %s, %s) 
    """
cur.execute(sql,(20180220,11,'OLD',20170511,'Conan',36388900,48011))
conn.commit()

EDIT Yor cur.execute() was inside the sql string, so it was doing nothing

like image 180
nacho Avatar answered Oct 20 '25 01:10

nacho



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!