python sqlite3 连接到数据库,创建表,INSERT 、SELECT、UPDATE 、DELETE等简单操作

Python sqlite3 模块它提供了一个SQL接口兼容。不需要单独安装这个模块,因为它默认情况下随着Python版本在2.5.x 一起安装。

要使用sqlite3模块,必须首先创建一个连接对象,表示数据库中,然后可以选择创建游标对象,这将帮助在执行的所有SQL语句。

这里只说python下的基本操作,如果需要更高级的朋友们,可以去查看官方API文档,地址:https://docs.python.org/2/library/sqlite3.html

(以下代码均在python 2.7.10下测试可行)

1、连接到数据库
Python代码显示了如何连接到一个现有的数据库。如果数据库不存在,那么它就会被创建,终于将返回一个数据库对象

#coding=utf-8
import sqlite3

#打开一个数据库,如果没有则会自动创建一个,
conn = sqlite3.connect('test.db')#在当前位置,创建(硬盘上)
#conn = sqlite3.connect('memory')#在内存中创建(RAM)

print "Opened database successfully"

2、创建表
以下Python程序将使用以前创建的数据库中创建一个表:

import sqlite3

conn = sqlite3.connect('test.db')
print "Opened database successfully";

conn.execute('''CREATE TABLE COMPANY
       (ID INT PRIMARY KEY     NOT NULL,
       NAME           TEXT    NOT NULL,
       AGE            INT     NOT NULL,
       ADDRESS        CHAR(50),
       SALARY         REAL);''')
print "Table created successfully";

conn.close()

3、INSERT 操作
Python程序显示了我们如何能够创建表COMPANY 在上面的例子中创建表中的记录:

import sqlite3

conn = sqlite3.connect('test.db')
print "Opened database successfully";

conn.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
      VALUES (1, 'Paul', 32, 'California', 20000.00 )");

conn.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
      VALUES (2, 'Allen', 25, 'Texas', 15000.00 )");

conn.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
      VALUES (3, 'Teddy', 23, 'Norway', 20000.00 )");

conn.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
      VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 )");

conn.commit()
print "Records created successfully";
conn.close()

4、SELECT 操作
Python程序,表明我们如何能够获取并显示COMPANY 在上面的例子中创建表的记录:

import sqlite3

conn = sqlite3.connect('test.db')
print "Opened database successfully";

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()

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

import sqlite3

conn = sqlite3.connect('test.db')
print "Opened database successfully";

conn.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()

6、DELETE 操作
Python代码显示了如何我们可以使用DELETE语句删除任何记录,然后获取并显示剩余的记录COMPANY 表:

import sqlite3

conn = sqlite3.connect('test.db')
print "Opened database successfully";

conn.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()

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