python对mysql的一些操作(drop,create,insert)
by 伍雪颖
import MySQLdb,random
def getRandomNum():
key_list = []
for iin range(200):
key_list.append(str(random.uniform(10,20)))
return key_list
def write_to_mysql(key_list):
db = MySQLdb.connect("localhost","root","","test")
cursor = db.cursor()
cursor.execute("drop table if exists numTable")
sql = """create table numTable (key_value char(40) not null)"""
cursor.execute(sql)
try:
for iin range(200):
cursor.execute(‘insert into numTable values("%s")‘ % (key_list[i]))
db.commit()
except:
db.rollback()
db.close()
if __name__ ==‘__main__‘:
write_to_mysql(getRandomNum())
时间: 2024-11-08 12:08:34