python庫: MySQLdb中_mysql模塊

其實資料在http://mysql-python.sourceforge.net/MySQLdb.html#id5,可是彷佛須要翻一下gfw才行ffff....html

原本想翻譯下的,可是翻譯水平實在太差,只好做罷。python

MySQLdb中的_mysql模塊和MySQLdb模塊:
1.The _mysql module implements the MySQL C API directly. It is not compatible with the Python DB API interface.    犧牲了可移植性
2.MySQLdb is a thin Python wrapper around _mysql. It is compatible with the Python DB API, which makes the code more portable. Using this model is the preferred way of working with the MySQL.mysql

要使用_mysql,首先
import _mysqlsql

db = _mysql.connect(host="local", user='user', passwd='?', db='mydb')
db.query("set names utf8;")api

而後select:app

db.query("select * from mytable where id > 5")

There's no return value from this, but exceptions can be raised. The exceptions are defined in a separate module, _mysql_exceptions, but _mysql exports them. Read DB API specification PEP-249 to find out what they are, or you can use the catch-all MySQLError.less

要獲取結果,有兩種選擇:fetch

r=db.store_result()         #選擇1
# ...or...
r=db.use_result()           #選擇2

Both methods return a result object. What's the difference? store_result() returns the entire result set to the client immediately. If your result set is really large, this could be a problem. One way around this is to add a LIMIT clause to your query, to limit the number of rows returned. The other is to use use_result(), which keeps the result set in the server and sends it row-by-row when you fetch. This does, however, tie up server resources, and it ties up the connection: You cannot do any more queries until you have fetched all the rows. Generally I recommend using store_result() unless your result set is really huge and you can't use LIMIT for some reason.this

Now, for actually getting real results:spa

>>> r.fetch_row()
(('3','2','0'),)

This might look a little odd. The first thing you should know is, fetch_row() takes some additional parameters. The first one is, how many rows (maxrows) should be returned. By default, it returns one row. It may return fewer rows than you asked for, but never more. If you set maxrows=0, it returns all rows of the result set. If you ever get an empty tuple back, you ran out of rows.

The second parameter (how) tells it how the row should be represented. By default, it is zero which means, return as a tuple. how=1 means, return it as a dictionary, where the keys are the column names, or table.column if there are two columns with the same name (say, from a join). how=2 means the same as how=1 except that the keys are alwaystable.column; this is for compatibility with the old Mysqldb module.

 

 

MySQL C API function mapping

C API _mysql
mysql_affected_rows() conn.affected_rows()
mysql_autocommit() conn.autocommit()
mysql_character_set_name() conn.character_set_name()
mysql_close() conn.close()
mysql_commit() conn.commit()
mysql_connect() _mysql.connect()
mysql_data_seek() result.data_seek()
mysql_debug() _mysql.debug()
mysql_dump_debug_info conn.dump_debug_info()
mysql_escape_string() _mysql.escape_string()
mysql_fetch_row() result.fetch_row()
mysql_get_character_set_info() conn.get_character_set_info()
mysql_get_client_info() _mysql.get_client_info()
mysql_get_host_info() conn.get_host_info()
mysql_get_proto_info() conn.get_proto_info()
mysql_get_server_info() conn.get_server_info()
mysql_info() conn.info()
mysql_insert_id() conn.insert_id()
mysql_num_fields() result.num_fields()
mysql_num_rows() result.num_rows()
mysql_options() various options to_mysql.connect()
mysql_ping() conn.ping()
mysql_query() conn.query()
mysql_real_connect() _mysql.connect()
mysql_real_query() conn.query()
mysql_real_escape_string() conn.escape_string()
mysql_rollback() conn.rollback()
mysql_row_seek() result.row_seek()
mysql_row_tell() result.row_tell()
mysql_select_db() conn.select_db()
mysql_set_character_set() conn.set_character_set()
mysql_ssl_set() ssl option to _mysql.connect()
mysql_stat() conn.stat()
mysql_store_result() conn.store_result()
mysql_thread_id() conn.thread_id()
mysql_thread_safe_client() conn.thread_safe_client()
mysql_use_result() conn.use_result()
mysql_warning_count() conn.warning_count()
CLIENT_* MySQLdb.constants.CLIENT.*
CR_* MySQLdb.constants.CR.*
ER_* MySQLdb.constants.ER.*
FIELD_TYPE_* MySQLdb.constants.FIELD_TYPE.*
FLAG_* MySQLdb.constants.FLAG.*
相關文章
相關標籤/搜索