#!/user/bin/env python
# _*_ coding:utf-8 _*_
import MySQLdb
conn= MySQLdb.connect(
        host='localhost',
        port = 3306,
        user='root',
        passwd='123456',
        db ='test',
        )
#用于创建数据库的连接
cur = conn.cursor()
#编写查询语句
sql1 = "select * fromStaff";
#通过游标cur操作execute()方法写入纯
count=cur.execute(sql1);
info =cur.fetchmany(count);
for user in info:
    print(user);
cur.close()
conn.commit()
conn.close()

学习任务

     使用sql语句查询user数据

    

学习目标

知识目标

1.      熟悉sql的查询数据语句

2.      熟悉对MySql数据库的查询操作

能力目标

1.能够编写sql查询语句

2.学会对MySql数据库的查询操作

 

 

 

 

 

 

 

查询语句

select 【查询的内容】from 【填写表名】

 

fetchmany(self, size=None)函数
 
fetchmany(self, size=None):接收size条返回结果行.如果size的值大于返回的结果行的数量,则会返回cursor.arraysize条数据。
 
 


示例

import MySQLdb             ||导入MySql模块
conn= MySQLdb.connect(
        host='localhost',
        port = 3306,
        user='root',
        passwd='123456',
        db ='test',
        )                ||连接数据库
 

。。。

#用于创建数据库的连接
cur = conn.cursor()
#编写查询语句,返回查询的数据个数
sql1 = "select * fromuser";
#通过游标cur操作execute()方法写入纯sql语句来对数据进行操作
count=cur.execute(sql1);
# fetchmany()可以获取多条数据,但需要指定查询个数,然后通过for循环可以打印出查询到的数据了
info =cur.fetchmany(count);
for user in info:
    print(user);


 

#关闭数据库的连接
cur.close()
conn.commit()
conn.close()
 

任务实施


 
录屏
 
 
知识点总结
1. 使用sql语句查詢数据
2.fetchmany()函數的用法
 
 
问题
1.fetchmany()函数的正确用法
2.sql查询语句的关键字
3.查询student表中的数据


答案
1.fetchmany()可以获取多条数据,但需要指定数据的条数,通过一个for循环就可以把多条数据输出出来
2.select 【查询的内容】from 【填写表名】
3.
 #!/user/bin/env python
# _*_ coding:utf-8 _*_
import MySQLdb
conn= MySQLdb.connect(
        host='localhost',
        port = 3306,
        user='root',
        passwd='123456',
        db ='test',
        )
#用于创建数据库的连接
cur = conn.cursor()
#编写查询语句
sql1 = "select * from student";
#通过游标cur操作execute()方法写入纯
count=cur.execute(sql1);
info =cur.fetchmany(count);
for user in info:
    print(user);
cur.close()
conn.commit()
conn.close()


 
 

 

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐