首页 > 数据库技术 > 详细

python+postgreSql

时间:2015-03-20 06:56:23      阅读:444      评论:0      收藏:0      [点我收藏+]

 

def getopenconnection(user=postgres, password=1234, dbname=dds_assgn1):
    return psycopg2.connect("dbname=‘" + dbname + "‘ user=‘" + user + "‘ host=‘localhost‘ password=‘" + password + "")


def loadratings(ratingstablename, ratingsfilepath, openconnection):
    cur = openconnection.cursor()
    cur.execute(Drop Table if exists Ratings; CREATE TABLE Ratings(UserID int NOT NULL, movieid int NOT NULL, Rating double precision, CONSTRAINT key PRIMARY KEY (UserID, movieid));)
    with open(ratingsfilepath, "r") as ins:
        for line in ins:
            array=line.split("::")
            cur.execute(INSERT INTO Ratings VALUES ({0},{1},{2}) .format(array[0],array[1],array[2]))
    openconnection.commit()
    cur.close()
    pass

def rangepartition(ratingstablename, numberofpartitions, openconnection):
    cur=openconnection.cursor()
    #part=(5-0.5)/numberofpartitions
    part=5/numberofpartitions
    for i in range(0,numberofpartitions):
        str=Drop Table if exists range_part{0};CREATE TABLE range_part{0}(UserID int NOT NULL, movieid int NOT NULL, Rating double precision, PRIMARY KEY (UserID, movieid));.format(i)
        print(str)
        cur.execute(str)
    cur.execute(select * from {0}.format(ratingstablename))
    result=cur.fetchall()
    for row in result:
        #i=(row[2]-0.01-0.5)/part
        i=(row[2]-0.01)/part
        i=int (i)
        cur.execute(INSERT INTO range_part{3} VALUES ({0},{1},{2}) .format(row[0],row[1],row[2],i))
    cur.execute(Drop Table if exists hrange_record;CREATE TABLE hrange_record(n text NOT NULL,part text NOT NULL);)
    cur.execute(INSERT INTO hrange_record VALUES ({0},{1}) .format(numberofpartitions,part))
    openconnection.commit()
    cur.close()
    pass

 

python+postgreSql

原文:http://www.cnblogs.com/lilyfindjobs/p/4352642.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!