educoder/scripts/tablesplit.py

63 lines
1.6 KiB
Python

#!/usr/bin/env python
# coding=utf-8
import pymysql
def connectdb():
# db = pymysql.connect("localhost", "root", "NUDTpdl@", "ossean_production")
db = pymysql.connect("localhost", "root", "root", "test")
return db
def get_num(db):
cursor = db.cursor()
# id_sql = "select osp_id from relation_recommends order by osp_id desc limit 1"
id_sql = "select osp_id from simility_recommends order by osp_id desc limit 1"
try:
cursor.execute(id_sql)
return cursor.fetchone()[0]
except:
db.rollback()
def get_records(db, osp_id):
cursor = db.cursor()
# sql = "select * from relation_recommends WHERE osp_id=%d" % osp_id
sql = "select * from simility_recommends WHERE osp_id=%d" % osp_id
try:
cursor.execute(sql)
records = cursor.fetchall()
return records
except:
db.rollback()
def get_insert(db, table_index, record):
cursor = db.cursor()
# insert_sql = "insert into relation_recommends_%d VALUES %s" % (table_index, str(record))
insert_sql = "insert into simility_recommends_%d VALUES %s" % (table_index, str(record))
try:
cursor.execute(insert_sql)
db.commit()
except:
db.rollback()
if __name__ == '__main__':
db = connectdb()
num = get_num(db)
for i in range(1, num+1):
table_index = i % 100
records = get_records(db, i)
for record in records:
if record is None:
continue
get_insert(db, table_index, record)
print 'has reached ' + str(i) + " osp_id"