码迷,mamicode.com
首页 > 数据库 > 详细

python对mysql的一些操作(drop,create,insert)

时间:2014-12-26 16:41:38      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:python

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())

python对mysql的一些操作(drop,create,insert)

标签:python

原文地址:http://blog.csdn.net/rainlesvio/article/details/42173135

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!