三垄潮、查詢數(shù)據(jù)
import pymysql
def main():
con = pymysql.connect(host='localhost', port=3306,
db='datasets', user='root',
passwd='123456', charset='utf8')
try:
with con.cursor() as cursor:
# execute執(zhí)行抓半,后面括號是sql語句襟企。查詢所有
cursor.execute("select * from dat_movies")
# fetchall()抓取所有
print(cursor.fetchall())
# fetchone()只拿一條
print(cursor.fetchone())
# fetchmany(2)指定拿取參數(shù)
print(cursor.fetchmany(2))
# 捕獲異常
except pymysql.MySQLError as e:
print(e)
# 撤銷
con.rollback()
finally:
con.close()
if __name__ == '__main__':
main()