程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python article series - SQLite details

編輯:Python
  • sqlite_demo
 The import module
mport sqlite3
Connect to database
onn = sqlite3.connect('test.db')
Create cursors
s = conn.cursor()
Create table
cs.execute('''CREATE TABLE student
(id varchar(20) PRIMARY KEY,
name varchar(20));''')
close Cursor
cs.close()
Submit
conn.commit()
Close the connection
conn.close()
newly added
cs.execute("INSERT INTO student (id, name) VALUES ('1', 'Jhon')")
cs.execute("INSERT INTO student (id, name) VALUES ('2', 'Alan')")
cs.close()
conn.commit()
conn.close()
Inquire about
cs.execute("SELECT id, name FROM student")
data = cs.fetchall()
print(data)
cs.close()
conn.close()
modify
cs.execute("SELECT id, name FROM student WHERE id = '1'")
print(' Before the change -->', cs.fetchall())
cs.execute("UPDATE student set name = 'Nicolas' WHERE id = '1'")
cs.execute("SELECT id, name FROM student WHERE id = '1'")
print(' After modification -->', cs.fetchall())
conn.commit()
cs.close()
conn.close()
Delete
cs.execute("SELECT id, name FROM student")
print(' Before deleting -->', cs.fetchall())
cs.execute("DELETE FROM student WHERE id = '1'")
cs.execute("SELECT id, name FROM student")
print(' After deleting -->', cs.fetchall())
conn.commit()
cs.close()
conn.close()

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved