⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mysql.c

📁 This a Python 3 module that adds support for a MySQL database, written entirely in C. It s purpose i
💻 C
📖 第 1 页 / 共 3 页
字号:
	_m_member("use_dict",		T_BOOL,		use_dict,		0,		_mdoc_use_dict),	{NULL}				/* sentinel */};/* mysql object methods */static PyMethodDef m_objectMethods[] = {	{"autocommit",		(PyCFunction)m_autocommit,		METH_VARARGS,	_mdoc_autocommit},	{"change_user",		(PyCFunction)m_change_user,		METH_VARARGS,	_mdoc_change_user},	{"commit",		(PyCFunction)m_commit,			METH_NOARGS,	_mdoc_commit},	{"create_db",		(PyCFunction)m_create_db,		METH_VARARGS,	_mdoc_create_db},	{"drop_db",		(PyCFunction)m_drop_db,			METH_VARARGS,	_mdoc_drop_db},	{"escape_string",	(PyCFunction)m_escape_string,		METH_VARARGS,	_mdoc_escape_string},	{"hex_string",		(PyCFunction)m_hex_string,		METH_VARARGS,	_mdoc_hex_string},	{"kill",		(PyCFunction)m_kill,			METH_VARARGS,	_mdoc_kill},	{"list_dbs",		(PyCFunction)m_list_dbs,		METH_VARARGS,	_mdoc_list_dbs},	{"list_fields",		(PyCFunction)m_list_fields,		METH_VARARGS,	_mdoc_list_fields},	{"list_processes",	(PyCFunction)m_list_processes,		METH_NOARGS,	_mdoc_list_processes},	{"list_tables",		(PyCFunction)m_list_tables,		METH_VARARGS,	_mdoc_list_tables},	{"ping",		(PyCFunction)m_ping,			METH_NOARGS,	_mdoc_ping},	{"query",		(PyCFunction)m_query,			METH_VARARGS,	_mdoc_query},	{"refresh",		(PyCFunction)m_refresh,			METH_VARARGS,	_mdoc_refresh},	{"reload",		(PyCFunction)m_reload,			METH_VARARGS,	_mdoc_reload},	{"rollback",		(PyCFunction)m_rollback,		METH_VARARGS,	_mdoc_rollback},	{"select_db",		(PyCFunction)m_select_db,		METH_VARARGS,	_mdoc_select_db},	{"set_character_set",	(PyCFunction)m_set_character_set,	METH_VARARGS,	_mdoc_set_character_set},	{NULL}			/* sentinel */};/* MySQL connection object definition */static PyTypeObject MySQLConnObjectType = {	PyVarObject_HEAD_INIT(NULL, 0)	"mysql.MySQL",	sizeof(MySQLconn),	0,	(destructor)m_dealloc,				/* tp_dealloc */	0,						/* tp_print */	0,						/* tp_getattr */	0,						/* tp_setattr */	0,						/* tp_compare */	(reprfunc)m_repr,				/* tp_repr */	0,						/* tp_as_number */	0,						/* tp_as_sequence */	0,						/* tp_as_mapping */	0,						/* tp_hash */	0,						/* tp_call */	0,						/* tp_str */	0,						/* tp_getattro */	0,						/* tp_setattro */	0,						/* tp_as_buffer */	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,	/* tp_flags */	0,						/* tp_doc */	0,						/* tp_traverse */	0,						/* tp_clear */	0,						/* tp_richcompare */	0,						/* tp_weaklistoffset */	0,						/* tp_iter */	0,						/* tp_iternext */	m_objectMethods,				/* tp_methods */	m_objectMembers,				/* tp_members */	0,						/* tp_getset */	0,						/* tp_base */	0,						/* tp_dict */	0,						/* tp_descr_get */	0,						/* tp_descr_set */	0,						/* tp_dictoffset */	0,						/* tp_init */	PyType_GenericAlloc,				/* tp_alloc */	m_new,						/* tp_new */	PyObject_Del,					/* tp_free */};/* result object methods *//* class destructor */static void r_dealloc(MySQLres *r){	int i;	for (i=0; i < r->num_rows; i++)	{		Py_DECREF(PyTuple_GetItem(r->data, i));	}	Py_DECREF(r->data);	Py_DECREF(r->query);	Py_DECREF(r->fields);	Py_TYPE(r)->tp_free((PyObject *)r);}/* result object representation */static PyObject *r_repr(MySQLres *r){	Py_INCREF(r->query);	return r->query;}/* result object extension methods *//* length */static Py_ssize_t r_mapping_length(MySQLres *r){	return (Py_ssize_t)r->num_rows;}/* subscript */static PyObject *r_mapping_subscr(MySQLres *r, PyObject *item){	long i = PyLong_AsLong(item);	if (i < r->num_rows) return PyTuple_GetItem(r->data, PyLong_AsLong(item));	else {		raise_error("array index out of range");	}}/* mapping methods */static PyMappingMethods r_mapping = {	(lenfunc)r_mapping_length,	(binaryfunc)r_mapping_subscr,	0};/* return new iteration object */static PyObject *r_iter(MySQLres *r){	MySQLresIter *ri = PyObject_GC_New(MySQLresIter, &MySQLIterObjectType);	if (ri == NULL) return NULL;	Py_INCREF(r);	ri->r = r;	ri->index = 0;	PyObject_GC_Track(ri);	return (PyObject *)ri;}/* make object behave like a function */_r_header(r_call){	Py_INCREF(r->data);	return r->data;}PyDoc_STRVAR(_rdoc_fields,	"This is a read-only attribute that returns a list of names of fields in the result set.");PyDoc_STRVAR(_rdoc_data,	"Read-only attribute containing the entire result set");PyDoc_STRVAR(_rdoc_num_rows,	"This attribute contains the number of rows fetched by the last query. Read only value.");PyDoc_STRVAR(_rdoc_num_fields,	"Contains the number of fields returned by the last query. Same as len(Result.fields). Read only value.");/* object members */static PyMemberDef r_objectMembers[] = {	{"fields",	T_OBJECT_EX,	offsetof(MySQLres, fields),	READONLY,	_rdoc_fields},	{"data",	T_OBJECT_EX,	offsetof(MySQLres, data),	READONLY,	_rdoc_data},	{"num_rows",	T_INT,		offsetof(MySQLres, num_rows),	READONLY,	_rdoc_num_rows},	{"num_fields",	T_INT,		offsetof(MySQLres, num_fields),	READONLY,	_rdoc_num_fields},	{NULL}};/* result object methods */static PyMethodDef r_objectMethods[] = {	{NULL}   /* sentinel */};/* MySQL result object definition */static PyTypeObject MySQLResObjectType = {	PyVarObject_HEAD_INIT(NULL, 0)	"mysql.Result",	sizeof(MySQLres),	0,	(destructor)r_dealloc,				/* tp_dealloc */	0,						/* tp_print */	0,						/* tp_getattr */	0,						/* tp_setattr */	0,						/* tp_compare */	(reprfunc)r_repr,				/* tp_repr */	0,						/* tp_as_number */	0,						/* tp_as_sequence */	&r_mapping,					/* tp_as_mapping */	0,						/* tp_hash */	(ternaryfunc)r_call,				/* tp_call */	0,						/* tp_str */	0,						/* tp_getattro */	0,						/* tp_setattro */	0,						/* tp_as_buffer */	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,	/* tp_flags */	0,						/* tp_doc */	0,						/* tp_traverse */	0,						/* tp_clear */	0,						/* tp_richcompare */	0,						/* tp_weaklistoffset */	(getiterfunc)r_iter,				/* tp_iter */	0,						/* tp_iternext */	r_objectMethods,				/* tp_methods */	r_objectMembers,				/* tp_members */	0,						/* tp_getset */	0,						/* tp_base */	0,						/* tp_dict */	0,						/* tp_descr_get */	0,						/* tp_descr_set */	0,						/* tp_dictoffset */	0,						/* tp_init */	0,						/* tp_alloc */	0,						/* tp_new */	PyObject_Del,					/* tp_free */};/* iterator object methods *//* iterator object destructor */static void ri_dealloc(MySQLresIter *ri){	PyObject_GC_UnTrack(ri);	Py_XDECREF(ri->r);	PyObject_GC_Del(ri);}/* return next item in current iteration */static PyObject *ri_iternext(MySQLresIter *ri){	if (ri->index < ri->r->num_rows)	{		PyObject *next = PyTuple_GetItem(ri->r->data, ri->index++);		Py_INCREF(next);		return next;	}	return NULL;}/* traverse */static int ri_traverse(MySQLresIter *ri, visitproc visit, void *arg){	Py_VISIT(ri->r);	return 0;}/* array iterator object definition */static PyTypeObject MySQLIterObjectType = {	PyVarObject_HEAD_INIT(NULL, 0)	"resultiterator",				/* tp_name */	sizeof(MySQLresIter),				/* tp_basicsize */	0,						/* tp_itemsize */	(destructor)ri_dealloc,				/* tp_dealloc */	0,						/* tp_print */	0,						/* tp_getattr */	0,						/* tp_setattr */	0,						/* tp_compare */	0,						/* tp_repr */	0,						/* tp_as_number */	0,						/* tp_as_sequence */	0,						/* tp_as_mapping */	0,						/* tp_hash */	0,						/* tp_call */	0,						/* tp_str */	PyObject_GenericGetAttr,			/* tp_getattro */	0,						/* tp_setattro */	0,						/* tp_as_buffer */	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,	/* tp_flags */	0,						/* tp_doc */	(traverseproc)ri_traverse,			/* tp_traverse */	0,						/* tp_clear */	0,						/* tp_richcompare */	0,						/* tp_weaklistoffset */	PyObject_SelfIter,				/* tp_iter */	(iternextfunc)ri_iternext,			/* tp_iternext */	0,						/* tp_methods */};PyDoc_STRVAR(module_doc,"This module provides various objects to access a MySQL database server.\n\\n\This module exports a MySQL object that can be used to connect to a MySQL database\n\server and execute queries on it. It allows a high level access to a MySQL server\n\functionality through a number of methods.\n\\n\A sample code that shows the basic use of this module:\n\\n\    #!/usr/bin/env python3.0\n\    import mysql\n\\n\    try:\n\        db = mysql.MySQL(\n\		username='user',\n\		password='password',\n\		database='database',\n\	)\n\	# this is equivalent to:\n\        # db = mysql.MySQL('user', 'password', 'database')\n\    except:\n\        import sys\n\        print('MySQL error: {0}'.format(db.error))\n\        sys.exit()\n\\n\    # db.use_dict = True\n\    res = db.query('SELECT * FROM test;')\n\    for r in res:\n\        print(r)\n\    \n\    # flush table cache\n\    db.refresh(mysql.REFRESH_TABLES)\n\\n\\n\The MySQL constructor accepts following arguments:\n\username, password, database, host, port. May be called with positional or keyword\n\arguments.\n\The above example is a simple one that aims to show basic functionality. and usage.\n\For a full list of objects methods and attributes, see below.\n\\n\Note: All attributes in MySQL object are read-only with one single exception: use_dict.\n\For more information, read description of attributes below.");/* global module methods */static PyMethodDef mysql_methods[] = {	{NULL, NULL, 0, NULL}  /* Sentinel */};/* module definition */static struct PyModuleDef mysqlModule = {	PyModuleDef_HEAD_INIT,	"mysql",	/* name of module */	module_doc,	/* module documentation, may be NULL */	-1,		/* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */	mysql_methods,	NULL,	NULL,	NULL,	NULL};/* integer constants */static struct { char *n; int v; } constants[] = {	{"REFRESH_GRANT",	REFRESH_GRANT},	{"REFRESH_LOG",		REFRESH_LOG},	{"REFRESH_TABLES",	REFRESH_TABLES},	{"REFRESH_HOSTS",	REFRESH_HOSTS},	{"REFRESH_STATUS",	REFRESH_STATUS},	{"REFRESH_THREADS",	REFRESH_THREADS},	{"REFRESH_SLAVE",	REFRESH_SLAVE},	{"REFRESH_MASTER",	REFRESH_MASTER},	{NULL}};/* Module init */PyMODINIT_FUNC PyInit_mysql(void) {	int i;	PyObject* m;	if (PyType_Ready(&MySQLConnObjectType) < 0) return NULL;	if (PyType_Ready(&MySQLResObjectType) < 0) return NULL;	m = PyModule_Create(&mysqlModule);	if (m == NULL) return NULL;	/* init objects */	Py_INCREF((PyObject *)&MySQLConnObjectType);	PyModule_AddObject(m, "MySQL", (PyObject *)&MySQLConnObjectType);	/* attach new exception object */	MySQLError = PyErr_NewException("mysql.error", NULL, NULL);	Py_INCREF(MySQLError);	PyModule_AddObject(m, "error", MySQLError);	/* add constants */	for (i=0; constants[i].n!=NULL; i++)	{		PyModule_AddIntConstant(m, constants[i].n, constants[i].v);	}	/* module */	return m;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -