python学习笔记(一)

因为项目需要,开始接触python。

使用系统为ubuntu server版。预装了python。

命令行:python print "hello world"

会看到终端输出 hello world

python与msql链接。

打开文本编辑器

import MySQLdb
try:
   connection = MySQLdb.connect(user="root",passwd="pwd",host="localhost",db="数据库名称")
except:
   print "Could not connect to MySQL server."

如果链接正确,终端没有输出。

try:
   cursor = connection.cursor()
   cursor.execute( "SELECT *From 数据库名称 )
   print "Rows selected:", cursor.rowcount

返回数据数量。

然后再尝试输出数据

 for row in cursor.fetchall():
       print row[0];
   cursor.close()

遇到一个问题

终端提示IndentationError:expected an indented block

原来

if xxxxxx:

(空格)xxxxx

或者

def xxxxxx:

(空格)xxxxx

还有

for xxxxxx:

(空格)xxxxx

一句话 有冒号的下一行往往要缩进,该缩进就缩进。

今天暂时就学到这里


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值