python - MySQLdb
1. LeeMdb.py#!/usr/bin/env python# coding=utf-8import MySQLdbclass LeeMySQLdb:def __init__(self,url,username,passwd,database):# 连接数据库self.db = MySQLdb.connect(url,usernam
·
1. LeeMdb.py
#!/usr/bin/env python
# coding=utf-8
import MySQLdb
class LeeMySQLdb:
def __init__(self,url,username,passwd,database):
# 连接数据库
self.db = MySQLdb.connect(url,username,passwd,database)
# 获取mysql操作游标
self.cursor = self.db.cursor()
# 创建数据库
def createDatabase(self,database_name,create_sql):
# 如果存在删除
self.cursor.execute('DROP TABLE IF EXISTS ' + database_name)
# 创建db
self.cursor.execute(create_sql)
# 操作数据
'''
insert/update/delete
'''
def operateDatabase(self,operate_sql):
#print operate_sql
try:
self.cursor.execute(operate_sql)
self.db.commit()
except:
print '操作失败!'
self.db.rollback()
# 查询数据
def selectDatabase(self,select_sql):
try:
result = self.cursor.execute(select_sql)
return result
except:
print '查询失败!'
# 关闭数据库
def closeDatabase(self):
self.db.close()
2. 读取本地.dat 将数据写入 mysql
#!/usr/bin/env python
# coding=utf-8
import os
import LeeMdb
# 创建数据库实例
mysql_db = LeeMdb.LeeMySQLdb('localhost','root','123','show160')
# 得到data 目录下的所有文件
file_names = [name for name in os.listdir('./data')
if os.path.isfile(os.path.join('data', name))]
#创建数据库
sql = '''
create table company_tbl (
company_id char(10) unique key primary key not null,
company_logo varchar(200),
company_name varchar(200),
company_service varchar(2000),
company_introduce varchar(2000),
company_address varchar(40),
company_linkman varchar(10),
company_tel varchar(50),
company_email varchar(50),
company_fax varchar(50)
)
'''
mysql_db.createDatabase('company_tbl',sql)
# 读取文件
for i in range(len(file_names)):
with open (os.path.join('data', file_names[i]), 'r') as f:
print i
company_info = f.read()
# 字符替换
company_info = company_info.replace('\n','')
# 字符分割
j = -1
info_list = []
for info in company_info.split('::'):
if(j == -1):
j = 0
continue
if(j < 10):
if(info == ''):
info = 'null'
info_list.append(info)
j += 1
else:
sql = 'insert ignore into company_tbl values('+info_list[0]+',"'+info_list[1]+'","'+info_list[2]+'","'+info_list[3]+'","'+info_list[4]+'","'+info_list[5]+'","'+info_list[6]+'","'+info_list[7]+'","'+info_list[8]+'","'+info_list[9]+'")'
mysql_db.operateDatabase(sql)
info_list = [info]
j = 1
更多推荐
已为社区贡献8条内容
所有评论(0)