`

Python 取 mysql 数据库操作

 
阅读更多


#!/bin/env python
#coding=utf-8
import os
import sys
import cgi
import cgitb
import re
import MySQLdb
from urllib import unquote
from comm import StrMD5
from comm import StrSqlHost
from comm import StrSqlUser
from comm import StrSqlPwsd
from comm import SessionLogin
from comm import SessionCheck

SIZE_PAGE = 10
print "Content-type:text/html\n\n"

try:
	form = cgi.FieldStorage()
	sess = form.getvalue('se')
	tasktype = form.getvalue('tasktype')
	account = form.getvalue('ac')
	role = form.getvalue('role')
	lang = form.getvalue('lang')
except:
	print "{\"Result\":false,\"ErrMsg\":\"system err\"}\n" 
	sys.exit()

client_ip = os.environ['REMOTE_ADDR']

if role < 0:
	role = "0"

if sess < 0:
	print "{\"Result\":false,\"ErrMsg\":\"no session\"}\n" 
	sys.exit()

try:
	conn = MySQLdb.connect(host = StrSqlHost(), user = StrSqlUser(), passwd = StrSqlPwsd())
except:
	print "{\"Result\":false,\"ErrMsg\":\"Database connection timeout(%d)\"}\n" %(sys._getframe().f_lineno, error)
	sys.exit()
	
error = SessionCheck(conn, sess, account)

if error < 0:
	conn.close()
	
	print "{\"Result\":false,\"ErrMsg\":\"system busy(%d/%d)\"}\n" %(sys._getframe().f_lineno, error)
	sys.exit()
elif error > 0:
	conn.close()

	print "{\"Result\":false,\"ErrMsg\":\"system timeout(%d)\"}\n" 	%(sys._getframe().f_lineno, error)
	sys.exit()

try:
	curs = conn.cursor()
except:
	conn.close()
	print "{\"Result\":false,\"ErrMsg\":\"system busy(%d)\"}\n" %(sys._getframe().f_lineno)
	sys.exit()


#lang
if tasktype=="10":
	sql = "UPDATE pavenas.manager SET lang=%s WHERE account='%s';" %(lang, account)	
	
	try:
		curs.execute(sql)
		conn.commit()
	except:
		print "{\"Result\":false,\"ErrMsg\":\"no lang\"}"
		conn.close()
		sys.exit()
	
	print "{\"Result\":\"su\",\"ErrMsg\":\"}"
	curs.close()	
	conn.close()
	sys.exit()
	
#add	
if tasktype=="1":
	UserName = form.getvalue('UserName')
	name = form.getvalue('na')
	dept = form.getvalue('dept')
	phone = form.getvalue('phone')
	email = form.getvalue('email')	
	uower = form.getvalue('uower')	
	pwd = form.getvalue('pwd')	

#	if name :
#		name = urllib.unquote(name)
#	if dept :
#		dept = urllib.unquote(dept)	
	if dept<0 :
		dept = ""
	if phone<0 :
		phone = ""
	if email<0 :
		email = ""		

	pwd = pwd.strip()
	sql = "insert into pavenas.manager (account, pwd,name,role,dept,mobile,mail) values('%s', '%s', '%s', %s, '%s', '%s','%s');" %(MySQLdb.escape_string(UserName),MySQLdb.escape_string(pwd),MySQLdb.escape_string(name),uower,MySQLdb.escape_string(dept),MySQLdb.escape_string(phone),MySQLdb.escape_string(email))
	
	try:
		curs.execute(sql)
		conn.commit()
	except:
		#print e.pgerror
		if lang == "1":
			print "{\"Result\":false,\"ErrMsg\":\"已有同名账号!\"}"
		else: 
			print "{\"Result\":false,\"ErrMsg\":\"Has the same name accounts!\"}"

		curs.close()
		conn.close()

		sys.exit()

	print "{\"Result\":\"操作成功\",\"ErrMsg\":\"\"}"	
	
	#系统日志
	#str = "成功"
	#str = str.decode('utf-8')
	sql= "insert into pavenas.logs (account,ip,clas,oper,memo) values('%s', '%s', %s, 'add account(%s)', 'success')" %(MySQLdb.escape_string(account),MySQLdb.escape_string(client_ip), uower, MySQLdb.escape_string(UserName))
	try:
		curs.execute(sql)
		conn.commit()
	except:
		#print e.pgerror
		print "{\"Result\":false,\"ErrMsg\":\"Add system error log!\"}"

		curs.close()
		conn.close()
		sys.exit()	

	curs.close()
	conn.close()
	sys.exit()	

#删除	
if tasktype == "2":
	userid_array = form.getvalue('userid_array')
	if userid_array < 0:
		print "{\"Result\":false,\"ErrMsg\":\"Delete error!\"}"
		conn.close()
		sys.exit()	
	
	str = userid_array.split(',')

	len = len(str) - 1

	if len :
		i = 0
		while(i<len):	
			sql = "SELECT account from pavenas.manager where id=%s;" %(str[i])
			try:
				curs.execute(sql)
				results = curs.fetchall()
			except MySQLdb.Error, e:
				print "{\"Result\":false,\"ErrMsg\":\"Delete the system error log!\"}"
				conn.close()
				sys.exit()	
	

			account1 = results[0][0]	
				
			sql = "delete from pavenas.manager where id=%s;"  %(str[i])
			try:
				curs.execute(sql)
				conn.commit()
			except:
				print "{\"Result\":false,\"ErrMsg\":\"Delete the system error log!\"}"
				conn.close()
				sys.exit()	
	
					
			sql = "insert into pavenas.logs (account,ip,clas,oper,memo) values('%s', '%s', %s, 'del(%s)', 'success');" %(MySQLdb.escape_string(account), MySQLdb.escape_string(client_ip),role, MySQLdb.escape_string(account1))
					
			try:
				curs.execute(sql)
				conn.commit()
			except:
				print "{\"Result\":false,\"ErrMsg\":\"Delete the system error log!\"}"
				conn.close()
				sys.exit()
			i = i+1

		print "{\"Result\":\"删除成功\",\"ErrMsg\":\"\"}"
		curs.close()
		conn.close()
		sys.exit()
	else:
		print "{\"Result\":false,\"ErrMsg\":\"Id get wrong\"}"
		curs.close()	
		conn.close()
		sys.exit()

#用户信息
if tasktype == "4":
	userid = form.getvalue('userid')
	
	sql =  "SELECT * from pavenas.manager WHERE id = %s;" %(userid)
	
	try:
		curs.execute(sql)
		results = curs.fetchall()
	except MySQLdb.Error, e:
		print "{\"Result\":false,\"ErrMsg\":\"result error!\"}"
		conn.close()
		sys.exit()	
	
	
	UserName = results[0][1].encode('utf-8')
	name =  results[0][3].encode('utf-8')
	
	ouwer_sgin = results[0][4]
	if len(results[0][5]) <=0:
		DepartMent = ""
	else:
		DepartMent = results[0][5].encode('utf-8')
	if len(results[0][6])<=0:
		phone = ""
	else:
		phone = results[0][6].encode('utf-8') 
	if len(results[0][7])<=0:
		EmailAddress = ""
	else:
		EmailAddress = results[0][7].encode('utf-8')


	print "{\"UserName\":\"%s\",\"name\":\"%s\",\"uower\":%d,\"DepartMent\":\"%s\",\"phone\":\"%s\",EmailAddress:\"%s\"}" %(UserName, name, ouwer_sgin, DepartMent, phone, EmailAddress)
	
	curs.close()	
	conn.close()
	sys.exit()	

		
#修改
if tasktype == "3":
	userid = form.getvalue('userid')
	pwd_sgin = form.getvalue('pwd_sgin')
	name = form.getvalue('na')
	dept = form.getvalue('dept')	
	phone = form.getvalue('phone')
	email = form.getvalue('email')	
	uower = form.getvalue('uower')	
	pwd = form.getvalue('pwd')	

	if name < 0:
		name = ""	
	#else:
		#name = unquote(name)
	if dept < 0:
		dept = ""
	#else:
		#dept =unquote(dept)

	
	
	if phone < 0:
		phone = ""
	if email < 0:
		email = ""	

		
	if pwd_sgin == "1":
		pwd = pwd.strip()
		sql = "UPDATE pavenas.manager SET  pwd='%s',name='%s', dept='%s', mobile='%s',mail='%s' WHERE id=%s;" %(MySQLdb.escape_string(pwd),MySQLdb.escape_string(name).decode('utf-8'), MySQLdb.escape_string(dept).decode('utf-8'), MySQLdb.escape_string(phone),MySQLdb.escape_string(email), userid)
	else:
		sql = "UPDATE pavenas.manager SET name='%s', dept='%s', mobile='%s', mail='%s' WHERE id=%s;" %(MySQLdb.escape_string(name), MySQLdb.escape_string(dept), MySQLdb.escape_string(phone), MySQLdb.escape_string(email),  userid)

	try:
		curs.execute(sql)
		conn.commit()
	except :
		print "{\"Result\":false,\"ErrMsg\":\"result error1!\"}"
		conn.close()
		sys.exit()	

	print "{\"Result\":\"操作成功\",\"ErrMsg\":\"\"}"


	#系统日志				
	sql = "SELECT account from pavenas.manager where id=%s;" %(userid)	
	
	try:
		curs.execute(sql)
		results = curs.fetchall()		
	except:
		print "{\"Result\":false,\"ErrMsg\":\"change the system error log!\"}"
		conn.close()
		sys.exit()		
	
	account1 = results[0][0]
	sql = "insert into pavenas.logs (account,ip,clas,oper,memo) values('%s', '%s', %s, 'Modify account(%s)', 'success');" %(MySQLdb.escape_string(account), MySQLdb.escape_string(client_ip), role, MySQLdb.escape_string(account1))
	
	try:
		curs.execute(sql)
		conn.commit()
	except:
		print "{\"Result\":false,\"ErrMsg\":\"change the system error log(1)!\"}"
		conn.close()
		sys.exit()	

	curs.close();
	conn.close()
	sys.exit()		

	
	

#用户信息修改
if tasktype == "5":
	username = form.getvalue('ac')
	name = form.getvalue('na')
	dept = form.getvalue('dept')
	phone = form.getvalue('phone')
	email = form.getvalue('email')	
		
	if dept < 0:
		dept = "";
	if phone < 0:
		phone = "";
	if email < 0:
		email = "";
	if dept < 0:
		dept = "";			

	sql =  "UPDATE pavenas.manager SET  name='%s', dept='%s', mobile='%s', mail='%s' WHERE account='%s';" %(   MySQLdb.escape_string(name), MySQLdb.escape_string(dept), MySQLdb.escape_string(phone), MySQLdb.escape_string(email), MySQLdb.escape_string(username))	

	try:
		curs.execute(sql)
		conn.commit()
	except:
		print "{\"Result\":false,\"ErrMsg\":\"result error(%d)!\"}" %(sys._getframe().flineno)
		conn.close()
		sys.exit()		
	

	print "{\"Result\":\"操作成功\",\"ErrMsg\":\"\"}"


	#系统日志		
	sql = "insert into pavenas.logs (account,ip,clas,oper,memo) values('%s', '%s', %s, 'Modify account(%s)', 'success');" %(MySQLdb.escape_string(account), MySQLdb.escape_string(client_ip), role, MySQLdb.escape_string(username))

	try:
		curs.execute(sql)
		conn.commit()
	except:
		print  "{\"Result\":false,\"ErrMsg\":\"Modify the system error log(%s)!\"}" %(sys._getframe().flineno)
		conn.close()
		sys.exit()
		
	curs.close();	
	conn.close()
	sys.exit()	
	
	
	

	
	
if tasktype == "8":	
	try:
		curs.execute('select * from pavenas.manager where account = \'%s\';' %(account))
		row = curs.fetchall()
	except:
		print "{\"Result\":false,\"ErrMsg\":\"result error(%d)!\"}" %(sys._getframe().flineno)
		curs.close()
		conn.close()
		sys.exit()	
	
	if len(row[0][3]) <= 0:
		usercount1 = "";
	else :
		usercount1 = row[0][3].encode('utf-8')	
	if len(row[0][5]) <= 0:
		DepartMent_user = "";
	else :
		DepartMent_user = (row[0][5]).encode('utf-8')	
	if len(row[0][6]) <= 0:
		phone_user = "";
	else :
		phone_user = row[0][6].encode('utf-8')	
	if len(row[0][7]) <= 0:
		EmailAddress_user = "";
	else :
		EmailAddress_user = row[0][7].encode('utf-8')
		

	print "{\"name_user\":\"%s\",\"DepartMent_user\":\"%s\",\"phone_user\":\"%s\",EmailAddress_user:\"%s\"}" %(usercount1, DepartMent_user, phone_user, EmailAddress_user)		
	
	curs.close()	
	conn.close()
	sys.exit()		
	

#用户密码修改
if tasktype == "6":	
	username = form.getvalue('ac')
	pwd = form.getvalue('pwd')
	pwd_yuan = form.getvalue('pwd_yuan')
	
	sql = "SELECT * from pavenas.manager WHERE account = '%s';" %(MySQLdb.escape_string(username))
	
	try:
		curs.execute(sql)
		results = curs.fetchall()
	except:
		print "{\"Result\":false,\"ErrMsg\":\"result error!\"}"
		conn.close()
		sys.exit()		
	pwd = pwd.strip()
	pwd_old = results[0][2].encode('utf-8')
	pwd_yuan = StrMD5(pwd_yuan)

	if pwd_old != pwd_yuan:
		if lang == 1:
			print "{\"Result\":false,\"ErrMsg\":\"原密码错误!\"}"
		else:
			print "{\"Result\":false,\"ErrMsg\":\"The original password mistake!\"}"
		
		conn.close()
		sys.exit()
		

	sql = "UPDATE pavenas.manager SET pwd='%s' WHERE account='%s';" %(MySQLdb.escape_string(pwd), MySQLdb.escape_string(username))

	try:
		curs.execute(sql)
		conn.commit()
	except:
		print "{\"Result\":false,\"ErrMsg\":\"result error\"}"
		conn.close()
		sys.exit()
	

	print "{\"Result\":\"操作成功\",\"ErrMsg\":\"\"}"

	#系统日志
	sql = "insert into pavenas.logs (account,ip,clas,oper,memo) values('%s', '%s', %s, 'Modify account(%s)Password', 'success');" %(MySQLdb.escape_string(account), MySQLdb.escape_string(client_ip), role, MySQLdb.escape_string(username))
	try:
		curs.execute(sql)
		conn.commit()
	except:
		print "{\"Result\":false,\"ErrMsg\":\"Modify the system error log password!\"}"
		conn.close()
		sys.exit()	
	
	curs.close()
	conn.close()
	sys.exit()	
	
	
	
	
#过滤
if tasktype == "7":
	currpage = form.getvalue('page')
	guolv_account = form.getvalue('guolv_account')
	guolv_caoz = form.getvalue('guolv_caoz')
	guolv_role = form.getvalue('guolv_select')		
	
	if guolv_account < 0:
		guolv_account2 = ""
	else:
		guolv_account = unquote(guolv_account)
		guolv_account2 = guolv_account.replace("_", "\\_")
		guolv_account2 = guolv_account2.replace("%", "\\%")
		guolv_account2 = guolv_account2.replace("\\", "\\\\")	
		#guolv_account1 = " and account like '%%%s%%'" %(guolv_account2)
		#guolv_account1 = " and account like '%%%s%%'" %(MySQLdb.escape_string((guolv_account)))

	if guolv_caoz < 0 :
		guolv_caozuo2 = ""
		guolv_caoz = ""
	else:
		guolv_caoz = unquote(guolv_caoz)
		guolv_caozuo2 = guolv_caoz.replace("_", "\\_")
		guolv_caozuo2 = guolv_caozuo2.replace("%", "\\%")
		guolv_caozuo2 = guolv_caozuo2.replace("\\", "\\\\")
		#guolv_caozuo1 = " and name like '%%%s%%'" %(guolv_caozuo2)
		#guolv_caozuo1 = " and name like '%%%s%%'" %(MySQLdb.escape_string(guolv_caoz))


	guolv_role1 = "role=1" 
	
	row = 0

	sql = "select count(*) from pavenas.manager where role=1  and name like '%%%s%%' and account like '%%%s%%' and account!= 'admin';" %(guolv_caozuo2, guolv_account2)
	

	try:
		#curs.execute("select count(*) from pavenas.manager where %s %s %s and account!= 'admin';" %(guolv_role1, guolv_account1, guolv_caozuo1))
		curs.execute(sql)
	except:
		print "{\"Result\":false,\"ErrMsg\":\"result error(%d)\"}" %(sys._getframe().f_lineno)
		conn.close()
		sys.exit()	
		

#	if curs.rowcount < 1:
#		print "{\"total\":0,\"start\":0,\"row\":0,\"data\":[]}"
#		conn.close()
#		sys.exit()
		

	row = curs.fetchone()[0]
	if row == 0:
		print "{\"total\":0,\"start\":0,\"row\":0,\"data\":[]}"
		conn.close()
		sys.exit()
		
		
	if row > 0:
		left = row % SIZE_PAGE
		totalPage = row / SIZE_PAGE + (1 if left else 0)
		if left == 0:
			left = SIZE_PAGE

		if currpage < 0:
			currpage = 1
		elif int(currpage) > totalPage:
			currpage = totalPage
		else:
			currpage = int(currpage)

		if 	currpage == 0:
			currpage = 1
		if currpage != totalPage:
			left = SIZE_PAGE	
	

	#sql = "SELECT * from pavenas.manager where %s %s %s and account!= 'admin' order by id asc limit %u offset %u;" %(guolv_role1, guolv_account1, guolv_caozuo1, left, (currpage - 1) * SIZE_PAGE)
	
	sql = "SELECT * from pavenas.manager where role=1  and name like '%%%s%%' and account like '%%%s%%' and account!= 'admin' order by id asc limit %u offset %u;" %(guolv_caozuo2, guolv_account2, left, (currpage - 1) * SIZE_PAGE)	

	try:
		curs.execute(sql)
		results = curs.fetchall()
	except:
		print "{\"Result\":false,\"ErrMsg\":\"result error\"}"
		conn.close()
		sys.exit()	
		
	total = 0
	result_str1 = ""
	result_str = ""
	if not results:
		print "{\"total\":0,\"start\":0,\"row\":0,\"data\":[]}\n"		
	else:
		for result in results: 
			result_id = result[0]
			#print "var result_id=%d;" %(result_id)
			result_account = result[1].encode('utf-8')
			#print "var result_account=\"%s\";" %(result_account)
			result_pwd = result[2].encode('utf-8')
			#print "var result_pwd=\"%s\";" %(result_pwd)
			result_name = result[3].encode('utf-8')
			#print "var result_name=\"%s\";" %(result_name)			
			result_role = result[4]
			#print "var result_role=\"%d\";" %(result_role)			
			result_state = result[8]
			#print "var result_state=\"%d\";" %(result_state)	
			result_lang = result[9]
			#print "var result_lang=\"%d\";" %(result_lang)	
			

			result_str = "{\"id\":%d,\"account\":\"%s\",\"name\":\"%s\",\"role\":1,\"lang\":%d,\"state\":%d}" %(result_id, result_account, result_name, result_lang, result_state)
			
			if total < 10:
				if total > 0:
					result_str1 = result_str1 + "," +result_str
				else:
					result_str1 = result_str
			
			total = total + 1
			

		print "{\"total\":%d,\"start\":%d,\"row\":10,\"data\":[%s]}"	%(row, (currpage - 1) * SIZE_PAGE, result_str1)			
		
	
	curs.close()
	conn.close()
	sys.exit()	
	


#分页
if tasktype == "9":
	currpage = form.getvalue('page')
	
	row = 0
	sql = "select count(*) from pavenas.manager where account!= 'admin';" 
	
	try:
		curs.execute(sql) 
	except:
		print "{\"Result\":false,\"ErrMsg\":\"result error\"}"%(sys._getframe().f_lineno)
		conn.close()
		sys.exit()			

	if curs.rowcount < 1:
		print "{\"total\":0,\"start\":0,\"row\":0,\"data\":[]}"
		conn.close()
		sys.exit()			
	
	row = curs.fetchone()[0]

	currpage = int(currpage)
	if row > 0:
		left = row % SIZE_PAGE
		totalPage = row / SIZE_PAGE + (1 if left else 0)
		if left == 0:
			left = SIZE_PAGE

		if currpage < 1:
			currpage = 1
		elif currpage > totalPage:
			currpage = totalPage

		if currpage != totalPage:
			left = SIZE_PAGE	
	

	sql = "SELECT * from pavenas.manager where account!= 'admin' order by id asc limit %u offset %u;" %(left, (currpage - 1) * SIZE_PAGE)
	
	try:
		curs.execute(sql)
		results = curs.fetchall()
	except:
		print "{\"Result\":false,\"ErrMsg\":\"result error\"}"
		conn.close()
		sys.exit()	
		
	total = 0
	result_str1 = ""
	result_str = ""
	if not results:
		print "{\"total\":0,\"start\":0,\"row\":0,\"data\":[]}\n"		
	else:
		for result in results: 
			result_id = result[0]
			#print "var result_id=%d;" %(result_id)
			result_account = result[1].encode('utf-8')
			#print "var result_account=\"%s\";" %(result_account)
			result_pwd = result[2].encode('utf-8')
			#print "var result_pwd=\"%s\";" %(result_pwd)
			result_name = result[3].encode('utf-8')
			#print "var result_name=\"%s\";" %(result_name)			
			result_role = result[4]
			#print "var result_role=\"%d\";" %(result_role)			
			result_state = result[8]
			#print "var result_state=\"%d\";" %(result_state)	
			result_lang = result[9]
			#print "var result_lang=\"%d\";" %(result_lang)	
			

			result_str = "{\"id\":%d,\"account\":\"%s\",\"name\":\"%s\",\"role\":1,\"lang\":%d,\"state\":%d}" %(result_id, result_account, result_name, result_lang, result_state)
			
			if total < 10:
				if total > 0:
					result_str1 = result_str1 + "," +result_str
				else:
					result_str1 = result_str
			
			total = total + 1
			

		print "{\"total\":%d,\"start\":%d,\"row\":10,\"data\":[%s]}"	%(row, (currpage - 1) * SIZE_PAGE,result_str1)			
		
	
	curs.close()
	conn.close()
	sys.exit()	






0
0
分享到:
评论

相关推荐

    Python:使用python读取mysql数据库 .docx

    使用python读取mysql数据库使用python读取mysql数据库使用python读取mysql数据库使用python读取mysql数据库使用python读取mysql数据库使用python读取mysql数据库使用python读取mysql数据库使用python读取mysql数据库...

    python读写MySQL数据库操作

    python读写MySQL数据库操作,读操作,写操作,python读写MySQL数据库操作

    Python访问MySQL数据库

    摘 要: 讲解了应用 Python 如何访问 MySQL 数据库。首先介绍了 Python、MySQL,然后讲解 MySQL 数据库访问模块,并通过一个实例来说明应用该模块来访问 MySQL 数据库,最后总结了用 Python 访问 MySQL 数据库的优点

    Python的MySQL数据库操作-修改版

    本文将详细介绍如何使用Python语言操作MySQL数据库,包括连接数据库、执行SQL语句、插入和查询数据以及关闭数据库连接等基本操作。 首先,要使用Python与MySQL数据库进行交互,需要安装并使用MySQLdb模块,这是一个...

    python连接mysql数据库脚本工具

    通过对这个文件进行解压和研究,你可以更深入地了解如何在实际项目中使用Python连接MySQL数据库,执行各种数据库操作。 总结来说,Python的`pymysql`库为我们提供了一个简单而强大的方式来与MySQL数据库进行交互。...

    Python访问Mysql数据库

    - **跨平台**: Python的跨平台能力意味着可以在任何操作系统上使用相同的代码访问MySQL数据库。 - **社区支持**: Python和MySQL都有强大的社区支持,遇到问题时可以迅速获得帮助。 通过上述介绍可以看出,Python...

    基于Python的MYSQL数据库访问技术.pdf

    本文将深入探讨如何使用Python语言访问MySQL数据库,通过实例演示如何在Eclipse集成开发环境中通过Python-MySQLconnector实现数据库的增、删、改、查操作。 首先,让我们了解Python DB-API的相关知识。在引入DB-API...

    python备份mysql数据库

    当谈到使用Python来备份MySQL数据库时,我们可以借助一些工具和库实现这一目标。在这个场景中,"python备份mysql数据库"的实践主要涉及到以下几个核心知识点: 1. Python语言:Python是一种高级编程语言,以其简洁...

    python操作mysql数据库.pdf

    Python 操作 MySQL 数据库 Python 操作 MySQL 数据库是通过 Python DB-API 实现的,Python DB-API 为开发人员提供了数据库应用编程接口。Python 数据库接口支持非常多的数据库,可以选择适合项目的数据库,如 ...

    python连接MySQL数据库问题.docx

    在进行数据分析或 Web 开发时,经常需要使用 Python 操作 MySQL 数据库。本文将详细介绍如何使用 Python 连接到 MySQL 数据库,并解释 `cursor()`、`execute()` 和 `fetchall()` 方法的具体作用。 #### 一、Python ...

    python连接mysql数据库示例代码

    1. 如何使用 Python 的 MySQL 连接器库(如 `mysql-connector-python`)进行数据库连接和操作。 2. 数据库的基本操作,包括创建数据库和表、插入数据、查询数据、更新数据和删除数据的实现方法。 3. 理解数据库连接...

    Python的MySQL数据库操作

    Python MySQL 数据库操作 Python 作为一门流行的编程语言,在与 MySQL 数据库进行交互时,需要遵循一定的步骤。下面我们将详细介绍 Python 如何与 MySQL 数据库进行交互。 建立连接 在开始与 MySQL 数据库进行...

    python数据库(mysql)连接代码

    python连接mysql数据库代码。必须先下载安装mysql数据库,并启动数据库

    Python操作mysql数据库

    Python操作mysql数据库

    【PyMySQL】python3 mysql数据库操作工具

    PyMySQL是Python连接MySQL数据库的一个库,它提供了类似于Python标准库DB-API(PEP 249)的接口,使得在Python中操作MySQL变得简单易行。 ### 安装PyMySQL 要开始使用PyMySQL,首先需要安装这个库。在安装过程中,...

    python操作mysql数据库上课讲义.pdf

    总的来说,Python的DB-API和MySQLdb库为Python程序员提供了便利的工具,能够高效地处理MySQL数据库的各类操作,无论是在Web开发、数据分析还是自动化任务中,都是连接和管理MySQL数据库的常用选择。

    Python 连接MySQL数据库时自动开启服务器 Python源码

    Python 连接MySQL数据库时自动开启服务器 Python源码Python 连接MySQL数据库时自动开启服务器 Python源码Python 连接MySQL数据库时自动开启服务器 Python源码Python 连接MySQL数据库时自动开启服务器 Python源码...

    Python的MySQL数据库操作.docx

    ### Python的MySQL数据库操作知识点详解 #### 一、Python与MySQL数据库交互流程 根据文档标题“Python的MySQL数据库操作”及描述中提到的程序与数据库交互的三个基本步骤,我们可以详细探讨这一流程。 1. **建立...

Global site tag (gtag.js) - Google Analytics