AJax技术

SQLite Python(2)

字号+ 作者:H5之家 来源:H5之家 2017-12-07 13:02 我要评论( )

下面的 Python 程序显示了如何从前面创建的 COMPANY 表中获取并显示记录: #!/usr/bin/pythonimport sqlite3conn = sqlite3.connect('test.db')c = conn.cursor()print "Opened database successfully";cursor = c.

下面的 Python 程序显示了如何从前面创建的 COMPANY 表中获取并显示记录:

#!/usr/bin/python import sqlite3 conn = sqlite3.connect('test.db') c = conn.cursor() print "Opened database successfully"; cursor = c.execute("SELECT id, name, address, salary from COMPANY") for row in cursor: print "ID = ", row[0] print "NAME = ", row[1] print "ADDRESS = ", row[2] print "SALARY = ", row[3], "\n" print "Operation done successfully"; conn.close()

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

Opened database successfully ID = 1 NAME = Paul ADDRESS = California SALARY = 20000.0 ID = 2 NAME = Allen ADDRESS = Texas SALARY = 15000.0 ID = 3 NAME = Teddy ADDRESS = Norway SALARY = 20000.0 ID = 4 NAME = Mark ADDRESS = Rich-Mond SALARY = 65000.0 Operation done successfully UPDATE 操作

下面的 Python 代码显示了如何使用 UPDATE 语句来更新任何记录,然后从 COMPANY 表中获取并显示更新的记录:

#!/usr/bin/python import sqlite3 conn = sqlite3.connect('test.db') c = conn.cursor() print "Opened database successfully"; c.execute("UPDATE COMPANY set SALARY = 25000.00 where ID=1") conn.commit() print "Total number of rows updated :", conn.total_changes cursor = conn.execute("SELECT id, name, address, salary from COMPANY") for row in cursor: print "ID = ", row[0] print "NAME = ", row[1] print "ADDRESS = ", row[2] print "SALARY = ", row[3], "\n" print "Operation done successfully"; conn.close()

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

Opened database successfully Total number of rows updated : 1 ID = 1 NAME = Paul ADDRESS = California SALARY = 25000.0 ID = 2 NAME = Allen ADDRESS = Texas SALARY = 15000.0 ID = 3 NAME = Teddy ADDRESS = Norway SALARY = 20000.0 ID = 4 NAME = Mark ADDRESS = Rich-Mond SALARY = 65000.0 Operation done successfully DELETE 操作

下面的 Python 代码显示了如何使用 DELETE 语句删除任何记录,然后从 COMPANY 表中获取并显示剩余的记录:

#!/usr/bin/python import sqlite3 conn = sqlite3.connect('test.db') c = conn.cursor() print "Opened database successfully"; c.execute("DELETE from COMPANY where ID=2;") conn.commit() print "Total number of rows deleted :", conn.total_changes cursor = conn.execute("SELECT id, name, address, salary from COMPANY") for row in cursor: print "ID = ", row[0] print "NAME = ", row[1] print "ADDRESS = ", row[2] print "SALARY = ", row[3], "\n" print "Operation done successfully"; conn.close()

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

Opened database successfully Total number of rows deleted : 1 ID = 1 NAME = Paul ADDRESS = California SALARY = 20000.0 ID = 3 NAME = Teddy ADDRESS = Norway SALARY = 20000.0 ID = 4 NAME = Mark ADDRESS = Rich-Mond SALARY = 65000.0 Operation done successfully

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • 用Python模拟ajax请求

    用Python模拟ajax请求

    2017-12-04 08:00

  • python虚拟环境virtualenv的使用教程

    python虚拟环境virtualenv的使用教程

    2017-10-31 09:51

  • Python实现扩展内置类型的方法分析

    Python实现扩展内置类型的方法分析

    2017-10-30 14:10

  • Python中的浮点数原理与运算分析

    Python中的浮点数原理与运算分析

    2017-10-28 17:04

网友点评