Python 操作数据库pymysql

import pymysql

#添加数据

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='yyy')

#更改获取数据结果的数据类型,默认是元组,可以改为字典等:
#cursor=conn.cursor(cursor=pymysql.cursors.DictCursor)
cursor = conn.cursor() # sql = """CREATE TABLE EMPLOYEE ( # FIRST_NAME CHAR(20) NOT NULL, # LAST_NAME CHAR(20), # AGE INT, # SEX CHAR(1), # INCOME FLOAT )""" # # cursor.execute(sql) #row_affected = cursor.execute("create table t1(id INT ,name VARCHAR(20))") #row_affected=cursor.execute("INSERT INTO t1(id,name) values (1,'alvin'),(2,'xialv')") #cursor.execute("update t1 set name = 'silv2' where id=2") #查询数据 row_affected=cursor.execute("select * from t1") #接受影响的行 one=cursor.fetchone() # many=cursor.fetchmany(2) # all=cursor.fetchall() #scroll #cursor.scroll(-1,mode='relative') # 相对当前位置移动 #cursor.scroll(2,mode='absolute') # 相对绝对位置移动 conn.commit() cursor.close() conn.close()

猜你喜欢

转载自www.cnblogs.com/icemonkey/p/10503677.html