#!/usr/bin/python
# -*- coding: utf-8 -*-
# @Time : 2020/11/15 7:45
# @Author : python_long
#通过python 一、pymsql操作mysql语句,二、ORM操作数据库
import pymysql
#连接mysql --db是指连接数据库名
conn=pymysql.connect(host="127.0.0.1",port=3306,user="root",passwd="",db="test")
cursor=conn.cursor(cursor=pymysql.cursors.DictCursor) #cursor=pymysql.cursors.DictCursor 以字典的形式出现键值对
# [{'id': 1, 'name': 'alex'}, {'id': 2, 'name': 'hello'}, {'id': 1, 'name': 'alex'}, {'id': 2, 'name': 'hello'}]
# sql_script= "create table t1 (id int ,name varchar (20))"
# sql_script1="insert into t1 (id,name) values (1,'alex'),(2,'hello')"
# ret=cursor.execute(sql_script1)
# print(ret)
sql_script="select * from t1"
cursor.execute(sql_script)
conn.commit() #插入数据后进行提交
#fetchone--取第一条结果 fetchmany(2)--取两条数据,fetchall取全部结果
# many=cursor.fetchmany(2)
# one=cursor.fetchone()
# all=cursor.fetchall()
# print(one)
# print("取全部结果",all) #取全部结果 ((1, 'alex'), (2, 'hello'), (1, 'alex'), (2, 'hello'))
#
# print("取多条结果",many) #取多条结果 ((1, 'alex'), (2, 'hello'))
# print("取一条结果",one) #取一条结果 (1, 'alex')
'''
#控制光标移动位置
print(cursor.fetchone())
print(cursor.fetchone())
cursor.scroll(-1,mode="relative") #相对位置,向上移一行
print(cursor.fetchone())
# (1, 'alex')
# (2, 'hello')
# (1, 'alex')
'''
'''
#控制光标移动位置
print(cursor.fetchone())
print(cursor.fetchone())
print(cursor.fetchone())
cursor.scroll(0,mode="absolute") #绝对位置,向上移一行
print("游标滚动后的位置:",cursor.fetchone())
# (1, 'alex')
# (2, 'hello')
# (1, 'alex')
# 游标滚动后的位置: (1, 'alex')
'''
print(cursor.fetchall())
cursor.close()
conn.close()