寒假学习Day 1:SQLite - Python的学习

寒假学习Day 1:SQLite - Python的学习

今天写这篇博客,差不多算是关于sqlite3一个笔记吧
去年暑假小学期接触了一些,眼下最近好像需要用到,就复习了一下去年学的一些
这篇是关于最基础的几个操作的学习和实践
代码参考菜鸟教程https://www.runoob.com/sqlite/sqlite-python.html
代码非原创

第一步

学数据库选择sqlite3的一个原因是它可以直接在python中import,不需要单独下载(import sqlite3)另一个原因是大三上学期的时候会学MySQL,怕先学了MySQL会配不好环境到时候头疼;
从http://www.sqlitebrowser.org/ 网站上下载了DB browser,方便直观地打开建立的.db文件查看里面的具体内容。去年也下载过,神奇的是去年不太好用的DB browser今年用着还挺顺溜;
这一步准备就绪就可以放心地开始学习具体的操作了。

连接数据库

import sqlite3

conn = sqlite3.connect('test.db')

print ("Opened database successfully")

创建表

数据类型–详情参考

import sqlite3

conn = sqlite3.connect('test.db')
print ("Opened database successfully")
c = conn.cursor()
c.execute('''CREATE TABLE COMPANY
       (ID INT PRIMARY KEY     NOT NULL,#确保该列无NULL值
       NAME           TEXT    NOT NULL,
       AGE            INT     NOT NULL,
       ADDRESS        CHAR(50),
       SALARY         REAL);''')
print ("Table created successfully")
conn.commit()
conn.close()

INSERT 操作

import sqlite3

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

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

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

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

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

SELECT 操作

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

UPDATE 操作

使用 UPDATE 语句来更新任何记录,然后从 COMPANY 表中获取并显示更新的记录:

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

DELETE 操作

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

你可能感兴趣的:(SQLite-Python,数据库,sqlite)