Python操作数据库案例
上一节
下一节
四、Python操作数据库案例
例: 编写程序将前面从Excel文件中读取的内容保存到数据库中去。

创建一张数据库表school
向school表中插入记录
def create_table():
connect = sqlite3.connect("school.db") # 连接数据库
cursor = connect.cursor() # 得到游标
create_table_sql = """
create table if not exists school (
school_code,
school,
province,
is_985,
is_211,
is_self_marking,
school_type )
"""
cursor.execute(create_table_sql) # 执行建表操作
connect.commit() # 提交操作
执行该段程序后,将会在当前目录生成一个school.db文件,该文件即为sqlite 数据库文件,可通过可视化工具查看,例如 SQLiteExpert。

