4、python程序开发

4.1、安装psycopg2模块

  1. [root@VM_0_29_centos ~]# yum install python-psycopg2

4.2、连接数据库

  1. #coding=utf-8
  2. #!/usr/bin/python
  3. import psycopg2
  4. try:
  5. conn = psycopg2.connect(database="postgres", user="tbase", password="", host="172.16.0.29", port="15432")
  6. print "连接数据库成功"
  7. conn.close()
  8. except psycopg2.Error,msg:
  9. print "连接数据库出错,错误详细信息: %s" %(msg.args[0])

运行

  1. [tbase@VM_0_29_centos python]$ python conn.py
  2. 连接数据库成功

4.3、创建数据表

  1. #coding=utf-8
  2. #!/usr/bin/python
  3. import psycopg2
  4. try:
  5. conn = psycopg2.connect(database="postgres", user="tbase", password="", host="172.16.0.29", port="15432")
  6. print "连接数据库成功"
  7. cur = conn.cursor()
  8. sql = """
  9. create table tbase
  10. (
  11. id int,
  12. nickname varchar(100)
  13. )distribute by shard(id) to group default_group
  14. """
  15. cur.execute(sql)
  16. conn.commit()
  17. print "建立数据表成功"
  18. conn.close()
  19. except psycopg2.Error,msg:
  20. print "TBase Error %s" %(msg.args[0])

运行

  1. [tbase@VM_0_29_centos python]$ python createtable.py
  2. 连接数据库成功
  3. 建立数据表成功

4.4、插入数据

  1. #coding=utf-8
  2. #!/usr/bin/python
  3. import psycopg2
  4. try:
  5. conn = psycopg2.connect(database="postgres", user="tbase", password="", host="172.16.0.29", port="15432")
  6. print "连接数据库成功"
  7. cur = conn.cursor()
  8. sql = "insert into tbase values(1,'tbase'),(2,'tbase');"
  9. cur.execute(sql)
  10. sql = "insert into tbase values(%s,%s)"
  11. cur.execute(sql,(3,'pg'))
  12. conn.commit()
  13. print "插入数据成功"
  14. conn.close()
  15. except psycopg2.Error,msg:
  16. print "操作数据库出库 %s" %(msg.args[0])

运行

  1. [tbase@VM_0_29_centos python]$ python insert.py
  2. 连接数据库成功
  3. 插入数据成功

4.5、查询数据

  1. #coding=utf-8
  2. #!/usr/bin/python
  3. import psycopg2
  4. try:
  5. conn = psycopg2.connect(database="postgres", user="tbase", password="", host="172.16.0.29", port="15432")
  6. print "连接数据库成功"
  7. cur = conn.cursor()
  8. sql = "select * from tbase"
  9. cur.execute(sql)
  10. rows = cur.fetchall()
  11. for row in rows:
  12. print "ID = ", row[0]
  13. print "NICKNAME = ", row[1],"\n"
  14. conn.close()
  15. except psycopg2.Error,msg:
  16. print "操作数据库出库 %s" %(msg.args[0])

运行

  1. [tbase@VM_0_29_centos python]$ python select.py
  2. 连接数据库成功
  3. ID = 1
  4. NICKNAME = tbase
  5. ID = 2
  6. NICKNAME = pgxz
  7. ID = 3
  8. NICKNAME = pg

4.6、copy from 加载文件到表

  1. #coding=utf-8
  2. #!/usr/bin/python
  3. import psycopg2
  4. try:
  5. conn = psycopg2.connect(database="postgres", user="tbase", password="", host="172.16.0.29", port="15432")
  6. print "连接数据库成功"
  7. cur = conn.cursor()
  8. filename = "/data/tbase/tbase.txt"
  9. cols = ('id','nickname')
  10. tablename="public.tbase"
  11. cur.copy_from(file=open(filename),table=tablename,columns=cols,sep=',')
  12. conn.commit()
  13. print "导入数据成功"
  14. conn.close()
  15. except psycopg2.Error,msg:
  16. print "操作数据库出库 %s" %(msg.args[0])

执行

  1. [tbase@VM_0_29_centos python]$ python copy_from.py
  2. 连接数据库成功
  3. 导入数据成功