openGauss数据库创建表,并给表字段添加注释。

#建立数据库连接

# connect() 方法的参数一般包括:
# database: 要连接的数据库名称
# user:连接数据库的用户名
# password: 连接数据库的密码
# host: 数据库端口的地址,一般为 “localhost”,或者主机的IP地址
# port: 门户 opengauss默认为26000.
con = psycopg2.connect(database="datasets", user="jack", password="gauss@111", host="192.168.80.130", port="26000")
#调用游标对象
cur = con.cursor()

 建表

 cur.execute('''CREATE TABLE  GongChengCanShuTest
       (Time TIMESTAMP PRIMARY KEY     NOT NULL  ,
       S2                 decimal  ,
       ElecCurrent100     decimal    ,
       Fwire2ElecCurrent   numeric);''')
 print("创建表成功")

 将csv文件中的数据按行插入数据库

def AddCSVData(file_path):
    data = pd.read_csv(file_path, encoding='gb2312', skiprows=1)  #skiprows跳过第一行表头
    con = psycopg2.connect(database="datasets", user="jack", password="gauss@111", host="192.168.80.130", port="26000")
    # 调用游标对象
    cur = con.cursor()

    n = len(data) #data的行数
    for i in range(n):
        time1= data.iloc[i,0]
        timeArray = datetime.strptime(time1, "%Y-%m-%d-%H:%M:%S.%f")
        s2 = data.iloc[i,269]
        E100 = data.iloc[i,375]
        F2EC = data.iloc[i,377]
        print(timeArray,s2,E100,F2EC)
        sql ="INSERT INTO gongchengcanshutest (time,S2,ElecCurrent100,Fwire2ElecCurrent) VALUES (%s,%s,%s,%s);"
        para = (timeArray,s2,E100,F2EC,) #传入参数
        cur.execute(sql,para)
        con.commit()

    print('done')
    con.close()

你可能感兴趣的:(高斯数据库openGauss,数据库连接,python,数据库,database,python)