Press ESC to close

python 操作mysql添加文章

代码:

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

import MySQLdb
import time


db = MySQLdb.connect("1.1.1.1","user01","@@@","xatest" )


cursor = db.cursor()

#数据来源
post_content = "python这是文章内容测试"
post_title = "title标题测试New"
post_name = "must_be_only_english"


times=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))


# SQL 语句
sql = """INSERT INTO wp_posts(post_author,post_date,post_date_gmt,post_content,post_title,post_excerpt,post_status,comment_status,ping_status,post_password,post_name,to_ping,pinged,post_modified,post_modified_gmt,post_content_filtered,post_parent,guid,menu_order,post_type,post_mime_type,comment_count) 
         VALUES ('1','%s','%s','%s','%s','','publish','open','open','','%s','','','%s','%s','','0','http://127.0.0.1/01wp/?p=20','0','post','','0')""" % (str(times),str(times),str(post_content),str(post_title),str(post_name),str(times),str(times))


try:
    # Execute the SQL command
    cursor.execute(sql)
    # Commit your changes in the     database
    db.commit()
    post_id = cursor.lastrowid
    if post_id >0:
      print("成功插入数据,数据编号为{}".format(post_id))
except:
    db.rollback()

db.close()

发表回复