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()
r = cur.execute('insert into people values(6,\'Bill Gates\',58,\'M\')')
cur.execute('delete from people where age=58')
conn.commit()
r = cur.execute('select * from people')
r = cur.fetchall()
print r