Pyhton连接MySQL数据库简单操作
#!/usr/bin/env python
#encoding:utf8
# sudo apt-get install python-mysqldb
# mysql> CREATE DATABASE ApingDB;
# mysql> use itDB;
# mysql> CREATE TABLE Nikon (time DATE,PriceA INT,PriceB INT);
# mysql> INSERT INTO Nikon values("2013-10-09","3199","3988");
import MySQLdb
#连接
conn=MySQLdb.connect(host="localhost",user="root",passwd="password",db="ApingDB",charset="utf8")
cur=conn.cursor()
#增加
sql = 'INSERT INTO Nikon(time,PriceA,PriceB) values("2013-10-07","3199","3988")'
cur.execute(sql)
#更改
#sql = 'UPDATE Nikon set PriceB="4000" where time="2013-10-09"'
#cur.execute(sql)
#查询
cur.execute("select * from Nikon")
#输出显示
for row in cur.fetchall():
for r in row:
print r
#提交
conn.commit() #提交事务,否则操作无效
#关闭
cur.close()
conn.close()
(责任编辑:IT) |