Python3.9+Sqlite3

python&sqlite简介

Sqlite3作为Python内置的数据库,不需要再单独下载sqlite,直接import即可。

使用python操作sqlite

创建并连接数据库

也可以把数据库名称指定为 :memory:,这样就会在 RAM 中创建一个数据库而不会在硬盘上生成文件.

#!/usr/bin/python

import sqlite3
conn = sqlite3.connect('test.db')
print ("数据库打开成功")

创建库表

#!/usr/bin/python
import sqlite3

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

print ("数据库打开成功")
c = conn.cursor()
c.execute('''CREATE TABLE COMPANY
       (ID INT PRIMARY KEY     NOT NULL,
       NAME           TEXT    NOT NULL,
       AGE            INT     NOT NULL,
       ADDRESS        CHAR(50),
       SALARY         REAL);''')
print ("数据表创建成功")

conn.commit()
conn.close()

新增数据

#!/usr/bin/python
import sqlite3
conn = sqlite3.connect('test.db')
c = conn.cursor()
print ("数据库打开成功")

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 ("数据插入成功")
conn.close()

修改数据

import sqlite3

conn = sqlite3.connect('test.db')
c = conn.cursor()
print ("数据库打开成功")

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 ("数据操作成功")
conn.close()

删除数据

import sqlite3

conn = sqlite3.connect('test.db')
c = conn.cursor()
print ("数据库打开成功")

c.execute("DELETE from COMPANY where ID=2;")
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 ("数据操作成功")
conn.close()

查询数据

import sqlite3

conn = sqlite3.connect('test.db')
c = conn.cursor()
print ("数据库打开成功")

result = c.execute("SELECT id, name, address, salary  from COMPANY")
for row in result:
   print("ID = ", row[0])
   print("NAME = ", row[1])
   print("ADDRESS = ", row[2])
   print("SALARY = ", row[3], "\n")

print ("数据操作成功")
conn.close()

查询数据库所有表名

import sqlite3

conn = sqlite3.connect("E:\python\test.db")
cursor = conn.cursor()
sql = """select name from sqlite_master where type='table' order by name"""
cursor.execute(sql)
result = cursor.fetchall()
print(result)
print(type(result))
conn.close()

查看表所有字段信息

import sqlite3
conn = sqlite3.connect("test.db")
cursor = conn.cursor()
sql = """pragma table_info(COMPANY)"""
cursor.execute(sql)
result = cursor.fetchall()
print(result)
print(type(result))
conn.close()

如果这篇文章对你有用,麻烦关注一下本人微信公众号~

Python3.9+Sqlite3

Original: https://www.cnblogs.com/aeolian/p/16488080.html
Author: 秋夜雨巷
Title: Python3.9+Sqlite3

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/9834/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

发表回复

登录后才能评论
免费咨询
免费咨询
扫码关注
扫码关注
联系站长

站长Johngo!

大数据和算法重度研究者!

持续产出大数据、算法、LeetCode干货,以及业界好资源!

2022012703491714

微信来撩,免费咨询:xiaozhu_tec

分享本页
返回顶部