python 访问数据库一:添加部门
来源:互联网 发布:2016淘宝818 编辑:程序博客网 时间:2024/06/07 03:45
import random
import string
'''conn = MySQLdb.connect(host='192.168.*.**',
port = ****,
user='*****',
passwd='****',
db ='****'
)
cur = conn.cursor()
#一次插入多条记录 executemany
sqli="insert into t_department(ParentDepID,DepName,Status,CreateDate,CompanyId) values(%s,%s,%s,%s,%s)"
cur.executemany(sqli,[
('50020','Tom','1','2017-05-16 15:41:02','39375'),
('50020','Jack','1','2017-05-16 15:41:02','39375'),
])
print "update success"
cur.close()
conn.commit()
conn.close()'''
#################################################################插入单条操作
def addDepartment():
'conn = MySQLdb.connect(host='192.168.*.**',
port = ****,
user='*****',
passwd='****',
db ='****'
)
cur = conn.cursor()
comid = raw_input("Input your companyid: ")
times = raw_input("Input your department number to increase: ")
num = int(times)
for i in range (1,num+1):
sqli="insert into t_department(ParentDepID,DepName,Status,CreateDate,CompanyId) values(%s,%s,%s,%s,%s)"
param=('50020','name','1','2017-05-16 15:41:02',comid)
cur.execute(sqli,param)
print "update success"
cur.execute("select count(*) from t_department where CompanyId = 39375 ")
result = cur.fetchmany()
print("该公司部门总数量: %s" % (result))
cur.close()
conn.commit()
conn.close()
if __name__ == "__main__":
addDepartment()
################################################尝试用定义函数,分层
def openConn():
conn = MySQLdb.connect(host='192.168.*.**',
port = ****,
user='*****',
passwd='****',
db ='****'
)
return conn
def addDepartment():
con = openConn()
cur = con.cursor()
comid = raw_input("Input your companyid: ")
times = raw_input("Input your department number to increase: ")
num = int(times)
for i in range (1,num+1):
sqli="insert into t_department(ParentDepID,DepName,Status,CreateDate,CompanyId) values(%s,%s,%s,%s,%s)"
param=('50020','name','1','2017-05-16 15:41:02',comid)
cur.execute(sqli,param)
print "update success"
cur.execute("select count(*) from t_department where CompanyId = 39375 ")
result = cur.fetchmany()
print("该公司部门总数量: %s" % (result))
cur.close()
con.commit()
con.close()
if __name__ == "__main__":
addDepartment()
- python 访问数据库一:添加部门
- 添加数据库访问权限
- 采用python中SQLalchemy模块访问数据库(一)
- 采用python中SQLalchemy模块访问数据库(一)
- python 访问 sqlite 数据库
- python 访问 sqlite 数据库
- python访问ZODB数据库
- python访问数据库
- Python数据库访问
- python访问MySQL数据库
- 树莓派上python访问数据库
- python访问MySQL数据库
- python访问mysql数据库
- Android学习笔记:访问和添加通讯录中的联系人和联系人数据库表简介一
- 部门数据库设计
- python访问纯真IP数据库
- Python访问SQL Server数据库
- 【python小记】访问mysql数据库
- 非局部均值滤波matlab代码理解
- 廖雪峰的Git教程十二-分支管理策略
- Android中ExpandableListView的使用
- Kalman Filter
- N
- python 访问数据库一:添加部门
- Sprite Editor
- 对象的组合
- 读书笔记《Effective C++》条款28:避免返回handles指向对象内部成分
- 《Ray Tracing in One Weekend》——Chapter 1: Output an image
- 如何让C#编译不安全代码(含unsafe的代码)
- Ubuntu8.04软件源(亲测无误)
- VCS-5续 LVM和VxVM
- LineSegment类