Python之 Mysql、多线程

一、Mysql

通过python操作mysql

1、增

import MySQLdb

# 打开门
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='dd',db='python3')
# 伸出手
cur = conn.cursor()

# 操作数据
sql = 'insert into usermg(id,name,address) values(%s,%s,%s)'
params = ('1','uu','usa')
reCount = cur.execute(sql,params)

# 提交请求
conn.commit()

# 把手伸回来
cur.close()

# 把门关上
conn.close()

print reCount

2、删

import MySQLdb

# 打开门
conn = MySQLdb.connect(host='127.0.0.1', user='root', passwd='dd', db='python3')
# 伸出手
cur = conn.cursor()
sql = 'delete from usermg where id = %s'
params = (1,)
reCount = cur.execute(sql,params)

# 提交请求
conn.commit()
# 把手伸回来
cur.close()

# 把门关上
conn.close()

3、改

(1)、修改一个

import MySQLdb
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='dd',db='python3')
cur = conn.cursor()
sql = 'update usermg set name = %s where id = %s '
params = ('pp','1')
reCount = cur.execute(sql,params)

conn.commit()

cur.close()
conn.close()

(2)、修改两个

import MySQLdb
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='dd',db='python3')

cur = conn.cursor()

sql = 'update count set money = %s where id =1'
params = (0,)
reCount = cur.execute(sql,params)

sql = 'update count set money = %s where id = 2'
params = (100,)
reCount = cur.execute(sql,params)
conn.commit()

cur.close()
conn.close()

4、查

import MySQLdb

# 打开门
conn = MySQLdb.Connect(host ='127.0.0.1',user='root',passwd='dd',db='python3')

# 伸出手
cur = conn.cursor() # 创建一个手

# 拿东西
# 这个操作影响了多少行(有多少行被操作了)
reCount = cur.execute('select * from userInfo')

# 把手伸回来
cur.close()
# 把门关上
conn.close()

print reCount
import MySQLdb

# 打开门
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='dd',db='python3')

# 伸出手
cur = conn.cursor()
# 拿东西
reCount = cur.execute('select * from userInfo')
data = cur.fetchall()

# 把手伸回来
cur.close()
# 把门关上
conn.close()

print reCount
print data

二、多线程

猜你喜欢

转载自blog.csdn.net/qq_37048504/article/details/82777238