zoukankan      html  css  js  c++  java
  • python操作数据库

    psycopg查询

    #coding:utf-8
    import psycopg2
    conn=psycopg2.connect(host='127.0.0.1',port=5432,user='postgres',password='1',database='postgis')
    print "opened db successfully"
    cursor=conn.cursor()
    cursor.execute("select name,type,state from wy")
    rows=cursor.fetchall()
    for row in rows:
       print "name:",row[0]
       print "type:",row[1]
       print "state:",row[2]
       print "*****"
    print "Records select successfully";
    conn.close()

    psycopg插入

    #coding:utf-8
    import psycopg2
    conn=psycopg2.connect(host='127.0.0.1',port=5432,user='postgres',password='1‘’,database='postgis')
    print "opened db successfully"
    cursor=conn.cursor()
    cursor.execute("insert into wy(mid,name,psw,type,state,creat_time) 
    values('7762cc64-5de1-11e6-ba75-000c2923b59f','admin2','1','测试组','1',CURRENT_TIMESTAMP)")
    conn.commit()
    print "Records created successfully";
    conn.close()

    psycopg修改

    #coding:utf-8
    import psycopg2
    conn=psycopg2.connect(host='127.0.0.1',port=5432,user='postgres',password='1',database='postgis')
    print "opened db successfully"
    cursor=conn.cursor()
    cursor.execute("update wy set type='UI组' where name='admin2'")
    conn.commit()
    print "Total number of rows updated:",cursor.rowcount
    cursor.execute("select name,type,state from wy")
    rows=cursor.fetchall()
    for row in rows:
    print "name:",row[0]
    print "type:",row[1]
    print "state:",row[2]
    print "*******"
    print "done";
    conn.close()

    psycopg删除

    #coding:utf-8
    import psycopg2
    conn=psycopg2.connect(host='127。0.0.1',port=5432,user='postgres',password='1',database='postgis')
    print "opened db successfully"
    cursor=conn.cursor()
    cursor.execute("delete from wy where name='admin2'")
    conn.commit()
    print "Total number of rows updated:",cursor.rowcount
    cursor.execute("select name,type,state from wy")
    rows=cursor.fetchall()
    for row in rows:
       print "name:",row[0]
       print "type:",row[1]
       print "state:",row[2]
       print "*******"
    print "done";
    conn.close()   
  • 相关阅读:
    Java基础__ToString()方法
    wamp的www目录更改为指定目录
    织梦Dedecms主要文件夹目录及模板文件说明
    下拉菜单
    替换代码行之间空白
    CSS 之 Opacity多浏览器透明度兼容处理
    网页字体特效代码
    jquery animate 动画效果使用说明
    CSS中属性position位置详解功能讲解与实例分析
    jQuery通过jquery.form.js插件使用AJAX提交Form表单
  • 原文地址:https://www.cnblogs.com/shijingjing07/p/5764784.html
Copyright © 2011-2022 走看看