20171027 python访问MySQL数据库的基本操作

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat May 20 10:39:57 2017

@author: lishaojun
"""

#import mysql.connector as con
import MySQLdb as con
cnx = con.connect(user='root', password='supermap', database='lte', charset='utf8')
cur = cnx.cursor()



#若表存在,则删除
cur.execute('drop table if exists a')

#创建表
cur.execute('create table a (a int , b int, c int)')

#向表中写入记录
for i in range(0, 100):
    cur.execute('insert into a values(1,2,3)')

#查询
cur.execute('select * from a')

#遍历结果
for row in cur:
    print(row)

#关闭,清空环境
cur.close()
cnx.close()


你可能感兴趣的:(20171027 python访问MySQL数据库的基本操作)