python 读取数据库,语句老是报错。

软件和网站开发以及相关技术探讨
回复
workatnet
帖子: 9
注册时间: 2011-11-10 14:25

python 读取数据库,语句老是报错。

#1

帖子 workatnet » 2014-05-15 14:07

在8.04.4 server环境下:

$ dpkg -l | grep -i python

ii libapache2-mod-python 3.3.1-2build1 Apache 2 module that embeds Python within th
ii moinmoin-common 1.5.8-5.1ubuntu2.5 Python clone of WikiWiki - common data
ii python 2.5.2-0ubuntu1 An interactive high-level object-oriented la
ii python-apt 0.7.4ubuntu7.7 Python interface to libapt-pkg
ii python-central 0.6.7ubuntu0.1 register and build utility for Python packag
ii python-django 0.96.1-2ubuntu2.1 A high-level Python Web framework
ii python-gdbm 2.5.2-0ubuntu2 GNU dbm database support for Python
ii python-gnupginterface 0.3.2-9ubuntu1 Python interface to GnuPG (GPG)
ii python-minimal 2.5.2-0ubuntu1 A minimal subset of the Python language (def
ii python-moinmoin 1.5.8-5.1ubuntu2.5 Python clone of WikiWiki - library
ii python-mysqldb 1.2.2-5ubuntu1 A Python interface to MySQL
ii python-support 0.7.5ubuntu1 automated rebuilding support for python modu
ii python2.5 2.5.2-2ubuntu6.2 An interactive high-level object-oriented la
ii python2.5-minimal 2.5.2-2ubuntu6.2 A minimal subset of the Python language (ver

写了一个小程序,希望读取显示自己数据库users表里面的 name 和 mail。

cat /var/www/dbtest.py
#!/usr/bin/python

db = MySQLdb.connect(host="localhost",user="XXXX",passwd="passwd****",db="mymanualdb")

cursor = db.cursor()
cursor.execute("SELECT name, mail FROM users")

result = cursor.fetchall()

for record in result:
print record[0] , "-->" , record[1]


但实际老是报错,不知各位大大有没有看出问题在哪?

$ python /var/www/dbtest.py
File "/var/www/dbtest.py", line 11
print record[0] , "-->" , record[1]
^
IndentationError: expected an indented block
nae6taiyie0T
帖子: 482
注册时间: 2013-09-13 0:42
系统: Debian sid

Re: python 读取数据库,语句老是报错。

#2

帖子 nae6taiyie0T » 2014-05-15 18:47

python给出的错误提示非常非常清楚, 如果不认识那些英文, 请至少查一下字典.

代码: 全选

File "/var/www/dbtest.py", line 11            # 文件 "/var /www/dbtest.py", 第11行
print record[0] , "-->" , record[1]           # print record[0] , "-->" , record[1] 
^                                             # ^ 这里, 行首
IndentationError: expected an indented block  # 缩进错误: 这个代码块需要缩进.   因为python用缩进来区分代码块(通常是4个空格).
格式化一下你上传的测试代码, 可以这样写:

代码: 全选

#!/usr/bin/env python

db = MySQLdb.connect(
        host="localhost",user="XXXX",passwd="passwd****",db="mymanualdb")

cursor = db.cursor()
req = cursor.execute("SELECT name, mail FROM users")

for record in req:
    print(record[0] , "-->" , record[1])
不用把python解析器的路径写死, 因为不是所有系统的python都在/usr/bin/python, 用env命令来自动处理是一个很通用的做法.
workatnet
帖子: 9
注册时间: 2011-11-10 14:25

Re: python 读取数据库,语句老是报错。

#3

帖子 workatnet » 2014-07-22 11:50

nae6taiyie0T 写了:python给出的错误提示非常非常清楚, 如果不认识那些英文, 请至少查一下字典.

代码: 全选

File "/var/www/dbtest.py", line 11            # 文件 "/var /www/dbtest.py", 第11行
print record[0] , "-->" , record[1]           # print record[0] , "-->" , record[1] 
^                                             # ^ 这里, 行首
IndentationError: expected an indented block  # 缩进错误: 这个代码块需要缩进.   因为python用缩进来区分代码块(通常是4个空格).
格式化一下你上传的测试代码, 可以这样写:

代码: 全选

#!/usr/bin/env python

db = MySQLdb.connect(
        host="localhost",user="XXXX",passwd="passwd****",db="mymanualdb")

cursor = db.cursor()
req = cursor.execute("SELECT name, mail FROM users")

for record in req:
    print(record[0] , "-->" , record[1])
不用把python解析器的路径写死, 因为不是所有系统的python都在/usr/bin/python, 用env命令来自动处理是一个很通用的做法.

谢谢大侠,it work.
回复