您的位置:首页 > 编程语言 > Python开发

python连接mysql数据库

2008-11-26 10:16 411 查看
看自己的机器有没有python

[root@localhost zn]#python -v //会进入python

Python 2.4.3 (#1, Dec 11 2006, 11:38:52)
[GCC 4.1.1 20061130 (Red Hat 4.1.1-43)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> //想要退出python 按键Ctrl+D


pythontest。py文件内容

#!/usr/bin/python
# imoprt MySQL module
import MySQLdb
#connect to the database
db=MySQLdb.connect(host="127.0.0.1",user="root",passwd="666666",db="cityman")
#get a cursor
cursor=db.cursor()
#sql statement
cursor.execute("insert into person values('','wangzhi','25','male')")
cursor.execute("select * from person")
#get the result set
result=cursor.fetchall()
#iterate thtough the result set
for i in result:
print(i)
cursor.close


回到终端

[root@localhost zn]# python python2mysql.py
python2mysql.py:9: Warning: Out of range value adjusted for column 'id' at row 1
cursor.execute("insert into person values('','wangzhi','25','male')")
(1L, 'liujun', 28L, 'male')
(2L, 'shenyang', 22L, 'fmale')
(3L, 'lizhiwei', 32L, 'male')
(4L, 'wangzhi', 25L, 'male')


执行成功
perl和python非常相像,这是我见过的最简练的两种语言,真的很棒。

不过上次看到csdn的文章说python的排名有所上升。而perl的却下降了,就我个人感觉用python确实比较简单易懂,但是目前没有太多的机会使用。老板比较偏向.net,自从他不做技术以后敏感度有所下降。God bless him。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: