?? mysql.c
字號:
row = mysql_fetch_row(res); PyTuple_SetItem(result, i, Py_BuildValue("y", row[0])); } mysql_free_result(res); return result; } return PyTuple_New(0);}/* Returns a result set consisting of table names in the current database that match the simple regular expression specified by the wild parameter. */PyDoc_STRVAR(_mdoc_list_tables,"list_dbs(wild) -> list tables\n\\n\Returns a result set consisting of table names in the current database that match the simple regular expression\n\specified by the wild parameter. wild may contain the wildcard characters “%” or “_”, or may be omitted to match\n\all tables.");_m_header(m_list_tables){ const char *wild = NULL; MYSQL_RES *res; /* parse arguments */ if (!PyArg_ParseTuple(args, "|s*", &wild)) return NULL; res = mysql_list_tables(m->mysql, wild); if (res != NULL) { int i; PyObject *result = PyTuple_New(mysql_num_rows(res)); MYSQL_ROW row; /* prepare list of databases */ for (i=0; i<mysql_num_rows(res); i++) { row = mysql_fetch_row(res); PyTuple_SetItem(result, i, Py_BuildValue("y", row[0])); } mysql_free_result(res); return result; } return PyTuple_New(0);}/* Returns a result set consisting of field names in the given table that match the simple regular expression specified by the wild parameter. */PyDoc_STRVAR(_mdoc_list_fields,"list_fields(table, wild) -> returns a result set consisting of field names in the given table\n\\n\Returns a result set consisting of field names in the given table that match the simple regular expression specified\n\by the wild parameter. wild may contain the wildcard characters “%” or “_”, or may be omited to match all fields.");static PyObject *m_list_fields(MySQLconn *m, PyObject *args, PyObject *kwds){ PyObject *query; char *table, *_query, *wild = NULL; ssize_t len, tlen, wlen; /* parse arguments */ if (!PyArg_ParseTuple(args, "s#|s#", &table, &tlen, &wild, &wlen)) return NULL; if (wild == NULL) { query = PyUnicode_FromFormat("show fields from %s;", table); } else { query = PyUnicode_FromFormat("show fields from %s like '%s';", table, wild); } _query = _PyUnicode_AsStringAndSize(query, &len); Py_DECREF(query); if (!mysql_real_query(m->mysql, _query, len)) { /* prepare and return list of processes */ MYSQL_RES *res = mysql_store_result(m->mysql); if (res != NULL) { PyObject *result = PyTuple_New(mysql_num_rows(res)); PyObject *row; MYSQL_ROW mysql_row; MYSQL_FIELD *fields = mysql_fetch_fields(res); int r, f; /* prepare result set */ for (r=0; r < mysql_num_rows(res); r++) { mysql_row = mysql_fetch_row(res); row = PyDict_New(); for (f=0; f < mysql_num_fields(res); f++) { PyObject *key = Py_BuildValue("y", fields[f].name); PyObject *value = Py_BuildValue("y", (char *)mysql_row[f]); PyDict_SetItem(row, key, value); Py_DECREF(key); Py_DECREF(value); } PyTuple_SetItem(result, r, row); } mysql_free_result(res); refprint(result); return result; } else { raise_error("Unable to get result set") } } else { printf("yupeee2\n"); raise_error("Unable to fetch list of fields"); }}/* Returns a result set describing the current server threads */PyDoc_STRVAR(_mdoc_list_processes, "list_processes() -> Returns a result set describing the current server threads.");_m_header_noargs(m_list_processes){ const char query[] = "show processlist;"; if (!mysql_real_query(m->mysql, query, 17)) { /* prepare and return list of processes */ MYSQL_RES *res = mysql_store_result(m->mysql); if (res != NULL) { PyObject *result = PyTuple_New(mysql_num_rows(res)); PyObject *row; MYSQL_ROW mysql_row; MYSQL_FIELD *fields = mysql_fetch_fields(res); int r, f; /* prepare result set */ for (r=0; r < mysql_num_rows(res); r++) { mysql_row = mysql_fetch_row(res); row = PyDict_New(); for (f=0; f < mysql_num_fields(res); f++) { PyObject *key = Py_BuildValue("y", fields[f].name); PyObject *value = Py_BuildValue("y", (char *)mysql_row[f]); PyDict_SetItem(row, key, value); Py_DECREF(key); Py_DECREF(value); } PyTuple_SetItem(result, r, row); } mysql_free_result(res); return result; } else { raise_error("Unable to get result set") } } else { raise_error("Unable to fetch list of mysql processes") }}/* Checks whether the connection to the server is working */PyDoc_STRVAR(_mdoc_ping,"ping() -> checks whether the connection to the server is working.\n\\n\If the connection has gone down and auto-reconnect is enabled an attempt to reconnect is made.\n\If the connection is down and auto-reconnect is disabled, mysql_ping() returns an error.");_m_funcbool(m_ping, mysql_ping(m->mysql))/* Execute sql statement and return new result object (if appropriate) */PyDoc_STRVAR(_mdoc_query,"query(sql, ...) -> execute sql query\n\\n\Execute a MySQL query and return data if available. If query did not produce any data, returns None instead.\n\If there is data available (select query), returns a mysql.Result object instance containing the data acquired.\n\Accepts up to 32 optional arguments that replace occurances of \"?\" character in the query string. Arguments\n\will be escaped before replace.");_m_header(m_query){ char *query; ssize_t len, i, result; struct { char *s; int l; } sql_args[33]; for (i=0; i<33; i++) { sql_args[i].s = NULL; sql_args[i].l = 0; } /* parse arguments */ if (!PyArg_ParseTuple(args, "s#|s#s#s#s#s#s#s#s#s#s#s#s#s#s#s#s#s#s#s#s#s#s#s#s#s#s#s#s#s#s#s#s#", &query, &len, &sql_args[0].s, &sql_args[0].l, &sql_args[1].s, &sql_args[1].l, &sql_args[2].s, &sql_args[2].l, &sql_args[3].s, &sql_args[3].l, &sql_args[4].s, &sql_args[4].l, &sql_args[5].s, &sql_args[5].l, &sql_args[6].s, &sql_args[6].l, &sql_args[7].s, &sql_args[7].l, &sql_args[8].s, &sql_args[8].l, &sql_args[9].s, &sql_args[9].l, &sql_args[10].s, &sql_args[10].l, &sql_args[11].s, &sql_args[11].l, &sql_args[12].s, &sql_args[12].l, &sql_args[13].s, &sql_args[13].l, &sql_args[14].s, &sql_args[14].l, &sql_args[15].s, &sql_args[15].l, &sql_args[16].s, &sql_args[16].l, &sql_args[17].s, &sql_args[17].l, &sql_args[18].s, &sql_args[18].l, &sql_args[19].s, &sql_args[19].l, &sql_args[20].s, &sql_args[20].l, &sql_args[21].s, &sql_args[21].l, &sql_args[22].s, &sql_args[22].l, &sql_args[23].s, &sql_args[23].l, &sql_args[24].s, &sql_args[24].l, &sql_args[25].s, &sql_args[25].l, &sql_args[26].s, &sql_args[26].l, &sql_args[27].s, &sql_args[27].l, &sql_args[28].s, &sql_args[28].l, &sql_args[29].s, &sql_args[29].l, &sql_args[30].s, &sql_args[30].l, &sql_args[31].s, &sql_args[31].l )) return NULL; PyObject *replace = PyUnicode_FromString("?"); /* prepare python statement */ PyObject *_query = PyUnicode_FromStringAndSize(query, len); /* parse arguments */ for (i=0; sql_args[i].s!=NULL; i++) { /* reserve memory */ char *to = (char *)malloc(sql_args[i].l*2+1); /* escape string */ int l = mysql_real_escape_string(m->mysql, to, sql_args[i].s, sql_args[i].l); /* store result */ PyObject *_to = PyUnicode_FromStringAndSize(to, l); PyObject *temp = PyUnicode_Replace(_query, replace, _to, 1); /* free memory */ free(to); Py_DECREF(_to); Py_DECREF(_query); _query = temp; } query = _PyUnicode_AsStringAndSize(_query, &len); Py_DECREF(replace); Py_DECREF(_query); /* try to execute sql statement */ result = mysql_real_query(m->mysql, query, len); if (!result) { /* determine */ int field_count = mysql_field_count(m->mysql); if (field_count) { /* define counters */ int i, j; /* there are results, create object and return it */ MySQLres *r = (MySQLres *)PyObject_New(MySQLres, &MySQLResObjectType); if (r == NULL) return NULL; PyObject *row; /* store result */ MYSQL_RES *mysql_res = mysql_store_result(m->mysql); if (mysql_res == NULL) { /* mysql_store_result should have returned data */ raise_error(mysql_error(m->mysql)); } MYSQL_FIELD *fields = mysql_fetch_fields(mysql_res); MYSQL_ROW mysql_row; /* store general parameters */ r->mysql = m->mysql; r->num_fields = mysql_num_fields(mysql_res); /* store field names */ r->field_count = field_count; r->fields = PyTuple_New(field_count); for (i=0; i < field_count; i++) { PyTuple_SetItem(r->fields, i, Py_BuildValue("s", fields[i].name)); } r->num_rows = mysql_num_rows(mysql_res); r->query = Py_BuildValue("s", query); r->field_count = field_count; r->data = PyTuple_New(r->num_rows); for (i=0; i < r->num_rows; i++) { /* prepare and store single result row */ if ((mysql_row = mysql_fetch_row(mysql_res)) != NULL) { if (!m->use_dict) { row = PyTuple_New(r->num_fields); for (j=0; j < r->num_fields; j++) { PyTuple_SetItem(row, j, Py_BuildValue("y", (char *)mysql_row[j])); } } else { row = PyDict_New(); PyObject *key, *value; for (j=0; j < r->num_fields; j++) { key = Py_BuildValue("y", fields[j].name); value = Py_BuildValue("y", (char *)mysql_row[j]); PyDict_SetItem(row, key, value); Py_DECREF(key); Py_DECREF(value); } } /* add row to result set */ PyTuple_SetItem(r->data, i, row); } } /* set other properties */ m->_affected_rows = mysql_affected_rows(m->mysql); m->_errno = mysql_errno(m->mysql); m->_error = (char *)mysql_error(m->mysql); m->_insert_id = mysql_insert_id(m->mysql); m->_info = (char *)mysql_info(m->mysql); /* free result */ mysql_free_result(mysql_res); /* return the newly created object */ return (PyObject *)r; } else { raise_error(mysql_error(m->mysql)); } } /* error, return it */ raise_error(mysql_error(m->mysql));}/* This function flushes tables or caches, or resets replication server information. */PyDoc_STRVAR(_mdoc_refresh,"refresh(options) -> flushes tables or caches, or resets replication server information\n\\n\The options argument is a bit mask composed from any combination of the following values.\n\Multiple values can be OR'ed together to perform multiple operations with a single call.\n\ REFRESH_GRANT - Refresh the grant tables, like FLUSH PRIVILEGES\n\ REFRESH_LOG - Flush the logs, like FLUSH LOGS\n\ REFRESH_TABLES - Flush the table cache, like FLUSH TABLES\n\ REFRESH_HOSTS - Flush the host cache, like FLUSH HOSTS\n\ REFRESH_STATUS - Reset status variables, like FLUSH STATUS\n\ REFRESH_THREADS - Flush the thread cache\n\ REFRESH_SLAVE - On a slave replication server, reset the master server information and restart the slave, like RESET SLAVE\n\ REFRESH_MASTER - On a master replication server, remove the binary log files listed in the binary log index and truncate the index file, like RESET MASTER\n\Returns True on success or False on fail.");_m_funcboolparam(m_refresh, int, options, "i", mysql_refresh(m->mysql, options))/* Asks the MySQL server to reload the grant tables */PyDoc_STRVAR(_mdoc_reload,"reload() -> asks the MySQL server to reload the grant tables.");_m_header(m_reload){ char query[] = "flush privileges;"; RETURN_BOOL(mysql_real_query(m->mysql, query, 17))}/* Rolls back the current transaction */PyDoc_STRVAR(_mdoc_rollback,"rollback() -> rolls back the current transaction\n\\n\As of MySQL 5.0.3, the action of this function is subject to the value of the completion_type system variable.\n\In particular, if the value of completion_type is 2, the server performs a release after terminating a transaction\n\and closes the client connection. The client program should call mysql_close() to close the connection from the client side.");_m_funcbool(m_rollback, mysql_rollback(m->mysql))/* Causes the database specified by db to become the default (current) database on the connection specified by mysql */PyDoc_STRVAR(_mdoc_select_db,"select_db(dbname) -> select database\n\\n\Causes the database specified by dbname to become the default (current) database. In subsequent queries, this database is the\n\default for table references that do not include an explicit database specifier.");_m_funcboolparam(m_select_db, const char *, db, "s", mysql_select_db(m->mysql, db))/* This function is used to set the default character set for the current connection */PyDoc_STRVAR(_mdoc_set_character_set,"set_character_set(charset) -> used to set the default character set\n\\n\The string csname specifies a valid character set name. The connection collation becomes the default collation of the character\n\set. This function works like the SET NAMES statement. Affects the character set used by escape_string()");_m_funcboolparam(m_set_character_set, const char *, csname, "s", mysql_set_character_set(m->mysql, csname))PyDoc_STRVAR(_mdoc_affected_rows, "Returns the number of rows changed (for UPDATE), deleted (for DELETE), or inserted (for INSERT).");PyDoc_STRVAR(_mdoc_client_info, "Returns a string that represents the client library version.");PyDoc_STRVAR(_mdoc_client_version, "Returns an integer that represents the client library version.");PyDoc_STRVAR(_mdoc_character_set_name, "Return default character set name for the current connection.");PyDoc_STRVAR(_mdoc_errno, "Returns the error code for the most recently invoked API function that can succeed or fail.");PyDoc_STRVAR(_mdoc_error, "Returns a string containing the error message for the most recently invoked API function that failed.");PyDoc_STRVAR(_mdoc_info, "Retrieves a string providing information about the most recently executed statement, but only for the statements listed here.");PyDoc_STRVAR(_mdoc_insert_id, "Returns the value generated for an AUTO_INCREMENT column by the previous INSERT or UPDATE statement.");PyDoc_STRVAR(_mdoc_host_info, "Returns a string describing the type of connection in use, including the server hostname.");PyDoc_STRVAR(_mdoc_proto_info, "Returns the protocol version used by current connection.");PyDoc_STRVAR(_mdoc_server_info, "Returns a string that represents the server version number.");PyDoc_STRVAR(_mdoc_server_version, "Returns the version number of the server as an integer.");PyDoc_STRVAR(_mdoc_ssl_cipher, "Returns the SSL cipher used for the given connection to the server.");PyDoc_STRVAR(_mdoc_sqlstate, "Returns a null-terminated string containing the SQLSTATE error code for the most recently executed SQL statement.");PyDoc_STRVAR(_mdoc_use_dict, "Set this to True in order to get dictionary results from select queries");/* mysql object members */static PyMemberDef m_objectMembers[] = { _m_member("affected_rows", T_INT, _affected_rows, READONLY, _mdoc_affected_rows), _m_member("client_info", T_STRING, _client_info, READONLY, _mdoc_client_info), _m_member("client_version", T_INT, _client_version, READONLY, _mdoc_client_version), _m_member("character_set_name", T_STRING, _character_set_name, READONLY, _mdoc_character_set_name), _m_member("errno", T_INT, _errno, READONLY, _mdoc_errno), _m_member("error", T_STRING, _error, READONLY, _mdoc_error), _m_member("info", T_STRING, _info, READONLY, _mdoc_info), _m_member("insert_id", T_LONG, _insert_id, READONLY, _mdoc_insert_id), _m_member("host_info", T_STRING, _host_info, READONLY, _mdoc_host_info), _m_member("proto_info", T_INT, _proto_info, READONLY, _mdoc_proto_info), _m_member("server_info", T_STRING, _server_info, READONLY, _mdoc_server_info), _m_member("server_version", T_INT, _server_version, READONLY, _mdoc_server_version), _m_member("ssl_cipher", T_STRING, _ssl_cipher, READONLY, _mdoc_ssl_cipher), _m_member("sqlstate", T_STRING, _sqlstate, READONLY, _mdoc_sqlstate),
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -