游标.fetchall()在MySQL Python modu中

2024-04-25 08:49:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我想练习在python脚本中使用MySQL查询的技巧。 首先,我正在使用script,它可以从我的sql服务器(我使用的是ubuntu)获取所有数据库。你知道吗

我写了这个:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import MySQLdb as mdb
import sys

try:
    con = mdb.connect('localhost', 'testuser', 'testuser', 'test')

    cursor = con.cursor()
    cursor.execute("SHOW DATABASES;")

    for row in cursor:
    print row

except mdb.Error, e:
    print "Errot %d: %s" % (e.args[0], e.args[1])
    sys.exit(1)

finally:
    if con:
    con.close()

它返回:

('information_schema',)
('test',)

While I have more that that 2 databases - Output from mysql console is:
mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| test               |

| test1              |

+--------------------+

你能告诉我我做错了什么吗?你知道吗


Tags: testimportinformationthatschemasysmysqlargs