65 lines
2.3 KiB
Python
65 lines
2.3 KiB
Python
# 脚本是为了将SQLite数据库中的数据迁移到MySQL数据库中。
|
||
# 专门针对使用sqlite阶段写的代码,如果后续直接对Mysql做操作,就不要用这个脚本
|
||
|
||
import sqlite3
|
||
import mysql.connector
|
||
|
||
TABLE_NAME = 'phosphorus_synthesis_info'
|
||
input('你确定TABLE_NAME是{}吗?'.format(TABLE_NAME))
|
||
|
||
# SQLite setup
|
||
sqlite_connection = sqlite3.connect('/home/ubuntu/workplace/LYT/llm-agent/phosphorus/doi_status.db') # Ensure this is your actual SQLite database file
|
||
sqlite_cursor = sqlite_connection.cursor()
|
||
|
||
# MySQL connection setup
|
||
mysql_connection = mysql.connector.connect(
|
||
host='100.84.94.73',
|
||
user='metadata_mat_papers',
|
||
password='siat-mic',
|
||
database='metadata_mat_papers'
|
||
)
|
||
mysql_cursor = mysql_connection.cursor()
|
||
|
||
# Define the SQLite query to retrieve data
|
||
sqlite_query = "SELECT doi, status, pdf_url FROM doi_status" # Ensure these field names match your SQLite table
|
||
|
||
# Function to check if a record exists in the MySQL database
|
||
def record_exists(doi, table_name):
|
||
query = f"SELECT COUNT(*) FROM `{table_name}` WHERE doi = %s"
|
||
mysql_cursor.execute(query, (doi,))
|
||
count = mysql_cursor.fetchone()[0]
|
||
return count > 0
|
||
|
||
# Function to update a record in the MySQL database
|
||
def update_record(doi, scihub_downlowded, pdf_url, table_name):
|
||
query = f"""
|
||
UPDATE `{table_name}`
|
||
SET scihub_downlowded = %s, pdf_url = %s
|
||
WHERE doi = %s
|
||
"""
|
||
mysql_cursor.execute(query, (scihub_downlowded, pdf_url, doi))
|
||
|
||
# Fetch data from SQLite
|
||
sqlite_cursor.execute(sqlite_query)
|
||
rows = sqlite_cursor.fetchall()
|
||
|
||
# Iterate over SQLite rows and update MySQL records
|
||
for row in rows:
|
||
doi, scihub_downlowded, pdf_url = row
|
||
if record_exists(doi, TABLE_NAME): # Replace with your actual MySQL table name
|
||
update_record(doi, scihub_downlowded, pdf_url, TABLE_NAME) # Adjust table name if necessary
|
||
else:
|
||
# You can choose to handle non-existent DOI entries differently if necessary
|
||
print(f"Record with DOI {doi} does not exist in MySQL database.")
|
||
|
||
|
||
# Commit the changes to the MySQL database
|
||
mysql_connection.commit()
|
||
|
||
# Close connections
|
||
sqlite_cursor.close()
|
||
sqlite_connection.close()
|
||
mysql_cursor.close()
|
||
mysql_connection.close()
|
||
|
||
print("Data migration from SQLite to MySQL completed successfully!") |