python 操作mysql数据库存
代码:
说明:由于我本机没有安装数据库,数据库是在远程访问的,故地址不是localhost
# __author__ = 'STEVEN' import pymysql host = '10.1.1.136' port = 3306 name = 'root' pwd = 'root' db = 'PYTHONDB' #获得连接 con = pymysql.connect(host=host,user=name,passwd=pwd,db=db,port=port,charset='utf8') print('ok') # 创建cursor cursor = con.cursor() #开始执行sql sql = 'select * from STU' grant = "grant all on *.* to 'gwb'@'%' identified by 'gwb123'" stu_li = [('谭浩强','600',1),('麻生','400',2)] #一次执行多条插入 s = 'insert into STU(name,grade,sch_id) values(%s,%s,%s)' # effect_row = cursor.executemany(s,stu_li) cursor.execute(sql) print(cursor.fetchall())