第一步: python 與mysql接口
Windows 下安裝:
[http://www.codegood.com/downloads](http://www.codegood.com/downloads)
Linux:
sudo aptitude install python-mysqldb -y
第二步:
安裝 mysql
Linux :
~~~
sudo aptitude install mysql-client -y
sudo aptitude install mysql-server -y
~~~
Windows:
[https://dev.mysql.com/downloads/installer/5.6.html](https://dev.mysql.com/downloads/installer/5.6.html)
第三步:
安裝 SQLAlchemy
Linux
~~~
sudo easy_install SQLAlchemy
~~~
Windows:
~~~
easy_install SQLAlchemy
~~~
第四步:
~~~
easy_install uliweb
~~~
Linux下在 easy_install 前加 sudo
* * * * *
# sqlalchemy + mysql
~~~
from sqlalchemy import create_engine
engine = create_engine('mysql://username:password@127.0.0.1/foo')
~~~
sqlite
~~~
#Unix/Mac - 4 initial slashes in total
engine = create_engine('sqlite:////absolute/path/to/foo.db')
#Windows
engine = create_engine('sqlite:///C:\\path\\to\\foo.db')
#Windows alternative using raw string
engine = create_engine(r'sqlite:///C:\path\to\foo.db')
~~~
表和字段
~~~
metadata = MetaData()
user = Table('user', metadata,
Column('user_id', Integer, primary_key = True),
Column('user_name', String(16), nullable = False),
Column('email_address', String(60), key='email'),
Column('password', String(20), nullable = False)
)
user_prefs = Table('user_prefs', metadata,
Column('pref_id', Integer, primary_key=True),
Column('user_id', Integer, ForeignKey("user.user_id"), nullable=False),
Column('pref_name', String(40), nullable=False),
Column('pref_value', String(100))
)
metadata.create_all(engine)
~~~
~~~
connection = engine.connect()
~~~
插入,更新
~~~
stmt = user.insert().values(name="some name")
stmt = user.update().where(user.c.id==5).values(name="some name")
stmt = user.delete(user.c.userid==1)
connection.execute(stmt)
~~~
~~~
stmt = user.select()
all = connection.execute(stmt)
for a in all:
print a.user_name
~~~
* * * * *
# orm
建立 engine
~~~
from sqlalchemy import create_engine
engine = create_engine('mysql://username:password@127.0.0.1/foo')
~~~
創建 Base
~~~
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
~~~
創建表
~~~
from sqlalchemy import Column, Integer, String
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String(64))
fullname = Column(String(64))
password = Column(String(64))
~~~
~~~
Base.metadata.create_all(engine)
#User.create(engine) #建立一個表
#User.drop(engine) #刪除一個表
~~~
session
~~~
from sqlalchemy.orm import sessionmaker
Session = sessionmaker(bind=engine)
session = Session()
~~~
新紀錄
~~~
ed_user = User(name='ed', fullname='Ed Jones', password='edspassword')
~~~
~~~
session.add(ed_user)
session.commit()
~~~
多條記錄
~~~
session.add_all([item1, item2, item3])
~~~
查詢
~~~
our_user = session.query(User)
~~~
刪除
~~~
session.query(User).filter(User.id==3).delete()
~~~
更新
~~~
someobject = session.query(User).get(5)
# set 'value' attribute to a SQL expression adding one
someobject.name = "hello"
# issues "UPDATE some_table SET value=value+1"
session.commit()
~~~
- Python爬蟲入門
- (1):綜述
- (2):爬蟲基礎了解
- (3):Urllib庫的基本使用
- (4):Urllib庫的高級用法
- (5):URLError異常處理
- (6):Cookie的使用
- (7):正則表達式
- (8):Beautiful Soup的用法
- Python爬蟲進階
- Python爬蟲進階一之爬蟲框架概述
- Python爬蟲進階二之PySpider框架安裝配置
- Python爬蟲進階三之Scrapy框架安裝配置
- Python爬蟲進階四之PySpider的用法
- Python爬蟲實戰
- Python爬蟲實戰(1):爬取糗事百科段子
- Python爬蟲實戰(2):百度貼吧帖子
- Python爬蟲實戰(3):計算大學本學期績點
- Python爬蟲實戰(4):模擬登錄淘寶并獲取所有訂單
- Python爬蟲實戰(5):抓取淘寶MM照片
- Python爬蟲實戰(6):抓取愛問知識人問題并保存至數據庫
- Python爬蟲利器
- Python爬蟲文章
- Python爬蟲(一)--豆瓣電影抓站小結(成功抓取Top100電影)
- Python爬蟲(二)--Coursera抓站小結
- Python爬蟲(三)-Socket網絡編程
- Python爬蟲(四)--多線程
- Python爬蟲(五)--多線程續(Queue)
- Python爬蟲(六)--Scrapy框架學習
- Python爬蟲(七)--Scrapy模擬登錄
- Python筆記
- python 知乎爬蟲
- Python 爬蟲之——模擬登陸
- python的urllib2 模塊解析
- 蜘蛛項目要用的數據庫操作
- gzip 壓縮格式的網站處理方法
- 通過瀏覽器的調試得出 headers轉換成字典
- Python登錄到weibo.com
- weibo v1.4.5 支持 RSA協議(模擬微博登錄)
- 搭建Scrapy爬蟲的開發環境
- 知乎精華回答的非專業大數據統計
- 基于PySpider的weibo.cn爬蟲
- Python-實現批量抓取妹子圖片
- Python庫
- python數據庫-mysql
- 圖片處理庫PIL
- Mac OS X安裝 Scrapy、PIL、BeautifulSoup
- 正則表達式 re模塊
- 郵件正則
- 正則匹配,但過濾某些字符串
- dict使用方法和快捷查找
- httplib2 庫的使用