MySQLdb

MySQLdb

MySQL是一個小型關係型資料庫管理系統,開發者為瑞典MySQLAB公司。在2008年1月16號被Sun公司收購。目前MySQL被廣泛地套用在Internet上的中小型網站中。由於其體積小、速度快、總體擁有成本低,尤其是開放源碼這一特點,許多中小型網站為了降低網站總體擁有成本而選擇了MySQL作為網站資料庫。

基本介紹

  • 中文名:MySQLdb
  • 外文名:無
  • 性質:小型關係型資料庫管理系統,。
  • 開發者:為瑞典MySQLAB公司
  • Sun公司收購:在2008年1月16號
  • 優點:體積小、速度快、總體擁有成本低
簡介,MySQLdb,Python,屬性和方法,DATA,FUNCTIONS,簡單使用,更多幫助,

簡介

MySQLdb

Python 連線 MySQL 的模組。 MySQL versions 3.23-5.1; and Python versions 2.3-2.5 are supported.

Python

Python是一種程式語言,它的名字來源於一個喜劇。也許最初設計Python這種語言的人並沒有想到今天Python會在工業和科研上獲得如此廣泛的使用。著名的自由軟體作者EricRaymond在他的文章《如何成為一名黑客》中,將Python列為黑客應當學習的四種程式語言之一,並建議人們從Python開始學習編程。這的確是一個中肯的建議,對於那些從來沒有學習過編程或者並非計算機專業的編程學習者而言,Python是最好的選擇之一。Python第一次學習Python,我只用了不到二十分鐘的時間,站在書店裡把一本教初學編程的人學習Python的書翻了一遍。也是從那時起,我開始被這種神奇的語言吸引。

屬性和方法

DATA

BINARY = DBAPISet([249, 250, 251, 252])
DATE = DBAPISet([10, 14])
NULL = 'NULL'
NUMBER = DBAPISet([0, 1, 3, 4, 5, 8, 9, 13])
ROWID = DBAPISet([])
STRING = DBAPISet([253, 254, 247])
TIME = DBAPISet([11])
TIMESTAMP = DBAPISet([12, 7])
__all__ = ['BINARY', 'Binary', 'Connect', 'Connection', 'DATE', 'Date'...
__author__ = 'Andy Dustman __revision__ = '491'
__version__ = '1.2.2'
__warningregistry__ = {('the sets module is deprecated', <type 'except...
apilevel = '2.0'
paramstyle = 'format'
threadsafety = 1
version_info = (1, 2, 2, 'final', 0)

FUNCTIONS

Binary(x)
Connect(*args, **kwargs)
Factory function for connections.Connection.
Connection = Connect(*args, **kwargs)
Factory function for connections.Connection.
DateFromTicks(ticks)
Convert UNIX ticks into a date instance.
TimeFromTicks(ticks)
Convert UNIX ticks into a time instance.
TimestampFromTicks(ticks)
Convert UNIX ticks into a datetime instance.
connect = Connect(*args, **kwargs)
Factory function for connections.Connection.
debug(...)
Does a DBUG_PUSH with the given string.
mysql_debug() uses the Fred Fish debug library.
To use this function, you must compile the client library to
support debugging.
escape(...)
escape(obj, dict) -- escape any special characters in object obj
using mapping dict to provide quoting functions for each type.
Returns a SQL literal string.
escape_dict(...)
escape_sequence(d, dict) -- escape any special characters in
dictionary d using mapping dict to provide quoting functions for each type.
Returns a dictionary of escaped items.
escape_sequence(...)
escape_sequence(seq, dict) -- escape any special characters in sequence
seq using mapping dict to provide quoting functions for each type.
Returns a tuple of escaped items.
escape_string(...)
escape_string(s) -- quote any SQL-interpreted characters in string s.
Use connection.escape_string(s), if you use it at all.
_mysql.escape_string(s) cannot handle character sets. You are
probably better off using connection.escape(o) instead, since
it will escape entire sequences as well as strings.
get_client_info(...)
get_client_info() -- Returns a string that represents
the client library version.
string_literal(...)
string_literal(obj) -- converts object obj into a SQL string literal.
This means, any special SQL characters are escaped, and it is enclosed
within single quotes. In other words, it performs:
"'%s'" % escape_string(str(obj))

簡單使用

>>>import MySQLdb
>>>test=MySQLdb.connect(db='databasename',host='localhost',user='username',passwd='password')
>>> cur = test.cursor()
>>> cur.execute('CREATE TABLE users(login VARCHAR(8), uid INT)')
0L
現在我們來插入幾行數據到資料庫, 然後再將它們取出來.
>>> cur.execute("INSERT INTO users VALUES('john', 7000)")
1L
>>> cur.execute("INSERT INTO users VALUES('jane', 7001)")
1L
>>> cur.execute("INSERT INTO users VALUES('bob', 7200)")
1L
>>> cur.execute("SELECT * FROM users WHERE login LIKE 'j%'")
2L
>>> for data in cur.fetchall():
... print '%s\t%s' % data
...
john 7000
jane 7001

更多幫助

更多內容參見Python線上幫助文檔
>>>help()
help>MySQLdb

相關詞條

熱門詞條

聯絡我們