0

I'm new to mySQL and Python.

I have code to insert data from Python into mySQL,

conn = MySQLdb.connect(host="localhost", user="root", passwd="kokoblack", db="mydb")
for i in range(0,len(allnames)):
    try:
        query = "INSERT INTO resumes (applicant, jobtitle, lastworkdate, lastupdate, url) values ("
        query = query + "'"+allnames[i]+"'," +"'"+alltitles[i]+"',"+ "'"+alldates[i]+"'," + "'"+allupdates[i]+"'," + "'"+alllinks[i]+"')"
        x = conn.cursor()
        x.execute(query)
        row = x.fetchall()
    except:
        print "error"

It seems to be working fine, because "error" never appears. Instead, many rows of "1L" appear in my Python shell. However, when I go to MySQL, the "resumes" table in "mydb" remains completely empty.

I have no idea what could be wrong, could it be that I am not connected to MySQL's server properly when I'm viewing the table in MySQL? Help please.

(I only use import MySQLdb, is that enough?)

NightShadeQueen
  • 3,284
  • 3
  • 24
  • 37
abi
  • 285
  • 1
  • 7
  • 17

1 Answers1

3

use commit to commit the changes that you have done

MySQLdb has autocommit off by default, which may be confusing at first

You could do commit like this

conn.commit()

or

conn.autocommit(True) Right after the connection is created with the DB
The6thSense
  • 8,103
  • 8
  • 31
  • 65
  • Omg you are AWESOME, I've spent two days trying to figure out what was wrong. Thank you SO MUCH – abi Jul 02 '15 at 06:35
  • Haha I see. Another thing is, when scraping webdata, if I only insert the data into mySQL after I've stored it all in an object, am I likely to run out of memory? – abi Jul 02 '15 at 06:56