之前测试了 python + mssql, 一字之差 mysql 顺便也测试下:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
6 rows in set (0.00 sec)
可以看到 有sakila样本库存在了
简单看看
mysql> use sakila
Database changed
mysql> select count(*) from actor;
+----------+
| count(*) |
+----------+
| 200 |
+----------+
1 row in set (0.00 sec)
mysql>
结果演员名单共有200条记录
好吧,开始测试 python 连接 : 需要CMD安装 pymysql
pip install pymysql
import pymysql
import pymysql.cursors
# Connect to the database # 如果不加这个,打印出来的result为元组, 加上这个 打印的result 结果为列表里面包含字典。
connection = pymysql.connect(host='localhost', port=3306, user='root', password='mysql',
db='sakila', charset='utf8',cursorclass=pymysql.cursors.DictCursor)
cursor = connection.cursor()
print(cursor)
sql = 'SELECT * FROM actor where actor_id <= 20' # 200 个演员总选取编号前20
cursor.execute(sql)
for row in cursor:
print(row)
connection.commit() # 连接提交事务
cursor.close() # 关闭游标连接
connection.close(); # 关闭连接,释放内存
结果大概这样子
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
===== RESTART: Z:\DataOnNAS_0901\5 Python\python pymysql\create table.py =====
<pymysql.cursors.DictCursor object at 0x00000288AABC22B0>
{
'actor_id': 1, 'first_name': 'PENELOPE', 'last_name': 'GUINESS', 'last_update': datetime.datetime(2006, 2, 15, 4, 34, 33)}
{
'actor_id': 2, 'first_name': 'NICK', 'last_name': 'WAHLBERG', 'last_update': datetime.datetime(2006, 2, 15, 4, 34, 33)}
{
'actor_id': 3, 'first_name': 'ED', 'last_name': 'CHASE', 'last_update': datetime.datetime(2006, 2, 15, 4, 34, 33)}
{
'actor_id': 4, 'first_name': 'JENNIFER', 'last_name': 'DAVIS', 'last_update': datetime.datetime(2006, 2, 15, 4, 34, 33)}
{
'actor_id': 5, 'first_name': 'JOHNNY', 'last_name': 'LOLLOBRIGIDA', 'last_update': datetime.datetime(2006, 2, 15, 4, 34, 33)}
{
'actor_id': 6, 'first_name': 'BETTE', 'last_name': 'NICHOLSON', 'last_update': datetime.datetime(2006, 2, 15, 4, 34, 33)}
{
'actor_id': 7, 'first_name': 'GRACE', 'last_name': 'MOSTEL', 'last_update': datetime.datetime(2006, 2, 15, 4, 34, 33)}
{
'actor_id': 8, 'first_name': 'MATTHEW', 'last_name': 'JOHANSSON', 'last_update': datetime.datetime(2006, 2, 15, 4, 34, 33)}
{
'actor_id': 9, 'first_name': 'JOE', 'last_name': 'SWANK', 'last_update': datetime.datetime(2006, 2, 15, 4, 34, 33)}
{
'actor_id': 10, 'first_name': 'CHRISTIAN', 'last_name': 'GABLE', 'last_update': datetime.datetime(2006, 2, 15, 4, 34, 33)}
{
'actor_id': 11, 'first_name': 'ZERO', 'last_name': 'CAGE', 'last_update': datetime.datetime(2006, 2, 15, 4, 34, 33)}
{
'actor_id': 12, 'first_name': 'KARL', 'last_name': 'BERRY', 'last_update': datetime.datetime(2006, 2, 15, 4, 34, 33)}
{
'actor_id': 13, 'first_name': 'UMA', 'last_name': 'WOOD', 'last_update': datetime.datetime(2006, 2, 15, 4, 34, 33)}
{
'actor_id': 14, 'first_name': 'VIVIEN', 'last_name': 'BERGEN', 'last_update': datetime.datetime(2006, 2, 15, 4, 34, 33)}
{
'actor_id': 15, 'first_name': 'CUBA', 'last_name': 'OLIVIER', 'last_update': datetime.datetime(2006, 2, 15, 4, 34, 33)}
{
'actor_id': 16, 'first_name': 'FRED', 'last_name': 'COSTNER', 'last_update': datetime.datetime(2006, 2, 15, 4, 34, 33)}
{
'actor_id': 17, 'first_name': 'HELEN', 'last_name': 'VOIGHT', 'last_update': datetime.datetime(2006, 2, 15, 4, 34, 33)}
{
'actor_id': 18, 'first_name': 'DAN', 'last_name': 'TORN', 'last_update': datetime.datetime(2006, 2, 15, 4, 34, 33)}
{
'actor_id': 19, 'first_name': 'BOB', 'last_name': 'FAWCETT', 'last_update': datetime.datetime(2006, 2, 15, 4, 34, 33)}
{
'actor_id': 20, 'first_name': 'LUCILLE', 'last_name': 'TRACY', 'last_update': datetime.datetime(2006, 2, 15, 4, 34, 33)}
>>>