INSERT 操作

Python程序显示了我们如何创建表COMPANY 在上面的例子中创建表中的记录:

  1. #!/usr/bin/python 
  2.  
  3. import psycopg2 
  4.  
  5. conn = psycopg2.connect(database="testdb"user="postgres"password="pass123"host="127.0.0.1"port="5432"
  6. print "Opened database successfully" 
  7.  
  8. cur = conn.cursor() 
  9.  
  10. cur.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \ 
  11.       VALUES (1, 'Paul', 32, 'California', 20000.00 )"); 
  12.  
  13. cur.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \ 
  14.       VALUES (2, 'Allen', 25, 'Texas', 15000.00 )"); 
  15.  
  16. cur.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \ 
  17.       VALUES (3, 'Teddy', 23, 'Norway', 20000.00 )"); 
  18.  
  19. cur.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \ 
  20.       VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 )"); 
  21.  
  22. conn.commit() 
  23. print "Records created successfully"; 
  24. conn.close() 

上述程序执行时,它会创建COMPANY表中的记录,并显示以下两行:

  1. Opened database successfully 
  2. Records created successfully 

SELECT 操作

Python程序,显示如何获取并显示COMPANY 表在上面的例子中创建的记录:

  1. #!/usr/bin/python 
  2.  
  3. import psycopg2 
  4.  
  5. conn = psycopg2.connect(database="testdb"user="postgres"password="pass123", host="127.0.0.1", port="5432"
  6. print "Opened database successfully" 
  7.  
  8. cur = conn.cursor() 
  9.  
  10. cur.execute("SELECT id, name, address, salary  from COMPANY"
  11. rows = cur.fetchall() 
  12. for row in rows
  13.    print "ID = ", row[0] 
  14.    print "NAME = ", row[1] 
  15.    print "ADDRESS = ", row[2] 
  16.    print "SALARY = ", row[3], "\n" 
  17.  
  18. print "Operation done successfully"
  19. conn.close() 

当上述程序执行时,它会产生以下结果:

  1. Opened database successfully 
  2. ID =  1 
  3. NAME =  Paul 
  4. ADDRESS =  California 
  5. SALARY =  20000.0 
  6.  
  7. ID =  2 
  8. NAME =  Allen 
  9. ADDRESS =  Texas 
  10. SALARY =  15000.0 
  11.  
  12. ID =  3 
  13. NAME =  Teddy 
  14. ADDRESS =  Norway 
  15. SALARY =  20000.0 
  16.  
  17. ID =  4 
  18. NAME =  Mark 
  19. ADDRESS =  Rich-Mond 
  20. SALARY =  65000.0 
  21.  
  22. Operation done successfully 

UPDATE 操作

Python代码显示如何,我们可以使用UPDATE语句来更新记录,然后从COMPANY表获取并显示更新的记录:

  1. #!/usr/bin/python 
  2.  
  3. import psycopg2 
  4.  
  5. conn = psycopg2.connect(database="testdb"user="postgres"password="pass123", host="127.0.0.1", port="5432"
  6. print "Opened database successfully" 
  7.  
  8. cur = conn.cursor() 
  9.  
  10. cur.execute("UPDATE COMPANY set SALARY = 25000.00 where ID=1"
  11. conn.commit 
  12. print "Total number of rows updated :", cur.rowcount 
  13.  
  14. cur.execute("SELECT id, name, address, salary  from COMPANY"
  15. rows = cur.fetchall() 
  16. for row in rows
  17.    print "ID = ", row[0] 
  18.    print "NAME = ", row[1] 
  19.    print "ADDRESS = ", row[2] 
  20.    print "SALARY = ", row[3], "\n" 
  21.  
  22. print "Operation done successfully"
  23. conn.close() 

当上述程序执行时,它会产生以下结果:

  1. Opened database successfully 
  2. Total number of rows updated : 1 
  3. ID =  1 
  4. NAME =  Paul 
  5. ADDRESS =  California 
  6. SALARY =  25000.0 
  7.  
  8. ID =  2 
  9. NAME =  Allen 
  10. ADDRESS =  Texas 
  11. SALARY =  15000.0 
  12.  
  13. ID =  3 
  14. NAME =  Teddy 
  15. ADDRESS =  Norway 
  16. SALARY =  20000.0 
  17.  
  18. ID =  4 
  19. NAME =  Mark 
  20. ADDRESS =  Rich-Mond 
  21. SALARY =  65000.0 
  22.  
  23. Operation done successfully 


评论关闭