select * from emp where empno=XX lock in share mode;加共享锁
update时,则锁住了
二、排它锁X锁
update emp set sal =XX where emp=XX;默认加排它锁
添加一个共享锁:select ** lock in share mode; 表明其它锁都胡扯
因为innodb默认为行锁,故其它行数据会锁吗???
处理docx文档入库(先在当前开发环境下安装以下文档处理模块)
pip install python-docx
下面是详细的代码
import pymysql.cursors
import docx
import re
def get_file(path):
'''获取文件'''
#获得word文档
file = docx.Document(path)
#print(file)
return file
def preproccess_file(file):
'''文件预处理'''
#输出文档段落数(行数)
paragraph_sum = len(file.paragraphs)
#print(paragraph_sum)
#输出每一段的内容
para_list = []
for para in file.paragraphs:
print(para.text)
para_list.append(para.text)
#合并字符串
file_text = ''.join(para_list)
print(file_text)
return file_text
def extract_file(keyword, file_text):
'''提取内容'''
#使用正则提取关键字后面的数字
result = re.findall('{}([0-9]*)'.format(keyword), file_text)
print(result)
return result
def sql_connection():
'''建立数据连接'''
#使用pymysql指令连接数据库
connection = pymysql.connect(host = '127.0.0.1', #要连接的数据库的IP地址
user = 'root', #登录的账户名,如果登录的是最高权限账户则为root
password = '123456', #对应的密码
db = 'Zzs_db', #要连接的数据库
charset = 'utf8mb4', #设置编码格式
#返回到Python的结果,以什么方式存储,如Dict.Cursor是以字典的方式存储
#如果不加这行数据是以元组方式返回
cursorclass = pymysql.cursors.DictCursor
)
return connection
def insert_text_to_sql(connection,search_result,keyword):
'''创建表'''
try:
# 使用cursor()方法获取操作游标word
with connection.cursor() as cursor:
'''判断表是否存在,如果不存在创建表'''
sql_show_table = 'SHOW TABLES'
cursor.execute(sql_show_table)
tables = cursor.fetchall()
table_list = []
for table in tables:
for value in table.values():
table_list.append(value)
if 'words' not in table_list:
sql = '''create table words(
name varchar(256) not null,
number int) engine=InnoDB DEFAULT CHARSET=utf8;
'''
cursor.execute(sql)
print('创建表成功')
# 插入数据
# 从数据库链接中得到cursor的数据结构
num = 0;
print('开始插入数据')
for i in search_result:
with connection.cursor() as cursor:
sql = " insert into words(name, price) VALUES (%s, %s)"
cursor.execute(sql,(keyword,i))
# 执行到这一行指令时才是真正改变了数据库,之前只是缓存在内存中
connection.commit()
num += 1;
print('成功插入{}条数据'.format(num))
print('数据插入完毕!')
except:
# 发生错误时回滚
connection.rollback()
finally:
# 关闭连接
connection.close()
def main():
path = '/Users/Zss/Desktop/word/test.docx'; #文件路径
keyword = '超算科技' #提取关键字
file = get_file(path) #获取文件内容
file_text = preproccess_file(file) #对文件内容预处理(把文本集中到一个字符串中)
search_result = extract_file(keyword, file_text) #提取内容
connection = sql_connection() #获取数据连接
insert_text_to_sql(connection,search_result,keyword) #最终结果插入数据库
if __name__ == '__main__':
main()