ltsstar
ltsstar

Reputation: 892

Can't execute mysql insert using pythons mysqldb

This is my first question on stackoverflow! I'm currently programming a steamcommunity crawler, which crawls through the steam profiles and saves things like owned games, steamname, friends, ... But when I try to run the insert command it just does NOTHING.

 if not self.check_user('games', id):
  print "insert"
  self.cursor.execute("INSERT INTO games (userid) VALUES(%s);" % id)

The program displays "insert" but the execute command does neither throw an exception, nor inserts something to the database. In addition, an eventualy happening exception is not caught. When I forexample change the query towards "INSERT INTO games (useridd)", the program quits and I see the exception.

The application is multithreaded but acquired before executing, so I can't see any issue in that.

Upvotes: 3

Views: 832

Answers (2)

wind
wind

Reputation: 61

  1. add "COMMIT" at the end of command

    for example:

    cursor.execute("INSERT INTO games (userid) VALUES(%s);COMMIT;" % id)
    
  2. set autocommit by add the second line

    db = MySQLdb.connect(user='username', db=dbName,) # exmaple
    db.autocommit(1) # enable auto commit
    
  3. add commit() after transaction

    db.commit()
    

Upvotes: 6

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798446

You forgot to commit the transaction.

Upvotes: 5

Related Questions