许明会的计算机技术主页

Language:C,C++,.NET Framework(C#)
Thinking:Design Pattern,Algorithm,WPF,Windows Internals
Database:SQLServer,Oracle,MySQL,PostSQL
IT:MCITP,Exchange,Lync,Virtualization,CCNP

导航

python体验(06)-Python处理mysql数据库

Python处理mysql数据库
1、检查用户环境:已经安装mysql-server,已经安装python-mysqldb 组件
        aptitude show mysql-server; aptitude show python-mysqldb

2、创建数据库,创建表,添加数据,修改mysql数据库的密码为Oracle11g

       use mysql;
        
update user set password=password('Oracle11g'where user='root';
        flush 
privileges;
        
---------------------
        create database myfamily;
        
use myfamily;
        
create table people(id int, name varchar(30), age int, sex char(1));
        
insert into people values(0,'Zihao Xu',5,'M');
        
select * from people;exit;
3、编写pyMysql.py测试程序
phoenix@debian:~$ cat pyMysql.py
#!/usr/bin/python
#
filename:pyMysql.py
#
coding:utf-8

import MySQLdb
conn 
= MySQLdb.connect(host='127.0.0.1', db='myfamily',
        user
='root', passwd='Oracle11g')
cur 
= conn.cursor()

= cur.execute('insert into people values(6,\'Bill Gates\',58,\'M\')')
cur.execute(
'delete from people where age=58')
conn.commit()

= cur.execute('select * from people')
= cur.fetchall()
print r

posted on 2010-12-02 15:43  许明会  阅读(365)  评论(0编辑  收藏  举报