python 中的多个 sql 语句
Multiple sql statement in python
我尝试从 mysql 更新多个数据。这是我的代码:
que = "select id_pl from datapl
db = MySQLdb.connect("localhost", "root", "", "tuongdata")
cur = db.cursor()
cur.execute(que)
pl = cur.fetchall()
cur.close()
pl = [ i[0] for i in pl ]
di = './newsdata/'
for i in pl:
lin = di + i + '/'
numb = len([name for name in os.listdir(str(lin)) if os.path.isfile(os.path.join(lin, name))])
qq = "update datapl set num = " + str(numb) + " where id_pl = " + str(i)
cur = db.cursor()
cur.execute(qq)
cur.close()
#print qq
db.close()
但是它不起作用,我不知道为什么:(
您似乎忘记在关闭连接之前提交更改。如果没有提交,数据库中将看不到任何更改。
que = "select id_pl from datapl"
db = MySQLdb.connect("localhost", "root", "", "tuongdata")
cur = db.cursor()
cur.execute(que)
pl = cur.fetchall()
cur.close()
pl = [ i[0] for i in pl ]
di = './newsdata/'
for i in pl:
lin = di + i + '/'
numb = len([name for name in os.listdir(str(lin)) if os.path.isfile(os.path.join(lin, name))])
qq = "update datapl set num = " + str(numb) + " where id_pl = " + str(i)
cur = db.cursor()
cur.execute(qq)
cur.close()
db.commit() # <---- Commits after each execution.
#print qq
db.close()
我尝试从 mysql 更新多个数据。这是我的代码:
que = "select id_pl from datapl
db = MySQLdb.connect("localhost", "root", "", "tuongdata")
cur = db.cursor()
cur.execute(que)
pl = cur.fetchall()
cur.close()
pl = [ i[0] for i in pl ]
di = './newsdata/'
for i in pl:
lin = di + i + '/'
numb = len([name for name in os.listdir(str(lin)) if os.path.isfile(os.path.join(lin, name))])
qq = "update datapl set num = " + str(numb) + " where id_pl = " + str(i)
cur = db.cursor()
cur.execute(qq)
cur.close()
#print qq
db.close()
但是它不起作用,我不知道为什么:(
您似乎忘记在关闭连接之前提交更改。如果没有提交,数据库中将看不到任何更改。
que = "select id_pl from datapl"
db = MySQLdb.connect("localhost", "root", "", "tuongdata")
cur = db.cursor()
cur.execute(que)
pl = cur.fetchall()
cur.close()
pl = [ i[0] for i in pl ]
di = './newsdata/'
for i in pl:
lin = di + i + '/'
numb = len([name for name in os.listdir(str(lin)) if os.path.isfile(os.path.join(lin, name))])
qq = "update datapl set num = " + str(numb) + " where id_pl = " + str(i)
cur = db.cursor()
cur.execute(qq)
cur.close()
db.commit() # <---- Commits after each execution.
#print qq
db.close()