Loading

Python连接SQLite数据库基本操作

import sqlite3

# 连接数据库(如果不存在则创建一个数据库)
conn = sqlite3.connect('d:/test.db')
# 创建一个游标
c = conn.cursor()

# 创建表 
create_tbl_sql = '''create table info(
                id int primary key not null ,
                name message_text not null ,
                age int not null,
                address char(50));'''               
c.execute(create_tbl_sql)

# 增删改查
c.execute("INSERT INTO info (ID,NAME,AGE,ADDRESS) VALUES (1, 'alx', 82, 'Shanghai')")
c.execute("UPDATE info set ADDRESS = 'NewYork' where id=1")
c.execute("DELETE from info where id=1;")

info_data = c.execute("SELECT id, name, age,address from info")
for data in info_data:
    print("id = ", data[0])
    print("name = ", data[1])
    print("age = ", data[2])
    print("address = ", data[3])

# 提交
conn.commit()
# 关闭连接
conn.close()
posted @ 2021-10-27 21:05  北兢王  阅读(658)  评论(0)    收藏  举报