利用python语言连接数据库

①去https://sqlitebrowser.org/下载DB Browser for SQLite

②利用python语言创建数据库连线
#ch29_3.py
import sqlite3
conn = sqlite3.connect(“myInfo.db”) # 資料庫連線
sql = ‘’‘Create table students(
id int,
name text,
gender text
score int)’’’
conn.execute(sql) # 執行SQL指令
conn.close() # 關閉資料庫連線
③将所创建数据库中的每一栏填入数据

ch29_4.py

import sqlite3
conn = sqlite3.connect(“myInfo.db”) # 資料庫連線
print(“請輸入myInfo資料庫students表單資料”)
while True:
new_id = int(input("請輸入id : ")) # 轉成整數
new_name = input("請輸入name : ")
new_gender = input("請輸入gender : ")
new_score = input("請輸入score : ")
x = (new_id, new_name, new_gender, new_score)
sql = ‘’‘insert into students values(?,?,?,?)’’’
conn.execute(sql,x)
conn.commit() # 更新資料庫
again = input("繼續(y/n)? ")
if again[0].lower() == “n”:
break
conn.close()
④运行结果显示如下:
利用python语言连接数据库利用python语言连接数据库