python3实现读取一个数据库中的数据填充到另一个数据库中

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import pymysql as MySQLdb
import redis

#打开redis连接
r=redis.Redis(host='localhost',port=6379,decode_responses=True)


# 打开数据库连接
db = MySQLdb.connect("localhost", "root", "test", "jian", charset='utf8',cursorclass = MySQLdb.cursors.DictCursor )

db_test = MySQLdb.connect("localhost", "root", "test", "test", charset='utf8' )
# 使用cursor()方法获取操作游标
cursor = db.cursor()
cursor_test=db_test.cursor()
# SQL 查询语句
sql = "SELECT * FROM person where id < 5"
try:
   # 执行SQL语句
   cursor.execute(sql)
   # 获取所有记录列表
   results = cursor.fetchall()
   for row in results:
     name=row["name"]
     sex=row["sex"]
     birthday=row["birthday"]
     degree=row["degree"]
     top_education_field=row["top_education_field"]
     marital_status=row["marital_status"]
     current_country=row["current_country"]
     current_province=row["current_province"]
     current_city=row["current_city"]
     current_district=row["current_district"]
     current_address=row["current_address"]
     household_registration=row["household_registration"]
     id_card=row["id_card"]
     mobile=row["mobile"]
     email=row["email"]
     expected_country=row["expected_country"]
     expected_province=row["expected_province"]
     expected_city=row["expected_city"]
     expected_district=row["expected_district"]
     expected_address=row["expected_address"]
     expected_salary_min=row["expected_salary_min"]
     expected_salary_max=row["expected_salary_max"]
     current_situation=row["current_situation"]
     standard_job_industry=row["standard_job_industry"]
     standard_job_function=row["standard_job_function"]
     expected_job_industry=row["expected_job_industry"]
     expected_job_function=row["expected_job_function"]
     self_evaluation=row["self_evaluation"]
     sql_insert = "INSERT INTO person (name,sex,birthday,degree,top_education_field,marital_status,current_country,current_province,current_city,current_district,current_address,household_registration,id_card,mobile,email,expected_country,expected_province,expected_city,expected_district,expected_address,expected_salary_min,expected_salary_max,current_situation,standard_job_industry,standard_job_function,expected_job_industry,expected_job_function,self_evaluation)  VALUES ('{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}')".format(name,sex,birthday,degree,top_education_field,marital_status,current_country,current_province,current_city,current_district,current_address,household_registration,id_card,mobile,email,expected_country,expected_province,expected_city,expected_district,expected_address,expected_salary_min,expected_salary_max,current_situation,standard_job_industry,standard_job_function,expected_job_industry,expected_job_function,self_evaluation)
     cursor_test.execute(sql_insert)
     person_id=row["id"]
     
     sql1="select * from person_education_experience where person_id='%d'"%(person_id)
     new_person_id=db_test.insert_id()
     print("^"*30)
     print(sql1)	
     # 执行SQL语句
     cursor.execute(sql1)
     # 获取所有记录列表
     print("&"*30)
     results1 = cursor.fetchall()
     print(results1)
     if results1 != None:
        for row1 in results1:
           #print(row1)
           p_id=new_person_id
           start_time=row1["start_time"]
           end_time=row1["end_time"]
           institution_name=row1["institution_name"]
           education_field=row1["education_field"]
           education_description=row1["education_description"]
           education_level=row1["education_level"]
           sql_edu="insert into person_education_experience (person_id,start_time,end_time,institution_name,education_field,education_description,education_level) values('{}','{}','{}','{}','{}','{}','{}')".format(p_id,start_time,end_time,institution_name,education_field,education_description,education_level)
           cursor_test.execute(sql_edu) 

     sql2="select * from person_work_experience where person_id='%d'"%(person_id)

     print("#"*30)

     # 执行SQL语句
     cursor.execute(sql2)
     # 获取所有记录列表
     results2 = cursor.fetchall()
     if results2 != None:
        for row2 in results2:
           #print(row2)
           p_id=new_person_id
           start_time=row2["start_time"]
           end_time=row2["end_time"]
           company_name=row2["company_name"]
           job_title=row2["job_title"]
           job_description=row2["job_description"]
           salary_min=row2["salary_min"]
           salary_max=row2["salary_max"]
           sql_work="insert into person_work_experience (person_id,start_time,end_time,company_name,job_title,job_description,salary_min,salary_max) values('{}','{}','{}','{}','{}','{}','{}','{}')".format(p_id,start_time,end_time,company_name,job_title,job_description,salary_min,salary_max)
           cursor_test.execute(sql_work)

     db.commit() #千万别忘了要提交
except:
  print("Error: unable to fecth data")

# 关闭数据库连接
db.close()
db_test.close()

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: Python是一种强大的编程语言,可以轻松地将Excel数据数据按照自定义的规则批量导入到数据库实现这个功能需要使用Python的开源库——pandas、xlrd和sqlalchemy。 首先,我们需要安装这些库。在Python环境使用pip命令进行安装。 接下来,我们需要导入这些库,并使用pandas读取Excel数据数据。使用read_excel方法读取Excel文件,并将数据存储到DataFrame对象。 然后,我们需要使用SQLAlchemy创建数据库连接,并创建需要将数据导入的数据表。这可以通过使用create_engine函数创建SQLAlchemy Engine对象,并使用pandas提供的to_sql方法将数据写入数据库。 最后,我们需要在代码实现逻辑来确定如何读取Excel数据数据,以及如何将其映射到数据库的列。这可以通过使用pandas的DataFrame和SQLAlchemy的Table对象来实现。 总的来说,使用Python将Excel表数据批量导入到数据库一个很容易的任务,只需要使用一些简单的函数和库,就可以完成这个任务。这个过程可以帮助用户快速地导入数据,从而简化其重复性工作。 ### 回答2: Python是一种经常用于数据处理和分析的编程语言,它提供了许多现成的库和模块来方便地将不同格式的数据导入数据库。而在导入数据时,Excel数据表通常是一种常见的格式,这种格式的数据可以通过Python来批量导入到数据库。 为了实现这一目的,需要使用Python的pandas库来读取Excel数据表,并将其转换为DataFrame格式,然后使用pandas库的to_sql()方法将DataFrame数据批量插入到数据库。要执行此操作,需要安装和配置相应的数据库连接器,如MySQLdb或psycopg2等。 首先,需要通过Python的pandas库创建一个DataFrame对象来加载Excel数据表。可以使用read_excel()方法读取Excel文件数据,并将其转换为DataFrame格式。必要时,可以通过pandas库的方法来处理数据,例如清除或填充缺失值,并对数据进行格式转换。 然后,可以使用pandas库的to_sql()方法创建一个数据库连接,并将DataFrame数据批量插入到数据库。需要提供有关数据库连接的详细信息,例如主机名称、端口、数据库名称、用户名和密码等。此外,还需要指定要将数据插入的表名和数据列名。 这样,Excel数据表的数据就可以通过使用Python和pandas库批量导入到数据库。这个过程可以自动化,并在每次需要更新数据库数据时使用。这可以大大减少手动处理数据的工作量,并提高数据的处理效率和准确性。 ### 回答3: 在进行数据处理过程Python的excel模块可以很好地对Excel数据表进行读取和处理。通过使用Python的pandas模块,我们可以轻松地将Excel数据数据批量导入到数据库。 首先,我们需要使用pandas读取Excel文件,并将数据存储在一个pandas的dataframe对象。然后,我们可以使用Python的SQLAlchemy模块来连接我们的数据库,并在其创建一个表格。接下来,我们将使用dataframe对象数据将其插入到数据库。 在这个过程,我们可以使用一些技巧将数据转换为数据库需要的格式,例如使用pandas的DatetimeIndex来将日期时间格式化为数据库正确的格式。 总体来说,使用Python将Excel数据数据批量导入数据库是非常简单和高效的。这个过程可以大大提高数据处理的效率,并支持更好的数据分析和可视化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

reg183

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值