码迷,mamicode.com
首页 > 数据库 > 详细

python+postgreSql

时间:2015-03-20 06:58:26      阅读:185      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!