#!/usr/bin/python
import MySQLdb
# Open database connection
db = MySQLdb.connect("localhost","root","Fav!360","blog_360converter" )
# prepare a cursor object using cursor() method
cursor = db.cursor()
# Prepare SQL query to INSERT a record into the database.
sqlSearch = """select * from wp_postmeta where post_id=# and meta_key='_yoast_wpseo_focuskw';"""
sqlUpdate1 = """INSERT INTO wp_postmeta(post_id, meta_key, meta_value) select id,'_yoast_wpseo_metadesc', post_title from wp_posts where id=#;"""
sqlUpdate2 = """INSERT INTO wp_postmeta(post_id, meta_key, meta_value) select id,'_yoast_wpseo_focuskw', post_title from wp_posts where id=#;"""
sqlUpdate3 = """INSERT INTO wp_postmeta(post_id, meta_key, meta_value) select id,'_yoast_wpseo_title', post_title from wp_posts where id=#;"""
sqlAllids = """select distinct(id) from wp_posts"""
cursor.execute(sqlAllids)
rows = cursor.fetchall()
for row in rows:
try:
# Execute the SQL command
sqlSearch1 = sqlSearch.replace( "#", str(row[0]))
print "search sql:", sqlSearch1
cursor.execute(sqlSearch1)
if cursor.rowcount == 0:
print str(row[0]) + ' not found, now insert ———————————'
sqlUpdate11 = sqlUpdate1.replace( "#", str(row[0]))
sqlUpdate22 = sqlUpdate2.replace( "#", str(row[0]))
sqlUpdate33 = sqlUpdate3.replace( "#", str(row[0]))
print sqlUpdate11
print sqlUpdate22
print sqlUpdate33
cursor.execute(sqlUpdate11)
cursor.execute(sqlUpdate22)
cursor.execute(sqlUpdate33)
else:
print cursor.rowcount
# Commit your changes in the database
db.commit()
except:
# Rollback in case there is any error
db.rollback()
# execute SQL query using execute() method.
cursor.execute("SELECT VERSION()")
# Fetch a single row using fetchone() method.
data = cursor.fetchone()
print "Database version : %s " % data
# disconnect from server
db.close()