python mysql 操作备忘

连接和获取数据

可以使用 pymysql lib;

import pymysql
from pymysql.constants import CLIENT

news_db = pymysql.connect(host='xx', port=xxx, user='xxx', passwd="xxx", db='xxx', client_flag=CLIENT.MULTI_STATEMENTS)
news_cursor = news_db.cursor()
def fetch_news_title(start_time,end_time,start_offset, batch_size):
    sql = "SELECT xxxx >= '{}' and xxx<'{}' " \
          "limit {}, {}".format(start_time, end_time, start_offset,batch_size)
    news_cursor.execute(sql)
    result = []
    for row in news_cursor.fetchall():
        result.append(row)

    return result

你可能感兴趣的:(python mysql 操作备忘)