Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't my script work, but I can manually INSERT into mysql?

conn = MySQLdb.connect(host='db1', user='user', passwd='pass', db='db', port=3306)
cursor = conn.cursor()

count = int(sys.argv[1])
x = 0
while x < count:
    x += 1
    cursor.execute("INSERT INTO auth_group(name) VALUES(%s)", (str(x)))
    #if I change %s to 'kkkk', it doesn't work either.

    print str(x) + ' / ' + str(count)
print 'done'

However...if I go into "mysql -uuser -ppass db", it works:

mysql > INSERT INTO auth_group(name) VALUES('kkkkk');

I don't know if this could be a problem...but I was having a replication issue earlier.

I would like to INSERT 99999 rows into the database. But it's empty.

mysql> select * from auth_group;
Empty set (0.33 sec)
like image 684
TIMEX Avatar asked Dec 08 '09 21:12

TIMEX


Video Answer


2 Answers

If there's no particular error message (in other words, it seems to work, but the insert doesn't stick), make sure you commit() after the statement:

conn.commit()
like image 195
Jarret Hardie Avatar answered Sep 19 '22 11:09

Jarret Hardie


You have to commit (by issuing a conn.commit()), because autocommit is off by default (and rightfully so).

like image 25
shylent Avatar answered Sep 18 '22 11:09

shylent