每日学习笔记(2)

Python Web开发基础与技巧
本文介绍了使用Python进行Web开发的基础知识,包括处理POST请求、语法升级、对象序列化、错误处理、数据库交互以及结果集操作等核心内容。强调了编码、语法规范、资源管理的重要性,并提供了实际代码示例。

1,使用python提交post请求时,如果参数中包含中文,则会提交失败,因此需要将参数进行utf-8编码,示例如下:

<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->self.name=name.encode("utf-8")

2,一直在linux下使用python2.4,习惯了print 'hello'这样的写法,今天换到windows下,并且安装了python3.1后发现print是一个函数,因此要写成print('hello'),悲剧,但目前项目中都还是用的老旧的语法,还是得继续2.4才好。

3,python中序列化及反序列化一个对象,

ExpandedBlockStart.gif代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->importpickle

defserializeObject(obj,filePath):
output
=open(filePath,'wb')#二进制写模式打开
pickle.dump(obj,output)#序列化
output.close()

defdeSerializeObject(filePath):
try:
in=open(filePath,'rb')#二进制读模式打开
obj=pickle.load(in)#反序列化
returnobj
exceptEOFError:
returnNone
exceptIOError:
returnNone

4,使用trigger_error而不是die,这样对用户来说更加友好

<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->mysql_connect($host,$user,$password)ortrigger_error("Couldnotconnect:".mysql_error());

5,mysql_query对于select等返回resultset的sql语句来说,运行成功返回一个resource,发生错误则返回FALSE,而对于insert,update,

delete,drop等sql语句,运行成功返回TRUE,发生错误则返回FALSE;

ExpandedBlockStart.gif代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--><?php
//Thiscouldbesuppliedbyauser,forexample
$firstname='fred';
$lastname='fox';

//FormulateQuery
//ThisisthebestwaytoperformaSQLquery
//Formoreexamples,seemysql_real_escape_string()

$query=sprintf("SELECTfirstname,lastname,address,ageFROMfriendsWHEREfirstname='%s'ANDlastname='%s'",
mysql_real_escape_string($firstname),
mysql_real_escape_string($lastname));

//PerformQuery
$result=mysql_query($query);

//Checkresult
//ThisshowstheactualquerysenttoMySQL,andtheerror.Usefulfordebugging.

if(!$result){
$message='Invalidquery:'.mysql_error()."/n";
$message.='Wholequery:'.$query;
die($message);
}

//Useresult
//Attemptingtoprint$resultwon'tallowaccesstoinformationintheresource
//Oneofthemysqlresultfunctionsmustbeused
//Seealsomysql_result(),mysql_fetch_array(),mysql_fetch_row(),etc.

while($row=mysql_fetch_assoc($result)){
echo$row['firstname'];
echo$row['lastname'];
echo$row['address'];
echo$row['age'];
}

//Freetheresourcesassociatedwiththeresultset
//Thisisdoneautomaticallyattheendofthescript

mysql_free_result($result);
?>

6,mysql_fetch_array其实是取结果集的一行到一个数组中,它可以是一个关联数组(MYSQL_ASSOC)或数字数组(MYSQL_NUM),默认情况下两者都可以使用

ExpandedBlockStart.gif代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--><?php
mysql_connect("localhost","mysql_user","mysql_password")or
die("Couldnotconnect:".mysql_error());
mysql_select_db("mydb");

$result=mysql_query("SELECTid,nameFROMmytable");

while($row=mysql_fetch_array($result,MYSQL_BOTH)){
printf("ID:%sName:%s",$row[0],$row["name"]);
}

mysql_free_result($result);
?>

与mysql_fetch_row相比,mysql_fetch_array也并不慢的

ExpandedBlockStart.gif代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--><?php
$result=mysql_query("SELECTid,emailFROMpeopleWHEREid='42'");
if(!$result){
echo'Couldnotrunquery:'.mysql_error();
exit;
}
$row=mysql_fetch_row($result);

echo$row[0];//42
echo$row[1];//theemailvalue
?>

7mysql_close()并不一定需要写,因为非持久的连接会在脚本执行完毕后自动关闭

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值