pymysql
python3的pymysql模块
安装pymysql
pip3 install pymysql
执行sql创表语句
import pymysql conn = pymysql.connect(host='118.89.43.175',user='root',passwd='0987abc123',db='test',port=3306) #连接数据库 cursor = conn.cursor() #光标 sql = "create table t1 (id int,name varchar(20))" #sql创建表,把sql内容替换成sql语句后执行各种增删改成语句 cursor.execute(sql) #执行sql conn.commit() #提交 cursor.close() #关闭光标 conn.close() #关闭连接
cursor.scroll光标位置调整
#relative相对位置 #absolute绝对位置 cursor.scroll(-1,mode='relative') #向上移动一行光标 cursor.scroll(1,mode='relative') #向下移动一行光标 cursor.scroll(3,mode='absolute') #从开始位置向下移动3行光标
fetchone,fetchmany,fetchall 三种查找方式,主要起始光标的位置
print(cursor.fetchone()) #查找一行 print(cursor.fetchmany(2)) #查找一行,但是是从上一结果后开始查找 cursor.scroll(-1,mode='relative') #向上移动一行 #cursor.scroll(1,mode='relative') #cursor.scroll(3,mode='absolute') print(cursor.fetchall()) #从光标位置开始,输出后面所以行

浙公网安备 33010602011771号