您的位置:首页 > 博客中心 > 数据库 >

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

时间:2022-03-14 04:18

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

热门排行

今日推荐

热门手游