import pymysql
#查询的数据表
db2=pymysql.connect('172.16.33.100','root',':****@','tz_after_loan')
cursor2=db2.cursor() //其实就是用来获得python执行Mysql命令的方法,也就是操作游标
len2=cursor2.execute('select * from address ' )
#插入的数据表
db1=pymysql.connect('localhost','root','123456','cgo')
cursor1=db1.cursor()
sql="insert into address_test() value(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
num=11
for i in range(int(len2/num)):
print(i)
#获取num条记录
data1=cursor2.fetchmany(num)
#executemany能同时执行多条语句,第一个参数为sql模板字符串,第二个为sql的参数,是一个列表
cursor1.executemany(sql,data1)
#这样可以一次性全部导入
data2=cursor2.fetchall() //返回查询到的所有记录
cursor1.executemany(sql,data2)
db1.commit()
db1.close()
db2.close()
参考链接
records库可以快速的查询数据库,比操作游标cursor对象要好使,还支持导出为具体格式
支持:RedShift, Postgres, MySQL, SQLite, Oracle, and MS-SQL
import records
db = records.Database('mysql://root:123456@localhost/demo?charset=utf8')
rows = db.query("select * from names")
for row in rows:
print(row.id, row.name, row.age)
"""
2 大红 24
3 大壮 24
4 秀英 24
6 小明 23
7 大名 23
10 壮壮 25
"""
"""
mysql> select * from names;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 2 | 大红 | 24 |
| 3 | 大壮 | 24 |
| 4 | 秀英 | 24 |
| 6 | 小明 | 23 |
| 7 | 大名 | 23 |
| 10 | 壮壮 | 25 |
+----+--------+------+
"""
# 转为json
print(rows.as_dict())
# 导出为具体格式,支持: csv、yaml、json、xls、df(DataFrame)
print(rows.export("json"))
# 转为表格形式
print(rows.dataset)
import records
import pymysql
companyMap = {} # 字典类似hashmap,取值用 companyMap[“sex”]
addressMap = {}
def connectDB():
global approvalDB
global appDB
global ourDB
appDB = records.Database('mysql+pymysql://root:****@172.16.33.100/tz_app_prod?charset=utf8')
ourDB = records.Database('mysql+pymysql://root::****@@localhost/books?charset=utf8mb4')
approvalDB = records.Database('mysql+pymysql://root::****@@172.16.33.100/tz_approval_prod?charset=utf8')
print("init DB connection finish")
def scanCompanyInfo():
workRows = appDB.query("select * from job_infos")
for workRow in workRows:
insertCompany(workRow)
print("scan company finish")
def scanCompanyClassInfo():
workClassRows = approvalDB.query("select * from approval_infos")
for workClassRow in workClassRows:
updataCompany(workClassRow)
print("scan company class finish")
def updataCompany(workClassInfo):
if workClassInfo.company_industry_category == '':
k = 0
else:
k = int(workClassInfo.company_industry_category)
ourDB.query("update company set class ='%d' where company_name='%s'" % (k, workClassInfo.company_name))
def insertCompany(companyInfo):
if companyInfo.workplace in companyMap:
return
ourDB.query('INSERT INTO company (company_name, address_id, phone_num) VALUES(:name, :aID, :phone)',
name=companyInfo.workplace, aID=1, phone=companyInfo.workplace_phone)
if __name__ == "__main__":
connectDB()
scanCompanyInfo()
scanCompanyClassInfo()