PostgreSQL 的应用和配置文件的使用(python3)

import psycopg2
import csv

import configparser
#配置文件请在个人积分资源处下载

INITXT = "./connpsy.ini"
cf = configparser.ConfigParser()
cf.read(INITXT)
db_host = cf.get("db", "db_host")
db_port = cf.get("db", "db_port")
db_databases = cf.get("db", "db_database")
db_user = cf.get("db", "db_user")
db_pass = cf.get("db", "db_pass")

#查找
def select_sql():
    # conn = psycopg2.connect(database='fotoabledb', user='fotoable', password='FotoablebjYTWD1021', host='52.15.112.93',
    #                        port='5439')
    conn = psycopg2.connect(database=db_databases, user=db_user, password=db_pass, host=db_host,
                            port=db_port)

    csvFile = open('E://buy_new.csv', 'w', encoding='utf-8', newline="")
    writer = csv.writer(csvFile)
    cur = conn.cursor()
    cur.execute("select a.buyitem,a.buylevel,a.installtime from "
                "public.buy_new a limit 10")
    rows = cur.fetchall()
    for row in rows:
        print(row)
        writer.writerow(row)

    print(rows)
    conn.commit()
    cur.close()
    conn.close()

#建表
def create_table():
    conn = psycopg2.connect(database=db_databases, user=db_user, password=db_pass, host=db_host,
                            port=db_port)

    print('connect successful!')
    cursor = conn.cursor()
    cursor.execute('''create table public.member(
     id integer not null primary key,
     name varchar(32) not null,
     password varchar(32) not null,
     singal varchar(128)
     )''')
    conn.commit()
    conn.close()
    print('table public.member is created!')

#插入
def insert_table():
    conn = psycopg2.connect(database=db_databases, user=db_user, password=db_pass, host=db_host,
                            port=db_port)
    cursor = conn.cursor()

    cursor.execute("insert into public.member(id,name,password,singal)\
   values(1,'member0','password0','signal0')")

    cursor.execute("insert into public.member(id,name,password,singal)\
  values(2,'member1','password1','signal1')")

    cursor.execute("insert into public.member(id,name,password,singal)\
 values(3,'member2','password2','signal2')")

    cursor.execute("insert into public.member(id,name,password,singal)\
   values(4,'member3','password3','signal3')")

    conn.commit()
    conn.close()
    print('insert records into public.memmber successfully')

#查找
def select_table():
    conn = psycopg2.connect(database=db_databases, user=db_user, password=db_pass, host=db_host,
                            port=db_port)
    cursor = conn.cursor()
    cursor.execute("select id,name,password,singal from public.member where id>2")
    rows = cursor.fetchall()
    for row in rows:
        print('id=', row[0], ',name=', row[1], ',pwd=', row[2], ',singal=', row[3], '\n')

    conn.close()

更新
def update_table():
    conn = psycopg2.connect(database=db_databases, user=db_user, password=db_pass, host=db_host,
                            port=db_port)
    cursor = conn.cursor()
    cursor.execute("update public.member set name='update ...' where id=2")
    conn.commit()

    print("Total number of rows updated :", cursor.rowcount)

    cursor.execute("select id,name,password,singal from public.member")
    rows = cursor.fetchall()
    for row in rows:
        print('id=', row[0], ',name=', row[1], ',pwd=', row[2], ',singal=', row[3], '\n')

    conn.close()
#删除
def delete_table():
    conn = psycopg2.connect(database=db_databases, user=db_user, password=db_pass, host=db_host,
                            port=db_port)
    cursor = conn.cursor()
    print('begin delete')
    cursor.execute("delete from public.member where id=2")
    conn.commit()
    print('end delete')
    print("Total number of rows deleted :", cursor.rowcount)
    cursor.execute("select id,name,password,singal from public.member")
    rows = cursor.fetchall()
    for row in rows:
        print('id=', row[0], ',name=', row[1], ',pwd=', row[2], ',singal=', row[3], '\n')
    conn.close()

if __name__ == '__main__':
    delete_table()

你可能感兴趣的:(数据库)